CSS3(笔记)
CSS簡介
Cascading Style Sheet層疊樣式表
CSS:表現(美化網頁)
字體,顏色,邊距,高度,寬度,背景圖片,網頁定位,網頁浮動
CSS的3種導入方式
Title <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>外部樣式兩種方法
鏈接式
html
<!--外部樣式--><link rel="stylesheet" href="css/style.css" />導入式
@import是CSS2.1特有的!
<!--導入式--><style>@import url("css/style.css");</style>選擇器
作用:選擇頁面上的某一個或者者某一類元素
基本選擇器
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> 12345678910111213141516172、類選擇器 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> 1234567891011121314151617181920212223242526273、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>優先級:id > class > 標簽
層次選擇器
1.后代選擇器 (空格)
在某個元素的后面
/*后代選擇器*/ <style> body p{background:red; } </style>2.子選擇器 (>)
一代
/*子選擇器*/ <style> body>p{background:orange; } </style>3.相鄰的兄弟選擇器(+)
同輩
/*相鄰兄弟選擇器:只有一個,相鄰(向下)*/ <style> .active+p{ background: red } </style><body><p class="active">p1<p><p>p2</p> </body>4.通用選擇器(~)
<style> /*通用兄弟選擇器,當前選中元素的向下的所有兄弟元素*/.active~p{background:red; } </style> <body><p class="active">p1<p><p>p2</p> </body>結構偽類選擇器
偽類
<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>屬性選擇器(常用)
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[]{}*//* a[id]{background: red;}*//*id=first的元素*//* a[id=first]{background: aqua;}*//*class中有links元素*//* a[class = "links item2 first2"]{background: orange;}*//*a[class*="links"]{background: black ;}*//*選中href中以http開頭的元素*/a[href^="http"]{background: orange;}</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" class="links item6 first6" >6</a><a href="yyy" class="links item7 first7" >7</a> </p> </body> </html>span標簽
重點要突出的字,使用span標簽套起來
<style>#title1{font-size: 50px;} </style> <body> 學習語言<span id="title1">JAVA</span> </body>字體樣式
font-family:字體
font-size:字體大小
font-weight:字體粗細
文本樣式
圖片、文字水平對齊
img,span{vertical-align:middle}[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-Z0vUNcL5-1608875405695)(C:\Users\王東梁\AppData\Roaming\Typora\typora-user-images\image-20201225003006064.png)]
超鏈接偽類
<style>a{/*超鏈接有默認的顏色*/text-decoration:none;color:#000000;}a:hover{/*鼠標懸浮的狀態*/color:orange;}a:active{/*鼠標按住未釋放的狀態*/color:green}a:visited{/*點擊之后的狀態*/color:red} </style>陰影
/* 第一個參數:表示水平偏移第二個參數:表示垂直偏移第三個參數:表示模糊半徑第四個參數:表示顏色 */ text-shadow:5px 5px 5px 顏色[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-gFnix5Xu-1608875405699)(C:\Users\王東梁\AppData\Roaming\Typora\typora-user-images\image-20201225095229521.png)]
列表ul li
/*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 } /*放在div中,作為導航欄*/ <div id="nav"></div> #nav{width:300px; }背景
3.綜合使用
background:red url("圖片相對路徑") 270px 10px no-repeat background-position:/*定位:背景位置*/漸變
網址:https://www.grablent.com
徑向漸變、圓形漸變
盒子模型
邊框
border:粗細 樣式 顏色
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-llfEVadD-1608875405700)(C:\Users\王東梁\AppData\Roaming\Typora\typora-user-images\image-20201225103546532.png)]
外邊距----妙用:居中
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+border+padding+內容的大小
總結:
body總有一個默認的外邊距 margin:0
常見操作:初始化
居中的終極方法
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-bdJ0Y6Kk-1608875405703)(C:\Users\王東梁\AppData\Roaming\Typora\typora-user-images\image-20201225110537377.png)]
圓角邊框----border-radius
border-radius有四個參數(順時針),左上開始
圓圈:圓角=半徑
盒子陰影
box-shadow: 10px 10px 1px black;源碼之家網站:www.mycodes.net
光年后臺管理系統: http://lyear.itshubao.com/index.html#
vue-element-admin:https://panjiachen.github.io/vue-element-admin-site/zh/
element:https://element.eleme.cn/#/zh-CN/component/installation
飛冰:https://ice.work
門戶網站模板之家:https://ice.work
浮動
標準文檔流
塊級元素:獨占一行 h1~h6 、p、div、 列表…
行內元素:不獨占一行 span、a、img、strong
注: 行內元素可以包含在塊級元素中,反之則不可以。
display(重要)
這也是一種實現行內元素排列的方式,但是我們很多情況用float
QQ會員頁面導航練習
float:left/right左右浮動
clear:both
overflow及父級邊框塌陷問題
clear:
right:右側不允許有浮動元素
left:左側不允許有浮動元素
both:兩側不允許有浮動元素
none:
解決塌陷問題方案:
方案一
增加父級元素的高度;
方案二
增加一個空的div標簽,清除浮動
<div class = "clear"></div> <style>.clear{clear:both;margin:0;padding:0; } </style>[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-HuGA4JNe-1608875405705)(C:\Users\王東梁\AppData\Roaming\Typora\typora-user-images\image-20201225123439932.png)]
方案三
在父級元素中增加一個overflow:hidden
overflow:hidden/*隱藏*/ overflow:scoll/*滾動*/layui:https://www.layui.com/admin/
方案四(重要)
父類添加一個偽類:after
#father:after{content:'';display:block;clear:both; }小結:
display與float對比
定位
相對定位
相對定位:positon:relstive;
相對于原來的位置,進行指定的偏移,相對定位的話,它仍然在標準文檔流中,原來的位置會被保留
絕對定位-absolute
定位:基于xxx定位,上下左右~
1、沒有父級元素定位的前提下,相對于瀏覽器定位
2、假設父級元素存在定位,我們通常會相對于父級元素進行偏移
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>z-index層級
圖層~
z-index:默認是0,最高無限~999
動畫及視野拓展
菜鳥教程:https://www.runoob.com
總結
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-IGvckSf0-1608875405707)(C:\Users\王東梁\AppData\Roaming\Typora\typora-user-images\image-20201225134804298.png)]
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-RmRi8zok-1608875405707)(C:\Users\王東梁\AppData\Roaming\Typora\typora-user-images\image-20201225134837125.png)]
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-jRdYijjX-1608875405708)(C:\Users\王東梁\AppData\Roaming\Typora\typora-user-images\image-20201225134917099.png)]
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-reE3wqBZ-1608875405709)(C:\Users\王東梁\AppData\Roaming\Typora\typora-user-images\image-20201225134945388.png)]
總結
- 上一篇: 121GB,《地平线 西之绝境》完整版是
- 下一篇: 购物车的功能——CSS源码