CSS快速入门教程

    技术2022-07-11  123

    本文主要内容是关于CSS3.0的使用,内容涵盖有选择器,网页美化,盒子模型,以及浮动定位等知识。如果你能坚持看完这篇文章,相信你一定会有所收获。 在看这篇文章之间,需要有HTML的知识基础。在此提供HTML文章,如有需要,可供查阅HTML教程

    初级入门

    CSS概述

    版本:CSS3.0

    CSS(Cascading Style Sheet层叠级联样式表)

    CSS选择器

    美化网页(文字,阴影,超链接,列表,渐变)

    盒子模型

    浮动

    定位

    网页动画(特效效果)

    CSS的优势:

    内容和表现的分离网页结构表现统一,可以实现复用样式丰富使用独立与HTML的CSS文件利用SEO,容易被收索引擎收录

    CSS导入

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>我的网页</title> </head> <!--导入方式的优先级:行内样式>(内部样式和外部样式采用就近原则)--> <!--内部样式: <style>可以编写css代码(不规范) 语法: 选择器{ 声明1; 声明2; 声明3; ... } --> <style> h1{ color:red; } </style> <!--外部样式: 规范的做法: 将css文件关联到html中 <link rel="stylesheet" href="css文件"> 其中css文件内容: h3{ color:green; } --> <!--外部样式导入方法1:--> <link rel="stylesheet" href="css/style.css"> <!--外部样式导入方法2--> <style> @import url(css/style.css); </style> <body> <!--内部样式--> <h1>标题1</h1> <!--行内样式 在标签元素中,编写一个style属性,从而编写样式--> <h2 style="color:black">标题2</h2> <!--外部样式--> <h3>标题3</h3> </body> </html>

    选择器

    基本选择器

    作用:选择页面中的某一个或者某一类元素

    基本选择器优先级(id选择器>class选择器>标签选择器)

    标签选择器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>标签选择器</title> <style> /*标签选择器会选择到页面上所有的这个标签元素*/ h1{ color: rgba(84, 177, 147, 0.2); } </style> </head> <body> <h1>标签1</h1> </body> </html>

    类选择器 class

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>类选择器</title> <style> /*类选择器的格式: .class{} 好处:可以多个标签归类,进行复用 */ .key1{ color: rgba(33, 167, 106, 0.13); } .key2{ color: #bf2c1b; } </style> </head> <body> <h1 class="key1">标题1</h1> <h1 class="key2">标题2</h1> </body> </html>

    id选择器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>ID选择器</title> <style> /* id选择器的格式: #id{} id必须保证全局只有一个; */ #key1{ color: #0c804c; } </style> </head> <body> <h1 id="key1">标题1</h1> </body> </html>
    层次选择器

    后代选择器:在某个元素的后面

    /*后代选择器*/ body p{ background:red; }

    子选择器:某个元素的后一代

    /*子选择器*/ body>p{ background:red; }

    相邻兄弟选择器:同辈,只有一个(相邻向下的那个)

    /*相邻兄弟选择器*/ .key +p { background:red; }

    通用选择器:同辈,当前选中元素向下的所有兄弟元素

    /*通用选择器*/ .key~p{ background:red; }

    测试代码:

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>层次选择器</title> <style> /*后代选择器*/ body p{ background:red; } /*子选择器*/ body>p{ background:red; } /*相邻兄弟选择器*/ .key +p { background:red; } /*通用选择器*/ .key~p{ background:red; } </style> </head> <body> <p>p1</p> <p class="key">p2</p> <p>p3</p> <ul> <li> <p>p4</p> </li> <li> <p>p5</p> </li> <li> <p>p6</p> </li> </ul> </body> </html>
    结构伪类选择器
    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>结构伪类选择器</title> <style> /*ul的第一个子元素,且该元素类型是li才生效*/ ul li:first-child{ background:red; } /*ul的最后一个子元素,且该元素类型是li才生效*/ ul li:last-child{ background:blue; } /*选择当前的p元素的父元素第X个子元素,并且是当前元素才生效*/ p:nth-child(1){ background:yellow; } /*选择当前的p元素的父元素第X个p类型元素*/ p:nth-of-type(2){ background:purple; } </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>
    属性选择器
    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>属性选择器</title> <style> /* 标签[属性名=属性值(精确匹配)]{} */ a[id="first"]{ background:red; } /* 标签[属性名*=属性值(包含属性值的元素)]{} */ a[class*="345"]{ background:yellow; } /* 标签[属性名^=属性值(以该属性值开头的元素)]{} */ a[href*="http"]{ background:purple; } /* 标签[属性名$=属性值(以该属性值结尾的元素)]{} */ a[href$="cn"]{ background:blue; } </style> </head> <body> <p class="key"> <a href="../基本选择器/标签选择器.html" class="123" id="first">1</a> <a href="../基本选择器/ID选择器.html" class="12345" target="_blank" title="test">2</a> <a href="../层次选择器/层次选择器.html" class="2345">3</a> <a href="https://www.baidu.com" >4</a> <a href="https://www.baidu.cn" >5</a> </p> </body> </html>

    美化网页

    字体样式

    span标签:重点要突出的字.可以使用span标签套起来

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>span标签</title> </head> <style> #key{ font-size:30px; } </style> <body> hello <span id ="key">world!</span> </body> </html> 字体,大小,粗细,颜色,样式,行高 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>字体样式</title> <!-- font-family:字体 font-size:字体大小 font-weight:粗细 color:字体颜色 --> <style> p{ color: red; font-family: Arial, 楷体; font-size: 20px; font-weight: lighter; } /*样式 粗细 大小 行高 字体*/ h1{ font: oblique lighter 10px/10px 楷体; } </style> </head> <body> <h1>故事简介</h1> <p>If you were a teardrop;In my eye,For fear of losing you,I would never cry.And if the golden sun,Should cease to shine its light,Just one smile from you,Would make my whole world bright.</p> </body> </html>

    文本样式

    颜色,文本对齐方式,首行缩进,行高,装饰,文本图像居中对齐 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>文本样式</title> <style> /*颜色: 使用方式:单词,RGB和RGBA(A表示透明读0~1) 排版: text-align:(center,left,right) 首行缩进: text-indent:使用em单位 行高: line-height(当行的高度和块的高度一致的时候,就可以实现上下居中) 装饰: text-decoration:(underline,line-through,overline); */ h1{ color:rgba(0,255,255,0.8); text-align: center ; text-decoration:underline; } h2{ text-indent:2em; background:red; line-height: 100px; } /*垂直居中对齐(图片和文字) 要使用参照物(两个对象) center是水平居中,middle是垂直居中*/ img,span{ vertical-align:middle; } /*a标签去除下划线*/ a{ text-decoration: none; } </style> </head> <body> <h1>故事简介</h1> <h2>hello world</h2> <p> <img src="..\..\resources\image\1.png" alt="loading.."> <span> If you were a teardrop;In my eye,For fear of losing you,I would never cry.And if the golden sun,Should cease to shine its light,Just one smile from you,Would make my whole world bright.</span> </p> <a href="https://www.baidu.com" target="_blank">百度</a> </body> </html>

    超链接伪类和文本阴影

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>超链接伪类</title> <style> a{ text-decoration: none; } /*未访问的链接的状态*/ a:link{ color:yellow; } /*鼠标悬停的状态*/ a:hover{ color:red; } /*鼠标按住未释放的状态*/ a:active{ color:blue; } /*被选择后的状态*/ a:visited{ color:green; } /*text-shadow:阴影颜色水平位移,垂直位移,阴影长度*/ #price{ text-shadow: gray 10px 10px 1px; } </style> </head> <body> <a href="#"> <img src="../../resources/image/2.png" alt="loading.."> </a> <p> <a href="#">码出高效</a> </p> <p> <a href="#">杨冠宝</a> </p> <p id="price"> $99 </p> </body> </html>

    列表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>列表样式</title> <style> /*无序列表项目符号样式 list-style: 去掉原点none 数字编号demical 空心原点circle 正方形 square */ ul li{ list-style: none; } </style> </head> <body> <p>列表</p> <ul> <li>1.1</li> <li>1.2</li> <li>1.3</li> <li>1.4</li> </ul> </body> </html>

    背景和渐变

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>背景</title> <style> div{ width:1000px; height:800px; border:1px solid red; } .div1{ /*背景图片(默认为平铺方式) background-image: url(); */ background-image: url(../../resources/image/1.png); } .div2{ /*平铺方式 background-image: 不平铺:no-repeat 平铺:repeat 水平:repeat-x 竖直:repeat-y */ background-image: url(../../resources/image/1.png); background-repeat:no-repeat; /*图片位置 background-positon:x,y */ background-position: 100px 100px; } /*背景图片综合应用: background: 颜色 图片 图片x,y位置 平铺方式 */ .div3{ background: red url("../../resources/image/1.png") 200px 100px no-repeat; } </style> </head> <body> <div class="div1"></div> <div class="div2"></div> <div class="div3"></div> </body> </html>

    渐变调色的网站:https://www.grabient.com

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>渐变</title> <style> body{ /*渐变 liner-gradient(角度,当前颜色,最后颜色) */ background-color: #FAACA8; background-image: linear-gradient(19deg, #FAACA8 0%, #DDD6F3 100%); } </style> </head> <body> </body> </html>

    盒子模型

    组成

    margin:外边距border:边距–边框的粗细样式,颜色padding:内边距

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盒子模型</title> <style> /*边框 border:粗细,样式(实线solid,虚线dashed),颜色 */ /*外边距和内边距 margin/padding:(可以使用auto自动调整) 四个参数:top right button left 两个参数:top,button right,left 一个参数:top,button,right,left */ #box{ height: 140px; width:300px; margin:0px 100px 50px 200px; padding:1px ; border: 1px solid black; } div:nth-of-type(1) input{ border: 3px solid gray; } div:nth-of-type(2) input{ border: 3px dashed burlywood; } h2{ text-align: center; line-height: 30px; } </style> </head> <body> <div id="box"> <h2>会员登录</h2> <form action="#"> <div> <span>用户名:</span> <input type="text"> </div> <div> <span>密码:</span> <input type="text"> </div> </form> </div> </body> </html>

    圆角边框和阴影

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>圆角边框和阴影</title> <style> /*border-radius: 四个参数:左上 右上 右下 左下(按顺时针方向) 两个参数:左上,右下 左下,右上 一个参数:左上,右下,左下,右上 */ /*box-shadow:阴影颜色 水平位移,垂直位移,阴影长度 */ div{ width:100px; height:100px; border:10px solid red; border-radius:100px 100px ; box-shadow: gray 10px 10px 2px; } </style> </head> <body> <div> </div> </body> </html>

    浮动

    diplay和float

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>浮动</title> <style> /*显示display block 块元素 inline 行内元素 inline-block 块元素但可以在一行 none 不显示 */ #key{ width:100px; height:100px; border:1px solid red; display: inline-block; } span{ width:100px; height:100px; border:1px solid red; display: inline-block; } /*浮动float(左右浮动)*/ .key{ border:1px solid black; width:1000px; height:500px; } .key1{ border:1px dashed red; display:inline-block; float:right; } .key2{ border:1px dashed red; display:inline-block; float:right; } .key3{ border:1px dashed red; display:inline-block; float:right; } </style> </head> <body> <div id="test">块元素</div> <span>行内元素</span> <hr/> <div class="key"> <div class="key1"><img src="..\..\resources\image\1.png" alt="loading.."></div> <div class="key2"><img src="..\..\resources\image\2.png" alt="loading.."></div> <div class="key3">浮动的文本</div> </div> </body> </html>

    父级边框塌陷问题

    增加父级元素的高度(元素设置了固定的高度,就会有限制) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>浮动</title> <style> /* 对key父级别增加足够大的width和height */ .key{ border:1px solid black; width:1000px; height:500px; } .key1{ border:1px dashed red; display:inline-block; float:right; } .key2{ border:1px dashed red; display:inline-block; float:right; } .key3{ border:1px dashed red; display:inline-block; float:right; } </style> </head> <body> <div class="key"> <div class="key1"><img src="..\..\resources\image\1.png" alt="loading.."></div> <div class="key2"><img src="..\..\resources\image\2.png" alt="loading.."></div> <div class="key3">浮动的文本</div> </div> </body> </html> 增加一个空的div标签,清除浮动() <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>浮动</title> <style> /* clear:right 右侧不允许有浮动元素 clear:left 左侧不允许有浮动元素 clear:none */ .key{ border:1px solid black; } .key1{ border:1px dashed red; display:inline-block; float:right; } .key2{ border:1px dashed red; display:inline-block; float:right; } .key3{ border:1px dashed red; display:inline-block; float:right; } .clear{ clear:both; margin: 0px; padding: 0px; } </style> </head> <body> <div class="key"> <div class="key1"><img src="..\..\resources\image\1.png" alt="loading.."></div> <div class="key2"><img src="..\..\resources\image\2.png" alt="loading.."></div> <div class="key3">浮动的文本</div> <div class="clear"></div> </div> </body> </html> overflow(在父级元素中添加一个overfloat:hidden) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>overflow</title> <style> /* overflow:hidden/scroll */ .key{ border:1px solid black; overflow:hidden } .key1{ border:1px dashed red; display:block; } .key2{ border:1px dashed red; display:block; } .key3{ border:1px dashed red; display:block; } </style> </head> <body> <div class="key"> <div class="key1"><img src="..\..\resources\image\1.png" alt="loading.."></div> <div class="key2"><img src="..\..\resources\image\2.png" alt="loading.."></div> <div class="key3">浮动的文本</div> </div> </body> </html> 父类中添加一个伪类:after <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>after</title> <style> .key{ border:1px solid black; } .key:after{ content:''; display:block; clear:both; } .key1{ border:1px dashed red; display:block; float:left; } .key2{ border:1px dashed red; display:block; float:right; } .key3{ border:1px dashed red; display:block; float:left; } </style> </head> <body> <div class="key"> <div class="key1"><img src="..\..\resources\image\1.png" alt="loading.."></div> <div class="key2"><img src="..\..\resources\image\2.png" alt="loading.."></div> <div class="key3">浮动的文本</div> </div> </body> </html>

    定位

    相对定位

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>相对定位</title> <style> /* 相对定位:相对于自己原来的位置进行偏移 positon:relative top,left,right,bottom */ div{ margin:10px; padding:5px; font-size:12px; line-height:25px; } #father{ border: 1px solid #0c804c; } #first{ border:1px dashed #bf2c1b; background-color:red; position:relative; top:-20px; } #second{ border:1px dashed #DDD6F3; background-color: #00ff19; position:relative; bottom:-20px; } #third{ border:1px dashed #FAACA8; background-color:blue; position:relative; right:-20px; } </style> </head> <body> <div id = "father"> <div id ="first">第一个盒子</div> <div id ="second">第二个盒子</div> <div id ="third">第三个盒子</div> </div> </body> </html>

    绝对定位

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>绝对定位</title> <style> /*绝对定位 position:absolute 在父级元素没有定位的前提下,相对于浏览器进行定位 在父级元素有定位的情况下,相对于父级元素进行偏移(在父级元素范围内移动) */ div{ margin:10px; padding:5px; font-size:12px; line-height:25px; } #father{ border: 1px solid #0c804c; position:relative; } #first{ border:1px dashed #bf2c1b; background-color:red; position:absolute; right:-30px; } #second{ border:1px dashed #DDD6F3; background-color: #00ff19; } #third{ border:1px dashed #FAACA8; background-color:blue; } </style> </head> <body> <div id = "father"> <div id ="first">第一个盒子</div> <div id ="second">第二个盒子</div> <div id ="third">第三个盒子</div> </div> </body> </html>

    固定定位

    <!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:#63b497; position:absolute; right:0px; bottom:0px; } /*固定定位positon:fixed */ div:nth-of-type(2){ width:50px; height:50px; background:#FAACA8; position:fixed; right:0px; bottom:0px; } </style> </head> <body> <div>绝对定位</div> <div>固定定位</div> </body> </html>

    图层z-index

    图层最低为0层次 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>z-index</title> <style> div:nth-child(1){ padding: 0px; margin:0px; overflow:hidden; font-size:12px; line-height:25px; width:400px; height:400px; border:2px solid #FAACA8; } ul,li{ padding:0px; margin:0px; list-style: none; } ul:nth-of-type(1){ position:relative; } li:nth-of-type(3),li:nth-of-type(2){ position:absolute; width:400px; height: 25px; bottom:54px; } /*z-index 改变图层的所在层数*/ li:nth-of-type(2){ color:white; z-index:1; } /*opacity 修改背景透明度 */ li:nth-of-type(3){ background:#FAACA8; opacity:0.5; } </style> </head> <body> <div> <ul> <li><img src="../../resources/image/1.png" alt="loading.."></li> <li>学习java</li> <li></li> <li>时间:2020-1-1</li> <li>地点:江苏南京</li> </ul> </div> </body> </html>

    说到最后

    附上本文章的思维导图CSS思维导图本文章也可以配合下面的视频链接(该视频是B站的UP主 遇见狂神说 录制,有兴趣的可以关注这个博主,强烈推荐)。CSS视频如果这篇文章对你有所帮助,希望你能点赞,留言,加关注哦。
    Processed: 0.011, SQL: 9