
批改狀態(tài):合格
老師批語:
根據(jù)元素的位置或狀態(tài)來匹配子元素
與上下文選擇器很相似
后文演示所用 HTML 代碼如下:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>偽類展示</title>
<link rel="stylesheet" href="rest.css" />
<style>
/* 此處填充 演示代碼 */
</style>
</head>
<body>
<ul class="list">
<li class="item">item1</li>
<li class="item">item2</li>
<li class="item">item3</li>
<li class="item">item4</li>
<li class="item">item5</li>
<li class="item">item6</li>
<li class="item">item7</li>
<li class="item">item8</li>
<li class="item">item9</li>
</ul>
</body>
</html>
:nth-child(an+b)
:獲取任意位置的元素規(guī)則: 計算出來的索引,必須是有效的(從 1 開始)
.list > .item:nth-child(5) {
background-color: red;
}
.list > .item:nth-child(-n + 3) {
background-color: red;
}
.list > .item:nth-last-child(-n + 3) {
background-color: red;
}
.list > .item:nth-child(2n + 1) {
background-color: red;
}
.list > .item:nth-child(2n - 1) {
background-color: red;
}
可使用參數(shù)(odd)進行選擇,更加方便快捷明了
.list > .item:nth-child(odd) {
background-color: red;
}
.list > .item:nth-child(even) {
background-color: red;
}
.list > .item:nth-child(2n) {
background-color: red;
}
.list > .item:nth-child(3n + 1) {
background-color: red;
}
:first-child
:選中父元素下的第一個子元素
.list > .item:first-child {
background-color: red;
}
:last-child
:選中父元素下的最后一個子元素
.list > .item:last-child {
background-color: red;
}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>狀態(tài)偽類</title>
<link rel="stylesheet" href="form.css" />
<link rel="stylesheet" href="fake-status.css" />
</head>
<body>
<form action="">
<fieldset class="login">
<legend class="title">用戶登錄</legend>
<label for="uname">呢稱:</label>
<input type="text" name="uname" autofocus required />
<label for="email">郵箱:</label>
<input type="email" name="email" required />
<label for="tel">電話:</label>
<input type="tel" name="tel" />
<div class="remember">
<input type="checkbox" name="remember" id="rem" />
<label for="rem">記住我</label>
</div>
<button class="submit">提交</button>
</fieldset>
</form>
</body>
</html>
form.css
為樣式代碼,fake-status.css
為實例操作代碼,樣式代碼如下:
.login {
display: inline-grid;
grid: auto-flow / 3em 1fr;
gap: 10px 0;
padding: 1em;
}
.login input {
border: none;
border-bottom: thin solid #666;
}
.login .title {
text-align: center;
}
.login .btn,
.login .remember {
grid-column: 2;
height: 2.2em;
}
.btn .submit,
.btn .reset {
width: 40%;
height: 2.2em;
}
鏈接,表單
:hover
: 鼠標懸停
.login > .submit:hover {
cursor: pointer;
background-color: seagreen;
color: white;
}
:enabled
: 有效控件.login
下所有有效控件的邊框
.login :enabled {
border: solid 5px rebeccapurple;
}
:disabled
: 禁用控件.login
下禁用控件的樣式。
.login :disabled {
background-color: aqua;
color: cadetblue;
border-color: antiquewhite;
}
:checked
: 選中控件
.login :checked + label {
color: red;
}
:required
: 必選控件
.login :required {
background-color: yellow;
}
-運行效果
:focus
: 焦點控件
.login :focus {
outline: 1px solid red;
border-bottom: none;
}
:not()
: 過濾掉某些元素.login
下非input
元素的背景顏色。
.login :not(input) {
background-color: red;
}
:empty
: 選擇頁面中為空的元素
.login :empty {
background-color: red;
}
關于更多偽類知識請查詢 MDN
優(yōu)先級就是分配給指定的 CSS 聲明的一個權重,它由 匹配的選擇器中的 每一種選擇器類型的 數(shù)值 決定。
而當優(yōu)先級與多個 CSS 聲明中任意一個聲明的優(yōu)先級相等的時候,CSS 中最后的那個聲明將會被應用到元素上。
當同一個元素有多個聲明的時候,優(yōu)先級才會有意義。因為每一個直接作用于元素的 CSS 規(guī)則總是會接管/覆蓋(take over)該元素從祖先元素繼承而來的規(guī)則。
可簡單理解為: 標簽是個位, class 是十位, id 是百位
<div class="demo" id="test">
<span class="text" id="title">愛你直到永遠!</span>
</div>
/* 權重100 */
#title {
color: green;
}
/* 權重010無法覆蓋綠色值 */
.text {
color: red;
}
/* 權重110,可以覆蓋綠色值! */
#title.text {
color: yellow;
}
/* 權重020無法覆蓋綠色值 */
.demo > .text {
color: aqua;
}
/* 權重224,為span元素的頂級權重 */
html > body > div#test.demo > span#title.text {
color: brown;
}
width
: 寬height
: 高padding
: 內(nèi)邊距border
: 邊框margin
: 外邊距width
,style
,color
<div class="test">
<span>demo</span>
</div>
.test {
width: 180px;
height: 100px;
/* 為每條邊框設置不同的顏色 */
border-top-width: 2px;
border-top-style: solid;
border-top-color: red;
border-top: 2px solid red;
border-right: 2px solid blue;
border-bottom: 2px solid green;
border-left: 2px solid violet;
/* 四條邊框相同,可簡寫 */
/* border: 2px solid red; */
}
width
padding/margin
.test {
width: 180px;
height: 100px;
/* 為每條邊框設置不同的顏色 */
border-top-width: 2px;
border-top-style: solid;
border-top-color: red;
border-top: 2px solid red;
border-right: 2px solid blue;
border-bottom: 2px solid green;
border-left: 2px solid violet;
/* 四條邊框相同,可簡寫 */
/* border: 2px solid red; */
margin: 5px 10px 15px 20px;
}
兩個值的邊距設置,第一個為上下,第二個為左右。例如:margin: 5px 10px;
邊距查看
margin: 5px 10px 30px;
內(nèi)邊距設置同樣如此,在二值和三值的情況下,左右永遠在第二個位置。
padding
,border
.test {
width: 180px;
height: 100px;
}
box-sizing
: 設置盒模型計算邊界content-box
: 默認值,僅包括內(nèi)容區(qū)
.test {
width: 180px;
height: 100px;
margin: 5px;
padding: 5px;
border: solid 2px red;
}
此模式下,盒子大小改變,寬高超出范圍不利于計算。
border-box
: 推薦值,寬高擴展到可視邊框
.test {
width: 180px;
height: 100px;
margin: 5px;
padding: 5px;
border: solid 2px red;
box-sizing: border-box;
}
此模式下,邊距往內(nèi)擴展,盒子大小不變。
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號