什么是CSS
如何学习
- CSS是什么
- CSS怎么用(快速入门)
- CSS选择器(重点)
- 美化网页(文字、阴影、超链接、列表、渐变…)
- 盒子模型
- 浮动
- 定位
- 网页动画(特效)
- 学习资源:菜鸟教程 https://www.runoob.com/
1.1 什么是CSS
Cascading Style Sheet层叠样式表
CSS:表现(美化网页)
字体,颜色,边框,高度,宽度,背景图片,网页定位,网页浮动…
1.2 发展史
CSS1.0
CSS2.0 DIV(块) + CSS,HTML与CSS结构分离的思想,网页变得简单,SEO
CSS2.1 浮动,定位
CSS3.0 圆角,阴影,动画… 浏览器兼容性~
练习格式:
1.3 快速入门
style
基本入门
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--规范 <style> 可以编写css代码,每一个声明,最好使用分号结尾
语法:
选择器{
声明1;
声明2;
声明3;
}
-->
<style>
h1{
color: red;
}
</style>
</head>
<body>
<h1>我是标题</h1>
</body>
</html>
建议CSS和HTML分离
CSS优势:
1、内容和表现分离
2、网页结构表现统一,可以实现复用
3、样式十分的丰富
4、建议使用独立于html的css文件
5、利于SEO,容易被搜索引擎收录
2 选择器
作用:选择页面上的某一个或者某一类元素
2.1 基本选择器
1、标签选择器:选择一类标签 标签{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*标签选择器,会选择到页面上所有的这个标签的元素*/
h1{
color: #ff4292;
background: #3cbda6;
border-radius: 24px;
}
p{
font-size: 80px;
}
</style>
</head>
<body>
<h1>学java</h1>
<h1>学java</h1>
<p>学java基础</p>
</body>
</html>
2、类选择器 class:选择所有class属性一致的标签,跨标签 .类名{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*类选择器的格式 .class的名称
好处:可以多个标签归类,是同一个class,可以复用
*/
.zhangyu{
color: #1d3d80;
}
.yuzhang{
color: #ff4292;
}
</style>
</head>
<body>
<h1 class="zhangyu">标题1</h1>
<h1 class="yuzhang">标题2</h1>
<h1 class="zhangyu">标题3</h1>
<p class="yuzhang">p标签</p>
</body>
</html>
3、id选择器:全局唯一 #id名{}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*id选择器:id只能保证全局唯一
#id名称{
不遵循就近原则,固定的
id选择器>class选择器>标签选择器
}
*/
#zhangyu{
color: #ff955b;
}
.style1{
color: red;
}
h1{
color: #32ff2d;
}
</style>
</head>
<body>
<!--id只能全局唯一-->
<h1 id="zhangyu">标题1</h1>
<h1 class="style1">标题2</h1>
<h1 class="style1">标题3</h1>
<h1>标题4</h1>
<h1>标题5</h1>
</body>
</html>
优先级:id>class>标签选择器
2.2 层次选择器
1、后代选择器:在某个元素的后面
/*后代选择器*/
body p{
background: red;
}
2、子选择器:只有后面一代
/*子选择器*/
body>p{
background: #32ff2d;
}
3、相邻兄弟选择器:同辈(只对向下的兄弟有效)
/*相邻兄弟选择器:只有一个,相邻(向下)*/
.active +p{
background: #ff4292;
}
4、通用选择器
/*通用兄弟选择器:当前选中元素向下的所有兄弟元素*/
.active~p{
background: #32ff2d;
}
2.3 结构伪类选择器
伪类:加上一些条件使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--避免使用id和class选择器-->
<style>
/*ul的第一个子元素*/
ul li:first-child{
background: #32ff2d;
}
/*ul的最后一个子元素*/
ul li:last-child{
background: #ff4292;
}
/*选中p1:定位到父元素,选择当前的第一个元素
选则当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效
*/
p:nth-child(1){
background: aqua;
}
/*选中父元素下的p元素的第二个类型为p的元素*/
p:nth-of-type(2){
background: yellow;
}
a:hover {
background: black;
}
</style>
</head>
<body>
<!--<a href="">请点我</a>
<h1>h1</h1>-->
<p>p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul>
</body>
</html>
效果图如下:
2.4 属性选择器(比较常用)
采用属性的方法来选取元素,将id和class属性相结合
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.demo a{
float: left;
display: block;
height: 50px;
width: 50px;
border-radius: 10px;
background: blue;
text-align: center;
color: gainsboro;
text-decoration: none;
margin-right: 5px;
font: bold 20px/50px Arial;
}
/*属性名,属性名 = 属性值(正则)
= 绝对等于
*= 包含这个元素
^= 以这个开头
$= 以这个结尾
*/
/*存在id属性的元素 a[]{}*/
/* a[id]{
background: yellow;
}*/
/*id=first的元素*/
/* a[id="first"]{
background: yellow;
}*/
/*class 中有link的元素*/
/*a[class*="links"]{
background: yellow;
}*/
/*选中href中以http开头的元素*/
/*a[href^=http]{
background: yellow;
}*/
a[href$=pdf]{
background: yellow;
}
</style>
</head>
<body>
<p class="demo">
<a href="http://www.baidu.com" class="links item first" id="first">1</a>
<a href="http://www" class="links item active" target="_blank" title="test">2</a>
<a href="images/123.html" class="links item">3</a>
<a href="images/123.png" class="links item">4</a>
<a href="images/123.jpg" class="links item">5</a>
<a href="/abc" class="links item">6</a>
<a href="/a.pdf" class="links item">7</a>
<a href="/abc.pdf" class="links item">8</a>
<a href="abc.doc" class="links item">9</a>
<a href="abcd.doc" class="links item last">10</a>
</p>
</body>
</html>
效果图如下:
3 美化网页元素
3.1 为什么要美化网页
1、有效的传递重要的页面信息
2、可以使网页更加美观,吸引用户
3、凸显页面的相关主题
4、美化网页可以提高用户的体验
span标签:重点要突出的字,就可以使用span标签套起来
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#title1{
font-size: 50px;
}
</style>
</head>
<body>
欢迎学习<span id="title1">java</span>
</body>
</html>
3.2 字体样式
<!--
font-family:字体
font-size:字体大小
font-weight:字体的粗细
color:字体颜色
-->
<style>
body{
font-family: "Arial Black",楷体;
color: #ff955b;
}
h1{
font-size: 50px;
}
.p1{
font-weight: bold;
}
</style>
3.3 文本样式
1、颜色 color rgb rgba
2、文本对齐的方式 text-align=center
3、首行缩进 text-indent: 2em;
4、行高 line-height:单行文字上下居中! line-height=height
5、装饰 text-decoration:
6、文本图片水平对齐 vertical-align: middle;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--
颜色:
单词
RGB 0~F
RGBA A:0~1
text-align:排版,居中
text-indent:2em;段落首行缩进
height:300px
line-height:300px
行高和块的高度一致就可以上下居中
-->
<style>
h1{
color:rgba(0,255,255,0.9) ;
text-align: center;
}
.p1{
text-indent: 2em;
}
.p3{
background: indigo;
height: 300px;
line-height: 300px;
}
.l1{
/*下划线*/
text-decoration: underline;
}
.l2{
/*中划线*/
text-decoration: line-through;
}
.l3{
/*上划线*/
text-decoration: overline;
}
a{
/*超链接去下划线*/
text-decoration: none;
}
/* 水平对齐有参照物*/
img,span {
vertical-align: middle;
}
</style>
</head>
<body>
<a href="">123</a>
<p class="l1">123123</p>
<p class="l2">123123</p>
<p class="l3">123123</p>
<h1>故事介绍</h1>
<p class="p1">
故事介绍编辑
平静安详的元泱境界,每隔333年,总会有一个神秘而恐怖的异常生物重生,它就是魁拔!魁拔的每一次出现,都会给元泱境界带来巨大的灾难!即便是天界的神族,也在劫难逃。在天地两界各种力量的全力打击下,魁拔一次次被消灭,但又总是按333年的周期重新出现。魁拔纪元1664年,天神经过精确测算后,在魁拔苏醒前一刻对其进行毁灭性打击。但谁都没有想到,由于一个差错导致新一代魁拔成功地逃脱了致命一击。很快,天界魁拔司和地界神圣联盟均探测到了魁拔依然生还的迹象。因此,找到魁拔,彻底消灭魁拔,再一次成了各地热血勇士的终极目标。
</p>
<p>
在偏远的兽国窝窝乡,蛮大人和蛮吉每天为取得象征成功和光荣的妖侠纹耀而刻苦修炼,却把他们生活的村庄搅得鸡犬不宁。村民们绞尽脑汁把他们赶走。一天,消灭魁拔的征兵令突然传到窝窝乡,村长趁机怂恿蛮大人和蛮吉从军参战。然而,在这个一切都凭纹耀说话的世界,仅凭蛮大人现有的一块冒牌纹耀,不要说参军,就连住店的资格都没有。受尽歧视的蛮吉和蛮大人决定,混上那艘即将启程去消灭魁拔的巨型战舰,直接挑战魁拔,用热血换取至高的荣誉。
</p>
<p class="p3">
When I wake up in the morning,You are all I see;When I think about you,And how happy you make me.You're everything I wanted; You're everything I need; I look at you and know; That you are all to me。 Barry Fitzpatrick
</p>
<p>
<img src="images/a.jpg">
<span>dhsuusklkikoslmxsjm</span>
</p>
</body>
</html>
3.4 阴影
阴影属性的书写格式为:
text-shadow:阴影颜色 水平偏移 垂直偏移 阴影半径
注意:水平偏移为正数时,表示向右偏移,为负数时,表示向左偏移
垂直偏移为正数时表示向下偏移,为负数时,表示向上偏移
/*text-shadow:阴影颜色 水平偏移 垂直偏移 阴影半径*/
#price{
text-shadow: skyblue 20px -10px 2px ;
}
3.5 超链接伪类
正常情况下,a,a:hover比较常用
a{
text-decoration: none;
color: black;
}
/*鼠标悬浮的效果(只需要记住这个属性效果)*/
a:hover{
color: orange;
font-size: 50px;
}
3.6 列表
#nav{
width: 300px;
background: bisque;
}
.title{
font-size: 18px;
font-weight: bold;
text-indent: 1em;
line-height: 30px;
background: red;
}
/*ul li*/
/*
list-style:
none;去掉列表前的原点
circle; 空心圆
decimal; 数字
square; 正方形
*/
/*ul{
background: bisque;
}*/
ul li{
height: 30px;
list-style: none;
text-indent: 1em;
}
a{
text-decoration: none;
font-size: 14px;
color: #000000;
}
a:hover{
color: orange;
text-decoration: underline;
}
3.7 背景
背景颜色
背景图片
<style>
div{
width: 1000px;
height: 700px;
border: 1px solid red;
background-image: url("images/北邮校徽.jpg");
/*默认是全部平铺的*/
}
.div1{
/*背景水平平铺*/
background-repeat: repeat-x;
}
.div2{
/*背景竖直平铺*/
background-repeat: repeat-y;
}
.div3{
/*不平铺,保持背景原来的样子*/
background-repeat: no-repeat;
}
</style>
制作一个简单的导航栏,效果如下
(由于是新手,而且审美不怎么好,只能做出个大概框架,还请各位大佬帮我美化美化哦~)
html代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表样式</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div id="nav">
<h2 class="title">全部商品分类</h2>
<ul>
<li><a href="#">图书</a> <a href="#">音像</a> <a href="#">数字商品</a></li>
<li><a href="#">家用电器</a> <a href="#">手机</a> <a href="#">数码</a></li>
<li><a href="#">电脑</a> <a href="#">办公</a></li>
<li><a href="#">家居</a> <a href="#">家装</a> <a href="#">厨具</a></li>
<li><a href="#">服饰鞋帽</a> <a href="#">个护化妆</a></li>
<li><a href="#">礼品箱包</a> <a href="#">钟表</a> <a href="#">珠宝</a></li>
<li><a href="#">食品饮料</a> <a href="#">保健食品</a></li>
<li><a href="#">彩票</a> <a href="#">旅行</a> <a href="#">充值</a> <a href="#">票务</a></li>
</ul>
</div>
</body>
</html>
css代码如下:
#nav{
width: 300px;
background: bisque;
}
.title{
font-size: 18px;
font-weight: bold;
text-indent: 1em;
line-height: 35px;
/*颜色 图片路径 图片位置 平铺方式 */
background: red url("../images/箭头向下.gif") 270px 10px no-repeat;
}
/*ul li*/
/*
list-style:
none;去掉列表前的原点
circle; 空心圆
decimal; 数字
square; 正方形
*/
/*ul{
background: bisque;
}*/
ul li{
height: 30px;
list-style: none;
text-indent: 1em;
background-image: url("../images/箭头向右.gif");
background-repeat: no-repeat;
background-position: 229px 6px;
}
a{
text-decoration: none;
font-size: 14px;
color: #000000;
}
a:hover{
color: orange;
text-decoration: underline;
}
3.8 渐变
background-image: linear-gradient(19deg,#21D4FD 0%,#B721FF 100%);
4 盒子模型
在网页元素中,盒子元素是非常重要的,其基本模型如下图所示:
参数解释:
margin:外边距,简单理解就是盒子外边线与盒子元素的距离
padding:内边距,指一个元素的内容和其边界之间的距离
border:边框
4.2 边框
1、边框的粗细
2、边框的样式
3、边框的颜色