
批改狀態(tài):合格
老師批語:總結(jié)之后, 是不是發(fā)現(xiàn)其實(shí)挺簡單的
一、選擇器類型
1、CSS選擇器類型可分為:簡單選擇器(含屬性選擇器)以及偽類選擇器
2、簡單選擇器見表一:
序號 | 選擇器 | 描述 | 舉例 |
---|---|---|---|
1 | 元素選擇器 | 根據(jù)元素標(biāo)簽名稱進(jìn)行匹配 | div {...} |
2 | 群組選擇器 | 同時(shí)選擇多個(gè)不同類型的元素 | h1,h2,h3{...} |
3 | 通配選擇器 | 選擇全部元素,不區(qū)分類型 | * {...} |
4 | 屬性選擇器 | 根據(jù)元素屬性進(jìn)行匹配 | *[...] |
5 | 后代選擇器 | 選擇當(dāng)前元素的所有后代元素空格 |
ul li{……} |
6 | 父子選擇器 | 選擇當(dāng)前元素的所有自帶元素> |
form>section{……} |
8 | 同級相鄰選擇器 | 選擇當(dāng)前擁有共同父級且相鄰的元素+ |
li.item+li{……} |
9 | 同級所有元素 | 選擇擁有共同父級的后續(xù)所有元素~ |
li.item~li{………} |
3、常有簡單屬性選擇器:
常見屬性選擇器 | 舉例 |
---|---|
類選擇器 | .container{………} |
id選擇器 | #first{……} |
4、偽類選擇器:結(jié)構(gòu)偽類選擇器和表單偽類選擇器
5、結(jié)構(gòu)偽類選擇器
a、結(jié)構(gòu)偽類選擇器分為:不分類型匹配(:nth-child(n)
)和分類型匹配(:nth-of-type(n)
);
b、結(jié)構(gòu)偽類參考表(n從1開始計(jì)算)
不分類匹配 | 分類匹配 | |
---|---|---|
匹配第一個(gè)元素 | :first-child{……} |
:first-of-type{……} |
匹配最后一個(gè)元素 | :last-child{……} |
:last-of-type{……} |
匹配任意元素 | :nth-child(n){……} |
:nth-of-type(){……} |
備注:匹配任意元素中n可以通過表達(dá)式來表示匹配一組元素,表達(dá)式中的n從0開始,且必須寫在前面;-n表示獲取前面一組元素,正數(shù)表示從指定元素獲取余下元素;
6、表單選擇器:
7、其他偽類選擇器
選擇器 | 描述 |
---|---|
:active |
向被激活的元素添加樣式 |
:focus |
向擁有鍵盤輸入焦點(diǎn)的元素添加樣式 |
:hover |
當(dāng)鼠標(biāo)懸浮在元素上方時(shí),向元素添加樣式 |
:link |
向未被訪問的鏈接添加樣式 |
:visited |
向已被訪問的鏈接添加樣式 |
:root |
根元素,通常是html |
:empty |
選擇沒有任何子元素的元素(含文本節(jié)點(diǎn)) |
:not() |
排除與選擇器參數(shù)匹配的元素 |
1、代碼部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
/* 元素選擇器 */
body {
background-color: #e3e3e3;
}
/* 類選擇器 */
.container {
width: 300px;
height: 300px;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
}
/* 父子選擇器 */
.container > * {
background-color: #80ffff;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
}
/* 后代選擇器 */
.container * {
/* border: 1px solid #ff0000; */
}
/* 偽類選擇器 */
.container > span:first-of-type {
background-color: lightblue;
}
/* ID選擇器 */
#center {
background-color: #ff00ff;
}
/*偽類選擇器*/
.container > :first-child {
background-color: lightblue;
}
/*相鄰?fù)夁x擇*/
#center + .item {
background-color: red;
}
/* 偽類選擇器 */
.item:nth-child(n + 7) {
background-color: lightgreen;
}
/*元素選擇器*/
fieldset {
width: 400px;
border: none;
background-color: lightgreen;
display: grid;
grid-template-columns: 1fr;
justify-items: center;
gap: 15px;
}
/*其他偽類選擇器,鼠標(biāo)移動到選中*/
fieldset:hover {
box-shadow: 0 0 3px rosybrown;
}
fieldset > legend {
text-align: center;
width: 400px;
margin: auto;
font-size: 20px;
color: green;
font-weight: bolder;
}
section {
width: 260px;
margin: 10px auto;
display: grid;
grid-template-columns: 60px 1fr;
}
section:last-child {
display: flex;
justify-content: space-between;
}
/*表單選擇器*/
input:enabled {
width: 200px;
height: 20px;
background: khaki;
border-style: none;
}
/*表單選擇器:獲取焦點(diǎn)時(shí)選中*/
input:focus {
background-color: #fff;
outline: none;
}
.button {
width: 100px;
height: 30px;
}
.button:hover {
border: none;
outline: none;
background-color: royalblue;
color: seashell;
}
</style>
</head>
<body>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<span class="item" id="center">5</span>
<span class="item">6</span>
<span class="item">7</span>
<span class="item">8</span>
<span class="item">9</span>
</div>
<hr />
<form action="">
<fieldset>
<legend>用戶登陸</legend>
<section>
<label for="username">賬號:</label>
<input
type="text"
placeholder="explome@qq.com"
id="username"
name="username"
required
autofocus
/>
</section>
<section>
<label for="pasword">密碼:</label>
<input
type="password"
name="password"
id="password"
placeholder="輸入密碼"
reuqired
/>
</section>
<section>
<button type="submit" class="button">登陸</button>
<button type="reset" class="button">取消</button>
</section>
</fieldset>
</form>
</body>
</html>
2、運(yùn)行效果圖:
1、css選擇器雖然非常多,但經(jīng)常用的并不多;最常見的由:元素選擇器、類選擇器和id選擇器、偽類選擇器(:nth-child(n)
、:hover
、input:focus
)等等
2、偽類選擇器重點(diǎn)在::nth-child(n+2){……}
在n表達(dá)式上的理解,偶數(shù)可以用even奇數(shù)可以用odd表示;
3、表單偽類選擇器和鏈接偽類選擇器,需要練習(xí)
4、特殊偽類::hover{……}
和input:focus
、:not()
不包含選擇器::not(span){……}
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號