一、 什么是CSS
1.1 什么是CSS
Cascading Style Sheet层叠样式表
CSS:表现(美化网页)
字体,颜色,边距,高度,宽度,背景图片,网页定位,网页浮动
1.2 发展史
CSS1.0
CSS2.0:DIV(块)+CSS,HTML与CSS结构分离的思想,网页变得简单,SEO
CSS2.1:浮动,定位
CSS3.0:圆角、阴影、动画…浏览器兼容性~
1.3快速入门
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--规范,<style>可以编写CSS的代码,每一个声明最好以“;”结尾
语法:
选择器{
声明1;
声明2;
声明3;
}
-->
<style>
h1{
color: crimson;
}
</style>
</head>
<body>
<h1>CSS测试</h1>
</body>
</html>
- 最好写成如下这种形式:
CSS的优势: - 1、内容和表现分离;
- 2、网页结构表现统一,可以实现复用
- 3、样式十分的丰富
- 4、建议使用独立于html的css文件
- 5、利用SEO,容易被搜索引擎收录!
1.4 CSS的三种导入方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--内部样式-->
<style>
h1{
color: green;
}
</style>
<!--外部样式-->
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<!--优先级:就近原则-->
<!--行内样式:在标签元素中,编写一个style属性,编写样式即可-->
<h1 style="color: red">这是标签</h1>
</body>
</html>
【扩展】外部样式两种方法
- 链接式:
<!--外部样式-->
<link rel="stylesheet" href="css/style.css" />
- 导入式
@import是CSS2.1特有的!
<!--导入式-->
<style>
@import url("css/style.css");
</style>
二、选择器(重点+难点)
作用:选择页面上的某一个或者某一类元素
2.1基本选择器
标签选择器
- 标签选择器:选择一类标签 标签{ }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
h1{
color: orange;
background: blue;
border-radius: 10px;
}
</style>
</head>
<body>
<h1>标签选择器</h1>
</body>
</html>
类选择器
- 类选择器class:选择所有class一致的标签,跨标签
- 格式:.类名{ }
- 好处:可以多个标签归类,是同一个class,可以复用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*类选择器的格式 .class的名称{}
好处:可以多个标签归类,是同一个class,可以复用
*/
.demo1{
color: blue;
}
.demo2{
color: red;
}
.demo3{
color: aqua;
}
</style>
</head>
<body>
<h1 class = "demo1">类选择器:demo1</h1>
<h1 class="demo2">类选择器:demo2</h1>
<h1 class="demo3">类选择器:demo3</h1>
</body>
</html>
id选择器
- id 选择器:全局唯一
- 格式:#id名{ }
- 不遵循就近原则,优先级是固定的:
- id选择器 > 类选择器 > 标签选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*id选择器:id必须保证全局唯一
#id名称{}
不遵循就近原则,优先级是固定的
id选择器 > 类选择器 > 标签选择器
*/
#demo1{
color: aqua;
}
.demo2{
color: red;
}
#demo2{
color: orange;
}
h1{
color: blue;
}
</style>
</head>
<body>
<h1 id="demo1">id选择器:demo1</h1>
<h1 class="demo2" id = "demo2">id选择器:demo2</h1>
<h1 class="demo2">id选择器:demo3</h1>
<h1>id选择器:demo4</h1>
<h1>id选择器:demo5</h1>
</body>
</html>
2.2 层次选择器
层次关系:
后代选择器
后代选择器:在某个元素的后面,能影响其所有的后代(除了直系以外)
/*后代选择器*/
<style>
body p{
background:red;
}
</style>
子选择器
子选择器:一代
/*子选择器*/
<style>
body>p{
background:orange;
}
</style>
相邻兄弟选择器
相邻的兄弟选择器,相邻说的是只能在这个设置了class的标签下面那个紧挨着他的,与他同级的兄弟标签
/*相邻兄弟选择器:只有一个,相邻(向下)*/
<style>
.active+p{
background: red
}
</style>
<body>
<p>p0</p>
<p class="active">p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>
<p>p4</p>
</li>
<li>
<p>p5</p>
</li>
<li>
<p>p6</p>
</li>
</ul>
</body>
通用选择器
当前选中元素的向下的所有兄弟元素
<style>
/*通用兄弟选择器,当前选中元素的向下的所有兄弟元素*/
.active~p{
background:red;
}
</style>
<body>
<p>p0</p>
<p class="active">p1</p>
<p>p2</p>
<p>p3</p>
<ul>
<li>
<p>p4</p>
</li>
<li>
<p>p5</p>
</li>
<li>
<p>p6</p>
</li>
</ul>
</body>
2.3结构伪类选择器
主要用于定位元素,主要几前两个
<style>
/*ul的第一个子元素*/
ul li:first-child{
background: aqua;
}
/*ul的最后一个子元素*/
ul li:last-child{
background: blue;
}
/*选中p1:定位到父元素,选择当前的第一个元素
选择当前p元素 的父级元素,选中父级元素的第一个,并且是当前元素才生效!
*/
p:nth-child(1){
background: orange;
}
/*选中父元素下的,第2个p元素*/
p:nth-of-type(2){
background: red;
}
</style>
2.4、属性选择器(常用)
id + class结合
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.demo a{
display: block;
height: 50px;
width: 50px;
float:left;
border-radius: 10px;
background: blue;
text-align: center;
color: beige;
text-decoration: none;
margin-right: 5px;
/*分别代表字体的粗细、大小、行高、样式*/
font: bold 20px/50px Arial;
}
/*属性名,属性名=属性值(正则)
=表示绝对等于
*=表示包含
^=表示以...开头
$=表示以...结尾
存在id属性的元素 a[]{}
*/
/*将属性中有id的元素的背景颜色变为红色*/
/* a[id]{
background: red;
}*/
/*将属性中id=first的元素的背景色改为aqua*/
/* a[id=first]{
background: aqua;
}*/
/*将class中为links item first2的元素的背景颜色变为橙色*/
/* a[class = "links item2 first2"]{
background: orange;
}*/
/*将class中带有有item的元素的背景颜色变为橙色*/
/*a[class*="item"]{
background: black ;
}*/
/*选中href中以http开头的元素*/
/*a[href^="http"]{
background: orange;
}*/
/*选中href中以doc结尾的元素*/
a[href$=doc]{
background:lemonchiffon;
}
</style>
</head>
<body>
<p class="demo">
<a href="http://www.baidu.com" class="links item first" id="first">1</a>
<a href="/adad/faf" class="links item2 first2" >2</a>
<a href="qwe123" class="links item3 first3" >3</a>
<a href="eweqe" class="links item4 first4" >4</a>
<a href="rrrrr" class="links item5 first5" >5</a>
<a href="ttt.doc" class="links item6 first6" >6</a>
<a href="yyy.doc" class="links item7 first7" >7</a>
</p>
</body>
</html>
2.5组合选择器
可以将上述任意选择器进行组合选择
<style type="text/css">
.class01 , #id01{
color: blue;
font-size: 20px;
border: yellow 1px solid;
}
</style>
<body>
<div id="id01">div标签class01</div> <br />
<span class="class01">span 标签</span> <br />
</body>
三、美化网页元素
3.1为什么要美化网页
有效的传递页面信息
美化网页,页面漂亮才能吸引客户
凸显页面的主题
提高用户的体验
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>
- font-family:字体
- font-size:字体大小
- font-weight:字体粗细
<style>
/*用body框起来,代表页面下body内的所有内容,即文字,都会应用这种样式*/
body{
font-family:楷体;
color:red;
}
h1{
font-size: 50px;
}
.p1{
font-weight:blod;
}
</style>
在第一个代码基础上应用字体样式
3.2字体样式
font-weight:bolder;/*也可以填px,但不能超过900,相当于bloder*/
/*常用写法:风格(斜体) 粗细 大小 样式*/
font:oblique bloder 12px "楷体"
文本样式
- 颜色–>color
- 单词:red、blue……
- RGB:000000~FFFFFF
- RGBA:rgba(0,255,255,0.5)前面三个是rgb,代表颜色,后面的范围是从0到1,代表透明度
- 文本对齐方式–>text-align:center
- 首行缩进–>text-indent:2em(2个字母)
- 行高–>line-height:300px;(单行文字上下居中)
行高如果和字符所在的块的高度一致,字符会上下居中
.p3{
background: lemonchiffon;
/*字符所在的p(段落)的高度*/
height: 300px;
/*行高*/
line-height: 300px;
}
- 下划线–>text-decoration
<style>
.l1{
text-decoration: underline;
}
.l2{
text-decoration: line-through;
}
.l3{
text-decoration: overline;
}
a{
text-decoration: none;
}
</style>
<body>
<p class="l1">123321</p>
<p class="l2">123321</p>
<p class="l3">123321</p>
<a href="">123</a>
</body>
- 图片、文字水平对齐
<style>
/*水平对齐:a,b,是将a和b两个内容对齐,不能只写一个,因为相当于要有参照物*/
img,span{
vertical-align: middle;
}
</style>
<body>
<p>
<img src="../css/image/a.jpg" alt=""height="200px" width="200px">
<span>xixi</span>
</p>
</body>
3.4文本,阴影和超链接伪类
- 伪类
主要记前两个:
<style>
a{/*超链接有默认的颜色*/
text-decoration:none;
color:#000000;
}
a:hover{/*鼠标悬浮的状态*/
color:orange;
}
a:active{/*鼠标按住未释放的状态*/
color:green
}
a:visited{/*点击之后的状态*/
color:red
}
</style>
- 阴影(向右和向下偏移都是正数,两外两个方向是负数)
<style>
/* 第一个参数:表示水平偏移
第二个参数:表示垂直偏移
第三个参数:表示模糊半径
第四个参数:表示颜色
*/
text-shadow:5px 5px 5px 颜色
</style>
3.5列表ul li
主要记:list-style及其对应的值
<style>
/*放在div中,作为导航栏*/
.nav{
width:300px;
}
/*list-style{
none:去掉原点
circle:空心圆
decimal:数字
square:正方形
}*/
ul li{
height:30px;
list-style:none;
text-indent:1em;
}
a{
text-decoration:none;
font-size:14px;
color:#000;
}
a:hover{
color:orange;
text-decoration:underline
}
</style>
<body>
<div class="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>
3.6背景
背景颜色:background
背景图片:background-image
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*默认是全部平铺,填满整个div形成的区域*/
.div{
background-image: url("image/a.jpg");
}
/*只在x方向上平铺*/
.div1{
background-repeat: repeat-x;
}
/*只在y方向上平铺*/
.div2{
background-repeat: repeat-y;
}
/*只显示一个图片,不重复*/
.div3{
background-repeat: no-repeat;
}
</style>
</head>
<body>
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
</body>
只在x方向平铺:
只在y方向平铺:
只显示一个图片,不重复:
电脑右键——检查,就可以出现:
综合:
/*背景颜色 路径 位置(x方向 y方向) 图片显示是否平铺*/
background:red url("图片相对路劲") 270px 10px no-repeat
background-position:/*定位:背景位置(x方向 y方向)*/
<style>
.title{
font-size:18px;
font-weight: bold;
text-indent: 1em;
line-height: 35px;
/*综合使用方法*/
background: red url("image/a.jpg") 200px 10px no-repeat;
}
/*ul{*/
/* background: #a0a0a0;*/
/*}*/
ul:li{
height: 30px;
list-style: none;
text-indent: 1em;
background-image: url("image/a.jpg");
background-repeat: no-repeat;
/*单独指定位置*/
background-position: 100px 10 px;
}
</style>
然后打开网页对应的布局,如果想调整标题处或者表格处的图片的位置,可以选中对应的行:
然后可以根据滚轮调整大小,并且能够看到调整后的位置,找到合适的位置后,可以将对应的位置写道html文件中即可
3.7渐变
网址:https://www.grablent.com
径向渐变、圆形渐变
直接复制网站的内容即可
四、盒子模型
4.1什么是盒子模型
- margin:外边距
- padding:内边距
- border:边框
4.2、边框
border:粗细 样式 颜色
- 边框的粗细
- 边框的样式(solid实现、dashed虚线)
- 边框的颜色
border :1px solid red;
4.3内外边距
margin-left/right/top/bottom–>表示四边,可分别设置,也可以同时设置如下:
margin:0 0 0 0/*分别表示上、右、下、左;从上开始顺时针*/
/*例1:居中*/
margin:0 auto /*auto表示左右自动,有两个元素,则第一个元素代表上下外边距,第二个元素代表作右外边距*/
/*例2:*/
margin:4px/*表示上、右、下、左都为4px*/
/*例3*/
margin:10px 20px 30px/*表示上为10px,左右为20px,下为30px*/
内边距就把margin改成padding
盒子的计算方式:
margin+border+padding+内容的大小
注意:想让元素居中,前提得是:元素是一个块元素
4.4 圆角边框----border-radius
如果是4个参数,分别设置的是:左上、右上、右下、左下
举例1:
<style>
div{
width: 100px;
height: 100px;
border: 10px solid red;
border-radius: 0px 50px 0px 0;
}
</style>
</head>
<body>
<div></div>
</body>
样式:
五、浮动
5.1块级元素&行内元素
块级元素:独占一行 h1~h6 、p、div、 列表…
行内元素:不独占一行 span、a、img、strong
注: 行内元素可以包含在块级元素中,反之则不可以。
5.2 display(重要)
1.block:块元素
2.inline:行内元素
3.inline-block:是块元素,但是可以内联,在一行
这也是一种实现行内元素排列的方式,但是我们很多情况用float
4.none:消失
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--block 块元素
inline 行内元素
inline-block 是块元素,但是可以内联 ,在一行
-->
<style>
div{
width: 100px;
height: 100px;
border: 1px solid red;
display: inline-block;
}
span{
width: 100px;
height: 100px;
border: 1px solid red;
display: inline-block;
}
</style>
</head>
<body>
<div>div块元素</div>
<span>span行内元素</span>
</body>
</html>
5.3 float:left/right左右浮动
5.4overflow及父级边框塌陷问题
clear right:右侧不允许有浮动元素
clear left:左侧不允许有浮动元素
clear both:两侧不允许有浮动元素
clear none:允许浮动
解决方法:
1、增加父级元素高度
2、增加一个空的div标签,清除浮动
<style>
div{
margin: 10px;
padding: 5px;
}
#father{
border: 1px #000 solid;
}
.clear{
clear: both;
margin: 0;
padding: 0;
}
.first{
border: 1px #F00 dashed;
display: inline-block;
float: left;
}
.second{
border: 1px #00F dashed;
display: inline-block;
float: right;
}
.third{
border: 1px #666 dashed;
line-height: 12px;
display: inline-block;
float: right;
}
</style>
</head>
<body>
<div id="father">
<div class="first">第一个盒子</div>
<div class="second">第二个盒子</div>
<div class="third">第三个盒子</div>
<div class="clear"></div>
</div>
</body>
3、overflow:在父级元素中添加“overflow:hidden”
overflow应用举例:超出相应的高度,就产生一个滚动条
<style>
div{
margin: 10px;
padding: 5px;
}
#father{
border: 1px #000 solid;
overflow: hidden;
}
.clear{
clear: both;
margin: 0;
padding: 0;
}
.first{
border: 1px #F00 dashed;
display: inline-block;
float: left;
}
.second{
border: 1px #00F dashed;
display: inline-block;
float: right;
}
.third{
border: 1px #666 dashed;
line-height: 12px;
display: inline-block;
float: right;
}
</style>
</head>
<body>
<div id="father">
<div class="first">第一个盒子</div>
<div class="second">第二个盒子</div>
<div class="third">第三个盒子</div>
<div class="clear"></div>
</div>
</body>
4、在父类添加一个伪类:after(推荐)
<style>
div{
margin: 10px;
padding: 5px;
}
#father{
border: 1px #000 solid;
}
#father:after{
content:'';
display:block;
clear:both;
}
.clear{
clear: both;
margin: 0;
padding: 0;
}
.first{
border: 1px #F00 dashed;
display: inline-block;
float: left;
}
.second{
border: 1px #00F dashed;
display: inline-block;
float: right;
}
.third{
border: 1px #666 dashed;
line-height: 12px;
display: inline-block;
float: right;
}
</style>
</head>
<body>
<div id="father">
<div class="first">第一个盒子</div>
<div class="second">第二个盒子</div>
<div class="third">第三个盒子</div>
<div class="clear"></div>
</div>
</body>
六、定位
6.1 相对定位
相对定位:positon:relative;
相对于原来的位置,进行指定的偏移,相对定位的话,它仍然在标准文档流中,原来的位置会被保留
top:-20px;
left:20px;
bottom:-10px;
right:20px;
6.2绝对定位-absolute
定位:基于xxx定位,上下左右~
1、没有父级元素定位的前提下,相对于浏览器定位
2、假设父级元素存在定位,我们通常会相对于父级元素进行偏移
3、在父级元素范围内移动
总结:相对一父级或浏览器的位置,进行指定的偏移,绝对定位的话,它不在标准文档流中,原来的位置不会被保留
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
margin: 10px;
padding: 5px;
font-size: 12px;
line-height: 25px;
}
#father{
border: 1px solid #666;
padding: 0;
position: relative;
}
#first{
background-color: #a13d30;
border: 1px dashed #b27530;
}
#second{
background-color: green;
border: 1px dashed #0ece4f;
position: absolute;
right:30px;
top:30px
}
#third{
background-color: red;
border: 1px dashed #ff1b87;
}
</style>
</head>
<body>
<div id = "father">
<div id="first">第一个盒子</div>
<div id="second">第二个盒子</div>
<div id="third">第三个盒子</div>
</div>
</body>
</html>
6.3 固定定位-fixed
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
height: 1000px;
}
div:nth-of-type(1){/*绝对定位:没有相对的父级元素,所以相对于浏览器*/
width: 100px;
height: 100px;
background:red;
position: absolute;
right: 0;
bottom: 0;
}
div:nth-of-type(2){
width: 50px;
height: 50px;
background: yellow;
position: fixed;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>
</html>
6.4 z-index
图层~
z-index:默认是0,最高无限~999