走入計算機的第四十六天(css語法二)
一 屬性選擇器
E[att] 匹配所有具有att屬性的E元素,不考慮它的值。(註意:E在此處可以省略。
比如“[cheacked]”。以下同。) p[title] { color:
#f00; }
E[att
=
val] 匹配所有att屬性等於“val”的E元素 div[
class
=
”error”] { color:
#f00; }
E[att~
=
val] 匹配所有att屬性具有多個空格分隔的值、其中一個值等於“val”的E元素
td[
class
~
=
”name”] { color:
#f00; }
E[attr^
=
val] 匹配屬性值以指定值開頭的每個元素
div[
class
^
=
"test"
]{background:
#ffff00;}
E[attr$
=
val] 匹配屬性值以指定值結尾的每個元素 div[
class
$
=
"test"
]{background:
#ffff00;}
E[attr
*
=
val] 匹配屬性值中包含指定值的每個元素 div[
class
*
=
"test"
]{background:
#ffff00;}
二 偽類選擇器
anchor偽類:專用於控制鏈接的顯示效果
‘‘‘ a:link(沒有接觸過的鏈接),用於定義了鏈接的常規狀態。 a:hover(鼠標放在鏈接上的狀態),用於產生視覺效果。 a:visited(訪問過的鏈接),用於閱讀文章,能清楚的判斷已經訪問過的鏈接。 a:active(在鏈接上按下鼠標時的狀態),用於表現鼠標按下時的鏈接狀態。 偽類選擇器 : 偽類指的是標簽的不同狀態: a ==> 點過狀態 沒有點過的狀態 鼠標懸浮狀態 激活狀態 a:link {color: #FF0000} /* 未訪問的鏈接 */ a:visited {color: #00FF00} /* 已訪問的鏈接 */ a:hover {color: #FF00FF} /* 鼠標移動到鏈接上 */ a:active {color: #0000FF} /* 選定的鏈接 */ 格式: 標簽:偽類名稱{ css代碼; } ‘‘‘
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; } .c1{ width: 100px; height: 200px; background-color: wheat; floatView Code: left; } .c2{ width: 300px; height: 100px; background-color: green; float: left; } .c3{ width: 200px; height: 200px; background-color: goldenrod; float: left; } .outer{ width: 400px; height: 300px; background-color: green; margin: 20px auto; } </style> </head> <body> <!--<div class="c1"></div>--> <!--<div class="c2"></div>--> <!--<div class="c3"></div>--> <!--<span class="c1"></span>--> <!--<span class="c2"></span>--> <!--<span class="c3"></span>--> <!--<div class="outer"></div>--> <!--<ul>--> <!--<li>1</li>--> <!--<li>2</li>--> <!--<li>3</li>--> <!--<li>4</li>--> <!--<li>5</li>--> <!--<li>6</li>--> <!--<li>7</li>--> <!--<li>8</li>--> <!--<li>9</li>--> <!--<li>10</li>--> <!--</ul>--> </body> </html>
before after偽類
:before p:before 在每個<p>元素之前插入內容 :after p:after 在每個<p>元素之後插入內容 例:p:before{content:"hello";color:red;display: block;}
三 選擇器優先級
css繼承
繼承是CSS的一個主要特征,它是依賴於祖先-後代的關系的。繼承是一種機制,它允許樣式不僅可以應用於某個特定的元素,還可以應用於它的後代。例如一個BODY定義了的顏色值也會應用到段落的文本中。
1 |
body{color:red;} <p>helloyuan< / p>
|
這段文字都繼承了由body {color:red;}樣式定義的顏色。然而CSS繼承性的權重是非常低的,是比普通元素的權重還要低的0。
1 |
p{color:green}
|
發現只需要給加個顏色值就能覆蓋掉它繼承的樣式顏色。由此可見:任何顯示申明的規則都可以覆蓋其繼承樣式。
此外,繼承是CSS重要的一部分,我們甚至不用去考慮它為什麽能夠這樣,但CSS繼承也是有限制的。有一些屬性不能被繼承,如:border, margin, padding, background等。
div{
border:1px solid #222
}
<div>hello <p>yuan</p> </div>
css的優先級
所謂CSS優先級,即是指CSS樣式在瀏覽器中被解析的先後順序。
樣式表中的特殊性描述了不同規則的相對權重,它的基本規則是:
1 內聯樣式表的權值最高 style=""------------1000;
2 統計選擇符中的ID屬性個數。 #id --------------100
3 統計選擇符中的CLASS屬性個數。 .class -------------10
4 統計選擇符中的HTML標簽名個數。 p ---------------1
按這些規則將數字符串逐位相加,就得到最終的權重,然後在比較取舍時按照從左到右的順序逐位比較。
1、文內的樣式優先級為1,0,0,0,所以始終高於外部定義。
2、有!important聲明的規則高於一切。
3、如果!important聲明沖突,則比較優先權。
4、如果優先權一樣,則按照在源碼中出現的順序決定,後來者居上。
5、由繼承而得到的樣式沒有specificity的計算,它低於一切其它規則(比如全局選擇符*定義的規則)。
四 css屬性操作
css text
文本顏色:color
顏色屬性被用來設置文字的顏色。
顏色是通過CSS最經常的指定:
- 十六進制值 - 如: #FF0000
- 一個RGB值 - 如: RGB(255,0,0)
- 顏色的名稱 - 如: red
1 |
p { color: rebeccapurple; }
|
水平對齊方式
text-align 屬性規定元素中的文本的水平對齊方式。
- left 把文本排列到左邊。默認值:由瀏覽器決定。
- right 把文本排列到右邊。
- center 把文本排列到中間。
- justify 實現兩端對齊文本效果。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>css</title>
<style>
h2 {text-align:center;}
p.publish_time {text-align:right;}
p.content {text-align:justify;}
</style>
</head>
<body>
<h1>CSS text-align 水平居中</h1>
<p class="publish_time">2017 年 5 月 17 號</p>
<p class="content">
有個落拓不得誌的中年人每隔三兩天就到教堂祈禱,而且他的禱告詞幾乎每次都相同。第一次他到教堂時,
跪在聖壇前,虔誠地低語:“上帝啊,請念在我多年來敬畏您的份上。讓我中一次彩票吧!阿門。”
幾天後,他又垂頭喪氣回到教堂,同樣跪著祈禱:“上帝啊,為何不讓我中彩票?我願意更謙卑地來
服侍你,求您讓我中一次彩票吧!阿門。”又過了幾天,他再次出現在教堂,同樣重復他的祈禱。如此周而
復始,不間斷地祈求著。到了最後一次,他跪著:“我的上帝,為何您不垂聽我的祈求?讓我中一次彩票吧!
只要一次,讓我解決所有困難,我願終身奉獻,專心侍奉您……”就在這時,聖壇上發出一陣宏偉莊嚴的聲
音:“我一直垂聽你的禱告。可是最起碼?你也該先去買一張彩票吧!”</p>
<p><b>註意:</b> 重置瀏覽器窗口大小查看 "justify" 是如何工作的。</p>
</body>
</html>
文本其它屬性
/*
font-size: 10px;
line-height: 200px; 文本行高 通俗的講,文字高度加上文字上下的空白區域的高度 50%:基於字體大小的百分比
vertical-align:-4px 設置元素內容的垂直對齊方式 ,只對行內元素有效,對塊級元素無效
text-decoration:none text-decoration 屬性用來設置或刪除文本的裝飾。主要是用來刪除鏈接的下劃線
font-family: ‘Lucida Bright‘
font-weight: lighter/bold/border/
font-style: oblique
text-indent: 150px; 首行縮進150px
letter-spacing: 10px; 字母間距
word-spacing: 20px; 單詞間距
text-transform: capitalize/uppercase/lowercase ; 文本轉換,用於所有字句變成大寫或小寫字母,或每個單詞的首字母大寫
*/
背景屬性
屬性介紹
- background-color
- background-image
- background-repeat
- background-position
1 2 3 4 5 6 7 |
background - color: cornflowerblue
background - image: url( ‘1.jpg‘ );
background - repeat: no - repeat;(repeat:平鋪滿)
background - position: right top( 20px 20px );
|
簡寫
1 |
background: #ffffff url(‘1.png‘) no-repeat right top;
|
邊框屬性
屬性介紹
- border-width
- border-style (required)
- border-color
1 2 3 4 5 |
border - style: solid;
border - color: chartreuse;
border - width: 20px ;
|
簡寫
1 |
簡寫:border: 30px rebeccapurple solid;
|
邊框-單獨設置各邊
1 2 3 4 |
border - top - style:dotted;
border - right - style:solid;
border - bottom - style:dotted;
border - left - style:none;
|
列表屬性
1 2 3 4 5 |
list - style - type 設置列表項標誌的類型。
list - style - image 將圖象設置為列表項標誌。
list - style - position 設置列表中列表項標誌的位置。
list - style 簡寫屬性。用於把所有用於列表的屬性設置於一個聲明中
|
ist-style-type屬性指定列表項標記的類型:
1 |
ul { list - style - type : square; }
|
使用圖像來替換列表項的標記:
1 2 3 |
ul {
list - style - image: url(‘‘);
}
|
dispaly屬性
none
block
inline
- inline-block
none(隱藏某標簽)
1 |
p{display:none;}
|
註意與visibility:hidden的區別:
visibility:hidden可以隱藏某個元素,但隱藏的元素仍需占用與未隱藏之前一樣的空間。也就是說,該元素雖然被隱藏了,但仍然會影響布局。
display:none可以隱藏某個元素,且隱藏的元素不會占用任何空間。也就是說,該元素不但被隱藏了,而且該元素原本占用的空間也會從頁面布局中消失。
block(
內聯標簽設置為塊級標簽)
1 |
span {display:block;}
|
註意:一個內聯元素設置為display:block是不允許有它內部的嵌套塊元素。
inline
(塊級
標簽設置為內聯標簽)
1 |
li {display:inline;}
|
inline-block
display:inline-block可做列表布局,其中的類似於圖片間的間隙小bug可以通過如下設置解決:
1 2 3 4 |
#outer{
border: 3px dashed;
word - spacing: - 5px ;
}
|
外邊距(margine)和內邊距(padding)
盒子模型
- margin: 用於控制元素與元素之間的距離;margin的最基本用途就是控制元素周圍空間的間隔,從視覺角度上達到相互隔開的目的。
- padding: 用於控制內容與邊框之間的距離;
- Border(邊框): 圍繞在內邊距和內容外的邊框。
- Content(內容): 盒子的內容,顯示文本和圖像。
margine(外邊距)
單邊外邊距屬性:
1 2 3 4 |
margin - top: 100px ;
margin - bottom: 100px ;
margin - right: 50px ;
margin - left: 50px ;
|
簡寫屬性
margin:10px 20px 20px 10px;
上邊距為10px
右邊距為20px
下邊距為20px
左邊距為10px
margin:10px 20px 10px;
上邊距為10px
左右邊距為20px
下邊距為10px
margin:10px 20px;
上下邊距為10px
左右邊距為20px
margin:25px;
所有的4個邊距都是25px
View Code
居中應用
1 |
margin: 0 auto;
|
padding(內邊距)
單獨使用填充屬性可以改變上下左右的填充。縮寫填充屬性也可以使用,一旦改變一切都改變。
設置同margine;
頁碼實例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .outer{ margin: 0 auto; width: 80%; } .content{ background-color: darkgrey; height: 500px; } a{ text-decoration: none; } .page-area{ text-align: center; padding-top: 30px; padding-bottom: 30px; background-color: #f0ad4e; } .page-area ul li{ display: inline-block; } .page-area ul li a ,.page-area ul li span{ display: inline-block; color: #369; height: 25px; width: 25px; text-align: center; line-height: 25px; padding: 8px; margin-left: 8px; border: 1px solid #e1e1e1; border-radius: 15%; } .page-area ul li .page-next{ width: 70px; border-radius:0 } .page-area ul li span.current_page{ border: none; color: black; font-weight:900; } .page-area ul li a:hover{ color: #fff; background-color: #2459a2; } </style> </head> <body> <div class="outer"> <div class="content"></div> <div class="page-area"> <ul> <li><span class="current_page">1</span></li> <li><a href="#" class="page-a">2</a></li> <li><a href="#" class="page-a">3</a></li> <li><a href="#" class="page-a">4</a></li> <li><a href="#" class="page-a">5</a></li> <li><a href="#" class="page-a">6</a></li> <li><a href="#" class="page-a">7</a></li> <li><a href="#" class="page-a">8</a></li> <li><a href="#" class="page-a">9</a></li> <li><a href="#" class="page-a">10</a></li> <li><a href="#" class="page-a page-next">下一頁</a></li> </ul> </div> </div> </body> </html>View Code
思考1:body的外邊距
邊框在默認情況下會定位於瀏覽器窗口的左上角,但是並沒有緊貼著瀏覽器的窗口的邊框,這是因為body本身也是一個盒子(外層還有html),在默認情況下, body距離html會有若幹像素的margin,具體數值因各個瀏覽器不盡相同,所以body中的盒子不會緊貼瀏覽器窗口的邊框了,為了驗證這一點,加上:
body{
border: 1px solid;
background-color: cadetblue;
}
>>>>解決方法:
body{
margin: 0;
}
思考2:margin collapse(邊界塌陷或者說邊界重疊)
1、兄弟div:
上面div的margin-bottom和下面div的margin-top會塌陷,也就是會取上下兩者margin裏最大值作為顯示值
2、父子div:
if
父級div中沒有border,padding,inlinecontent,子級div的margin會一直向上找,直到找到某個標簽包括border,padding,inline
content中的其中一個,然後按此div 進行margin;
<!DOCTYPE html> <html lang="en" style="padding: 0px"> <head> <meta charset="UTF-8"> <title>Title</title> <style> body{ margin: 0px; } .div1{ background-color: rebeccapurple; width: 300px; height: 300px; overflow: hidden; } .div2{ background-color: green; width: 100px; height: 100px; margin-bottom: 40px; margin-top: 20px; } .div3{ background-color:teal; width: 100px; height: 100px; margin-top: 20px; } </style> </head> <body> <div style="background-color: bisque;width: 300px;height: 300px"></div> <div class="div1"> <div class="div2"></div> <div class="div3"></div> </div> </body> </html>View Code
走入計算機的第四十六天(css語法二)