1.什么是
1.1什么是CSS
Casading Style Sheet 层叠级联样式表
CSS:表现(美化网页)
字体、颜色、边距、宽度、高度、背景图片、网页定位、网页浮动.....
1.2发展史
CSS1.0
CSS2.0 DIV(块) + CSS,HTML与CSS结构分离的思想,网页变得简单
CSS2.1 浮动,定位
CSS3.0 圆角,阴影,动画... 浏览器兼容性~
1.3css基础
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- 规范,<style> 可以编写css代码 语法: 选择器{ 声明1; 声明2; } --> <link rel="stylesheet" href="css/style.css"> </head> <body> <h1>标题</h1> </body> </html>
css的优势:
-
内容和表现分离
-
网页结构表现统一,可以实现复用
-
样式十分的丰富
-
建议使用独立于html的css文件
-
利于SEO,容易被搜索引擎收录!
1.4 CSS的三种导入方式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- 内部样式--> <style> h1{ color: aquamarine; } </style> <!-- 外部样式--> <link rel="stylesheet" href="css/style.css"> </head> <body> <!--优先级:就近原则!--> <!--行内样式:在标签元素中,编写一个style属性,编写样式即可--> <h1>标题</h1> </body> </html>
拓展:外部样式两种写法
-
链接式
<!-- 外部样式--> <link rel="stylesheet" href="css/style.css">
-
导入式(不推荐使用)
<style> @import url("css/style.css"); </style>
2.选择器
作用:选择界面上的某一个或者某一类元素
2.1 基本选择器
-
标签选择器:选择一类标签
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>标签选择器</title> <style> /*标签选择器,会选择到页面上所有的这个标签的元素*/ h1{ color: #38f506; } p{ font-size: 80px; } </style> </head> <body> <h1>H标签</h1> <h1>H标签</h1> <h1>H标签</h1> <p>P标签</p> </body> </html>
-
类选择器 class:选择所有calss属性一致的标签
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>类选择器</title> <style> /*类选择器的格式: .class的名称{} 好处:可以多个标签归位,同一个class可以复用 */ .title1{ color: #38f506; } .title2{ color: aqua; } .title3{ color: blueviolet; } </style> </head> <body> <h1 class="title1">标题1</h1> <h1 class="title2">标题2</h1> <h1 class="title3">标题3</h1> </body> </html>
-
id 选择器:全局唯一!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>id选择器</title> <style> /*id选择器:id必须保证全局唯一 不遵循就近原则 优先级:id选择器>class选择器>标签选择器 */ #title1{ color: #38f506; } #title2{ color: blueviolet; } #title3{ color: blue; } #title4{ color: aqua; } #title5{ color: red; } </style> </head> <body> <h1 id="title1">标题1</h1> <h1 id="title2">标题2</h1> <h1 id="title3">标题3</h1> <h1 id="title4"标题4</h1> <h1 id="title5">标题5</h1> </body> </html>
优先级:id>class>标签
2.2 层次选择器
-
后代选择器:在某个元素后面的全部改变
/* 后代选择器*/ body p{ background: #38f506; }
-
子选择器
/* 子选择器*/ body>p{ background: aqua; }
-
相邻兄弟选择器
/* 相邻兄弟选择器(只有向下相邻的同代选择器)*/ .active + p{ background: blueviolet; }
-
通用选择器
/* 通用选择器(当前选中元素的向下的所有兄弟元素)*/ .active~p{ background: blueviolet; }
2.3 结构伪类选择器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>层次选择器</title> <style> /*ul的第一个子元素*/ ul li:first-child{ background: #38f506; } /*ul的最后一个子元素*/ ul li:last-child{ background: aqua; } /*选中p1:定位到父元素,选择当前的第一个元素*/ p:nth-child(1){ background: blue; } /*选中父元素下的p元素的第二个*/ p:nth-of-type(2){ background: chocolate; } </style> </head> <body> <p>p1</p> <p>p2</p> <p>p3</p> <ul> <li>l1</li> <li>l2</li> <li>l3</li> </ul> </body> </html>
2.4 属性选择器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>属性选择器</title> <style> .demo a{ float: left; display: block; height: 50px; width: 50px; border-radius: 10px; background: #001ff1; text-align: center; color: gainsboro; text-decoration: none; margin-right: 10px; font: bold 20px/50px Arial; } /*属性名,属性名 = 属性值(正则) = 绝对等于 *= 包含这个元素 ^= 以这个开头 $= 以这个结尾 */ /*存在id属性的元素 a[]{} */ /*a[id]{*/ /* background: yellow;*/ /*}*/ /*id=first的元素*/ /*a[id=first]{*/ /* background: #38f506;*/ /*}*/ /*class中由links的元素*/ /*a[class*="links"]{*/ /* background: yellow;*/ /*}*/ /* 选中href中以http开头的元素*/ /* a[href^=http]{*/ /* background: yellow;*/ /* }*/ /*选中href中以doc结尾的元素*/ a[href$=doc]{ background: yellow; } </style> </head> <body> <p class="demo"> <a href="http://www.baidu.com" class="links item first" id="first">1</a> <a href="" 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 为什么要美化网页
-
有效的传递页面信息
-
美化网页,吸引用户
-
凸显页面的主题
-
提高用户体验
span标签:重点要突出的字,使用span套起来
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>span</title> <style> #title1{ font-size: 50px; } </style> </head> <body> 欢迎学习 <span id="title1">Java</span> </body> </html>
3.2 字体样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>字体样式</title> <style> body{ font-family: "Agency FB",幼圆;/*字体*/ color: #187cc0;/*字体颜色*/ } h1{ font-size: 50px;/*字体大小*/ } .p1{ font-weight: bold;/*字体粗细*/ } </style> </head> <body> <h1>故事介绍</h1> <p class="p1"> 完美世界主要讲的是在仙古时代的时候,一些修仙者发现毁灭仙域就可以使自己达到永生境界,所以这些修士为了使自己能够永生便掀起了灭世危机。主角石昊这时便应运而生,走上了拯救仙域的道路。 </p> <p> 《完美世界》是一部由起点白金作家辰东写的小说,辰东本人被广大书迷称作为燕京才子,同时这一部小说在全网的热度也是非常高的,可以说是网文玄幻类型中的巅峰之作。 </p> <p>She walks in beauty, like the night 她走在美的光彩中,象夜晚, </p> <p>Of cloudless climes and starry skies; 皎洁无云而且繁星漫天;</p> <p>And all that’s best of dark and bright;黑夜与白天最美妙的色彩;</p> <p>Meet in her aspect and her eyes. 都在她的面容和目光里显现。</p> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>字体样式</title> <style> p{ font: oblique bolder 16px "Arial Rounded MT Bold","幼圆"; } </style> </head> <body> <p>She walks in beauty, like the night 她走在美的光彩中,象夜晚, </p> <p>Of cloudless climes and starry skies; 皎洁无云而且繁星漫天;</p> <p>And all that’s best of dark and bright;黑夜与白天最美妙的色彩;</p> <p>Meet in her aspect and her eyes. 都在她的面容和目光里显现。</p> </body> </html>
3.3 文本样式
-
颜色
-
文本对齐方式
-
首行缩进
-
行高
-
下划线
-
装饰
-
文本图片水平对齐
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>字体样式</title> <style> h1{ color: rgb(2, 151, 253,0.9);/*颜色 RGB:0~F RGBA:0~1*/ line-height: 200px;/*行高*/ text-align: center;/*文字水平居中*/ } p{ text-align: center; text-indent: 2em;/*首行缩进*/ } .p2{ background: #187cc0; height: 40px;/*块的高度*/ line-height: 40px;/*行高*/ /*行高和块的高度一致就可以在块中上下居中*/ } .p3{ text-decoration: underline;/*underline:下划线 line-through:中划线 overline:上划线*/ } </style> </head> <body> <h1>最美的情诗</h1> <p class="p1">She walks in beauty, like the night 她走在美的光彩中,象夜晚, </p> <p class="p2">Of cloudless climes and starry skies; 皎洁无云而且繁星漫天;</p> <p class="p3">And all that’s best of dark and bright;黑夜与白天最美妙的色彩;</p> <p class="p4">Meet in her aspect and her eyes. 都在她的面容和目光里显现。</p> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>文字样式</title> <!--图片和文字水平对齐 参照物:a,b{} --> <style> img,span{ vertical-align: middle; } </style> </head> <body> <p> <img src="images/a.png" alt=""> <span>唯美情诗</span> </p> </body> </html>
3.4 超链接伪类
<!DOCTYPE html> <html lang="en" xmlns:margin="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>超链接伪类</title> <style> /*默认的颜色*/ a{ text-decoration: none; color: black; } /*鼠标悬浮的颜色(重点)*/ a:hover{ color: orange; font-size: 20px; } /*鼠标按住未释放状态*/ a:active{ color: #38f506; } /*阴影 阴影颜色 水平偏移 垂直偏移 阴影半径 */ #price{ text-shadow: aqua 5px 5px 2px; } </style> </head> <body> <a href="#"> <img src="image/a.png" alt=""> </a> <p> <a href="#">唯美图片</a> </p> <p> 作者: <a href="#">Pan</a> </p> <p id="price"> ¥5 </p> </body> </html>
3.5 列表
<!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: #c2c2c5; } .title{ font-size: 18px; font-weight: bold; text-indent: 1em; line-height: 35px; background: #38f506; } /*list—style: none:去掉圆点 circle:空心圆 decimal:数字 square:正方形 */ /*ul{*/ /* background: #c2c2c5;*/ /*}*/ ul li{ height: 30px; list-style: none; text-indent: 1em; } a{ text-decoration: none; font-size: 14px; font-family:幼圆; color: #000; } a:hover{ color: orange; font-size: 18px; text-decoration: underline; }
3.6 背景
网站:Grabient
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>渐变</title> <!-- 径向渐变,圆形渐变--> <style> body{ background-color: #4158D0; background-image: linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%); } </style> </head> <body> </body> </html>
4.盒子模型
4.1 什么是盒子模型
margin:外边距
padding:内边距
border:边框
4.2 边框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盒子模型</title> <style> /*body总有一个默认的外边距*/ /*h1,ul,li,a,body{*/ /* margin: 0;*/ /* padding: 0;*/ /* text-decoration: none;*/ /*}*/ #box{ /*border: 粗细 样式 颜色;*/ width: 300px; border: 1px solid red; } h2{ font-size: 16px; background: #84a6ef; line-height: 30px; margin: 0; color: wheat; } form{ background: aqua; } form div:nth-of-type(1) input{ border: 3px solid black; } form div:nth-of-type(2) input{ border: 2px dashed #c3c3d2; } form div:nth-of-type(3) input{ border: 2px dashed #84a6ef; } </style> </head> <body> <div id="box"> <h2>登录</h2> <form action="#"> <div> <span>用户名:</span> <input type="text"> </div> <div> <span>密码:</span> <input type="text"> </div> <div> <span>验证码:</span> <input type="text"> </div> </form> </div> </body> </html>
4.3 内外边距
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盒子模型</title> <style> #box{ /*border: 粗细 样式 颜色;*/ width: 300px; border: 1px solid red; margin: 0 auto; } /*顺时针旋转 上右下左 */ h2{ font-size: 16px; background: #84a6ef; line-height: 30px; margin: 0; color: wheat; } form{ background: aqua; } input{ border: 1px solid black; } div:nth-of-type(1){ padding: 10px; } </style> </head> <body> <div id="box"> <h2>登录</h2> <form action="#"> <div> <span>用户名:</span> <input type="text"> </div> <div> <span>密码:</span> <input type="text"> </div> <div> <span>验证码:</span> <input type="text"> </div> </form> </div> </body> </html>
4.4 圆角边框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>边框圆角</title> <style> div{ /*顺时针方向*/ width: 100px; height: 100px; border: 10px solid red; border-radius: 50px; } </style> </head> <body> <div></div> </body> </html>
4.5 阴影
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>阴影</title> <style> /*margin: 0 auto; 要求:块元素,块元素有固定的宽度 */ img{ /*margin: 0 auto;*/ width: 100px; height: 100px; border: 10px solid red; box-shadow: 10px 10px 100px #e7aa15; border-radius: 200px; } </style> </head> <body> <div style="width: 1200px;display: block;text-align: center"> <img src="image/a.png" alt=""> </div> </body> </html>
5. 浮动
-
块级元素
h1~h6 p div 列表...
-
行内元素:不独占一行
span a img strong...
-
行内元素可以被包含在块级元素中,反之则不可以
5.1 display
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>display</title> <style> div{ width: 100px; height: 100px; border: 1px solid red; display: inline; } /* block:块元素 inline-block:块元素,可以在一行 inline:行内元素 none:消失 */ span{ width: 100px; height: 100px; border: 1px solid red; display: inline-block; } </style> </head> <body> <div>div块元素</div> <span>span行内元素</span> </body> </html>
5.2 float
右浮动:float:right 左浮动:float:left
5.3 父级边框塌陷
右侧不允许有浮动元素:clear:right 左侧不允许有浮动元素:clear:left 两侧都不允许有浮动元素:clear:both clear:none
解决方案
-
增加父级元素高度
-
增加一个空的div标签,清除浮动
<div class = "clear"></div> <style> .clear{ clear:both; margin:0; padding:0; } </style>
-
overflow
在父级元素中增加一个 overflow:hidden;
-
父类添加一个伪类:after
#father:after{ content:''; display:block; clear:both; }
小结:
-
浮动元素后面加空div
简单,代码中尽量避免空div
-
设置父元素的高度
简单,元素假设有了固定的高度,就会被限制
-
overflow
简单,下拉的一些场景避免使用
-
父类添加一个伪类:after
推荐使用
5.4 对比
-
display
-
方向不可控制
-
-
float
-
浮动起来会脱离标准文档流,所以要解决父级边框塌陷的问题
-
6. 定位
6.1 相对定位
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>相对定位</title> <style> div{ margin: 10px; padding: 5px; font-size: 12px; line-height: 25px; } #father{ border: 1px solid #84a6ef; } #first{ background: #001ff1; border: 1px dashed #f52507; position: relative;/*相对定位:上下左右*/ top: -20px; left: 20px; } #second{ background: #187cc0; border: 1px dashed #c1f507; } #third{ background: #C850C0; border: 1px dashed #f8060f; position: relative; bottom: -10px; right: 20px; } </style> </head> <body> <div id="father"> <div id="first">第一个盒子</div> <div id="second">第二个盒子</div> <div id="third">第三个盒子</div> </div> </body> </html>
相对定位:position:relative;
相对于原来的位置进行指定偏移,相对定位的话。他仍然在标准文档流中,原来的位置会被保留
-
top
-
left
-
bottom
-
right
练习:方块定位
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> div{ width: 300px; height: 300px; padding: 10px; border: 2px solid red; } a{ width: 100px; height: 100px; background: #C850C0; text-decoration: none; display: block; line-height: 100px; text-align: center; color: white; } a:hover{ background: #001ff1; } .a2{ position: relative; right: -200px; top: -100px; } .a4{ position: relative; right:-200px; top: -100px; } .a5{ position: relative; right: -100px; top: -300px; } </style> </head> <body> <div id="box"> <a class="a1">方块1</a> <a class="a2">方块2</a> <a class="a3">方块3</a> <a class="a4">方块4</a> <a class="a5">方块5</a> </div> </body> </html>
6.2 绝对定位
-
没有父级元素定位的前提下,相对于浏览器定位
-
假设父级元素存在定位,我们通常会相对于父级元素进行偏移
-
在父级元素的范围内移动
-
相对于父级或浏览器的位置进行指定偏移,绝对定位的话。它不在在标准文档流中,原来的位置不会被保留
-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>默认</title> <style> div{ margin: 10px; padding: 5px; font-size: 12px; line-height: 25px; } #father{ border: 1px solid #84a6ef; padding: 0; position: relative; } #first{ background: #001ff1; border: 1px dashed #f52507; } #second{ background: #187cc0; border: 1px dashed #c1f507; position: absolute; right: 30px; } #third{ background: #C850C0; border: 1px dashed #f8060f; } </style> </head> <body> <div id="father"> <div id="first">第一个盒子</div> <div id="second">第二个盒子</div> <div id="third">第三个盒子</div> </div> </body> </html>
6.3 固定定位
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>固定定位</title> <style> body{ height: 1000px; } div:nth-of-type(1){ width: 100px; height: 100px; background: aqua; /*绝对定位,相对于浏览器*/ 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
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>z-index</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <div id="content"> <ul> <li><img src="image/a.png" alt=""></li> <li class="tipText">图片</li> <li class="tipBg"></li> <li>时间:2022.9.28</li> <li>地点:中国四川</li> </ul> </div> </body> </html>
-
CSS
#content{ width: 400px; padding: 0px; margin: 0px; overflow: hidden; font-size: 12px; line-height: 25px; border: 1px solid black; } ul,li{ padding: 0px; margin: 0px; list-style: none; } /*父级元素相对定位*/ #content ul{ position: relative; } .tipText,.tipBg{ position: absolute; width: 400px; height: 25px; top: 380px; } .tipText{ color: #001ff1; z-index: 1; font-size: 25px; } .tipBg{ background: #38f506; opacity: 0.6;/*背景透明度*/ /*filter: alpha(opacity=50);*/ }