
批改狀態(tài):合格
老師批語(yǔ):課堂 上將常用選擇器進(jìn)行分類, 其實(shí)還有一些沒(méi)有歸入, 可以通過(guò)查閱手冊(cè)
序號(hào) | 選擇器 | 描述 |
---|---|---|
1 | 元素選擇器 | 根據(jù)元素標(biāo)簽名稱來(lái)進(jìn)行操作 |
2 | 群組選擇器 | 可以同時(shí)選擇不同類型的元素進(jìn)行操作 |
3 | 通配符選擇器(通配選擇器) | 選擇全部元素,不區(qū)分類型 |
4 | 屬性選擇器 | 根據(jù)元素屬性進(jìn)行操作 |
5 | 類選擇器 | 根據(jù)元素class屬性進(jìn)行操作 |
6 | id選擇器 | 根據(jù)元素id屬性進(jìn)行操作 |
<head>
<style>
div {
background-color: aquamarine;
font-size: 30px;
}
</style>
</head>
<body>
<div>php中文網(wǎng)</div>
<p>php中文網(wǎng)</p>
<h1>php中文網(wǎng)</h1>
<div>php中文網(wǎng)</div>
<div>php中文網(wǎng)</div>
</body>
<style>
div,p,h1{
font-size: 10px;
background-color: lightblue;
}
</style>
<style>
*{
border: 1px solid lightpink;
}
</style>
<div style="font-size: 25px; color: lightseagreen;">php中文網(wǎng)</div>
<head>
<style>
.study{
border: 1px solid lightpink;
color: lightseagreen;
background-color: lightgreen;
}
</style>
</head>
<body>
<div class="study">php中文網(wǎng)</div>
</body>
<head>
<style>
#study{
border: 1px solid lightpink;
color: yellow;
background-color: red;
}
</style>
</head>
<body>
<div id="study">php中文網(wǎng)</div>
<p>php中文網(wǎng)</p>
<h1>php中文網(wǎng)</h1>
<div>php中文網(wǎng)</div>
<div>php中文網(wǎng)</div>
</body>
序號(hào) | 角色 | 描述 |
---|---|---|
1 | 祖先元素 | 擁有子元素,孫元素等所有層次的后代元素 |
2 | 父級(jí)元素 | 僅擁有子元素層級(jí)的元素 |
3 | 后代元素 | 與其他層級(jí)元素一起擁有共同祖先元素 |
4 | 子元素 | 與其他同級(jí)元素一起共有父級(jí)元素 |
序號(hào) | 選擇器 | 操作符 | 描述 | 例子 |
---|---|---|---|---|
1 | 后代選擇器 | 空格 |
選擇當(dāng)前元素的所有后代元素 | div p , body * |
2 | 父子選擇器 | > |
選擇當(dāng)前元素的所有子元素 | div > h2 |
3 | 同級(jí)相鄰選擇器 | + |
選擇擁有共同父級(jí)且相鄰的元素 | li.red + li |
4 | 同級(jí)所有選擇器 | ~ |
選擇擁有共同父級(jí)的后續(xù)所有元素 | li.red ~ li |
后代選擇器:
<head>
<style>
div p{
border: 5px solid lightblue;
color: red;
background-color: lightpink;
}
</style>
</head>
<body>
<div>
<p>php中文網(wǎng)</p>
</div>
</body>
父子選擇器:
<style>
div > p{
border: 5px solid lightblue;
color: red;
background-color: lightpink;
}
</style>
同級(jí)相鄰選擇器:
<head>
<style>
.center.asina + .center {
background-color: lime;
color: maroon;
border: 2px solid yellow;
}
</style>
</head>
<body>
<div>
<p class="center">php中文網(wǎng)</p>
<p class="center">php中文網(wǎng)</p>
<p class="center asina">php中文網(wǎng)</p>
<p class="center">php中文網(wǎng)</p>
<p class="center">php中文網(wǎng)</p>
<p class="center">php中文網(wǎng)</p>
</div>
</body>
同級(jí)所有選擇器:
<head>
<style>
.center.asina + .center {
background-color: lime;
color: maroon;
border: 2px solid yellow;
}
</style>
</head>
<body>
<div>
<p class="center">php中文網(wǎng)</p>
<p class="center">php中文網(wǎng)</p>
<p class="center asina">php中文網(wǎng)</p>
<p class="center">php中文網(wǎng)</p>
<p class="center">php中文網(wǎng)</p>
<p class="center">php中文網(wǎng)</p>
</div>
</body>
序號(hào) | 選擇器 | 描述 | 舉例 |
---|---|---|---|
1 | :first-child |
匹配第一個(gè)子元素 | div :first-child |
2 | :last-child |
匹配最后一個(gè)子元素 | div :last-child |
3 | :only-child |
選擇元素的唯一子元素 | div :only-child |
4 | :nth-child(n) |
匹配任意位置的子元素 | div :nth-child(n) |
5 | :nth-last-child(n) |
匹配倒數(shù)任意位置的子元素 | div :nth-last-child(n) |
例:
<style>
.review > :first-child {
background-color: wheat;
}
</style>
<style>
.review > :last-child {
background-color: wheat;
}
</style>
<style>
.review > :nth-child(-n + 3) {
background-color: lightgreen;
}
</style>
序號(hào) | 選擇器 | 描述 | 舉例 |
---|---|---|---|
1 | :first-of-type |
匹配按類型分組后的第一個(gè)子元素 | div :first-of-type |
2 | :last-of-type |
匹配按類型分組后的最后一個(gè)子元素 | div :last-of-type |
3 | :only-of-type |
匹配按類型分組后的唯一子元素 | div :only-of-type |
4 | :nth-of-type() |
匹配按類型分組后的任意位置的子元素 | div :nth-of-type(n) |
5 | :nth-last-of-type() |
匹配按類型分組后倒數(shù)任意位置的子元素 | div :nth-last-of-type(n) |
例:
<head>
<style>
.review span:first-of-type {
background-color: violet;
}
</style>
</head>
<body>
<div class="review">
<p class="center">php中文網(wǎng)</p>
<p class="center">php中文網(wǎng)</p>
<p class="center asina">php中文網(wǎng)</p>
<span class="center">php中文網(wǎng)</span>
<span class="center">php中文網(wǎng)</span>
<span class="center">php中文網(wǎng)</span>
</div>
</body>
<style>
.review span:last-of-type {
background-color: violet;
}
</style>
<style>
.review span:nth-of-type(-n + 3) {
background-color: violet;
}
</style>
<style>
.review span:nth-last-of-type(-n + 2) {
background-color: violet;
}
</style>
序號(hào) | 選擇器 | 描述 |
---|---|---|
1 | :active |
向被激活的元素添加樣式 |
2 | :focus |
向擁有鍵盤輸入焦點(diǎn)的元素添加樣式 |
3 | :hover |
當(dāng)鼠標(biāo)懸浮在元素上方時(shí),向元素添加樣式 |
4 | :link |
向未被訪問(wèn)的鏈接添加樣式 |
5 | :visited |
向已被訪問(wèn)的鏈接添加樣式 |
5 | :root |
根元素,通常是html |
5 | :empty |
選擇沒(méi)有任何子元素的元素(含文本節(jié)點(diǎn)) |
5 | :not() |
排除與選擇器參數(shù)匹配的元素 |
css選擇器分三種:
簡(jiǎn)單選擇器(其中類選擇器和id選擇器是最常用的)
上下文選擇器
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)