Cascading Style Sheet: 层叠样式表 , 作用: 美化页面
如何在html页面中添加样式代码(引入方式) , 总共有三种方式:
内联样式: 在标签的style属性中添加样式代码,不能复用内部样式: 在head标签里面添加style标签,在标签体内写样式代码,仅可以在当前页面复用,不可以多页面复用外部样式: 在单独的css样式文件中写样式代码, 在html页面通过link标签引入,可以实现多页面复用,并且可以将html代码和css样式代码分离开 三种引入方式的优先级: 内联优先级最高,内部和外部优先级一样,后执行的覆盖先执行的id选择器
格式: #id{样式代码}选取页面中对应id的元素页面当中的元素要保证id是唯一的当需要选择页面中的某一个元素时使用类选择器
格式: .class{样式代码}选取页面中某一类元素给需要选取的元素添加相同的class属性分组选择器
将多个选择器合并成一个选择器格式: #id,.class,div{样式代码}任意元素选择器
格式: *{样式代码}选取页面中所有的元素以上选择器的代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> <!--id选择器 --> #d2{ color:red; } <!--类选择器 --> .c1{ color:yellow; } /*分组选择器 */ #d2,.c1{ background-color:green; } /*任意元素选择器 */ *{ /*边框:1px粗细solid实线样式red颜色 */ border:1px solid red; } </style> </head> <body> <div>div1</div> <div id="d2">div2</div> <div class="c1">div3</div> <span>s1</span> <span>s2</span> <span class="c1">s3</span> <h3 class="c1">h3</h3> </body> </html>属性选择器
格式: 标签名[属性名=‘值’]{样式代码}通过元素的属性去选择元素子孙后代选择器
格式: body div span{样式代码}匹配body里面的div里面的所有span(包括后代)子元素选择器
格式: body>div>span{样式代码}匹配body里面的div里面的span子元素伪类选择器
该选择器选中的是元素的状态(未访问/访问过/悬停/点击)格式: a:visited/link/hover/active{样式代码} 以上四种选择器代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> /*属性选择器*/ input[type='text']{ background-color:green; } /*子孙后代选择器 */ /*body div div div span{ color:red;' }*/ /*子元素选择器*/ body>div>span{ color:red; } body>span{ background-color:green; } /*访问过 */ a:visited{ color:red; } /*未访问过*/ a:link{ color:green; } /* 悬停 */ a:hover{ color:purple; } /* 点击 */ a:active{ color:yellow; } </style> </head> <body> <a href="../imges/a.jpg">超链接1</a> <a href="../imges/b.jpg">超链接2</a> <a href="../imges/c.jpg">超链接3</a> <a href="../imges/d.jpg">超链接4</a><br> <span>s1</span> <div> <span>s2</span> <div> <span>s3</span> </div> <div> <span>s4</span> <div> <span>s5</span> </div> </div> </div> <input type="text"> <input type="password"> </body> </html>文本修饰 text-decoration: overline上划线/underline下划线/line-through删除线/none去掉下划线
对齐方式 text-align:left/right/center
文本颜色 color:red;
行高 可以实现垂直居中 line-height:20px;
文本阴影 text-shadow: 颜色 x偏移值 y偏移值 浓度(值越大越模糊)
字体大小 默认大小16px font-size:20px;
字体设置 font-family: xxxx,xxx,xxx,xxx; font: 20px xxx,xxx,xxx;
斜体 font-style:italic;
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> div{ widrh:150px; height:50px; border:1px solid red; <!--文本修饰overline上划线,underline下划线,line-through删除线 --> text-decoration:overline; /*水平对齐方式 left,center,right*/ text-align:center; line-height:50px;/*可以实现垂直居中 */ /*文本阴影 x偏移值 y偏移值 浓度 */ text-shadow:green -10px 10px 1px; /*字体大小,默认为16*/ font-size:30px; /*字体加粗*/ font-weight:bold; color:red; } a{ text-decoration:none;/*none去掉下划线*/ } h1{ font-weight:normal;/*去掉加粗 */ /*字体设置*/ /* font-family: cursive; */ /*字体大小和字体设置 */ font:10px cursive; /*斜体 */ font-style:italic; } </style> </head> <body> <h1>这是h1</h1> <a href="#">超链接</a> <div>文本测试</div> </body> </html>盒子模型之宽高width/height
赋值方式:1. 像素 2. 上级元素百分比行内元素不能修改宽高.盒子模型之外边距margin
什么是外边距: 元素距上级元素或相邻兄弟元素的距离称为外边距. 赋值方式: /* 单独某个方向添加外边距 */ /* margin-left: 50px; margin-bottom: 50px; margin-top: 50px; margin-right: 20px; */ /* 给四个方向添加外边距 */ /* margin: 20px; */ /* 上下和左右赋值 */ /* margin: 20px 40px; */ /* 元素居中 */ /* margin: 0 auto; */ /* 上右下左赋值 顺时针*/ margin: 10px 20px 30px 40px; ``` ## 查看页面元素样式 1. 在元素上面右键检查 2. 在页面中按f12出现和右键检查一样的工具, 点击拾取箭头 然后将鼠标放在元素上面点击