在CSS中,选择符用于选择要应用样式的HTML元素。有许多不同的选择符可用于定位元素。/ 通过元素名称选择元素 / p { color: blue; } / 通过类名选择元素 / .myclass {...
在CSS中,选择符用于选择要应用样式的HTML元素。有许多不同的选择符可用于定位元素。
/* 通过元素名称选择元素 */
p {
color: blue;
}
/* 通过类名选择元素 */
.my-class {
background-color: red;
}
/* 通过ID选择元素 */
#my-id {
font-size: 18px;
}
/* 通过属性选择元素 */
[type="text"] {
border: 1px solid black;
}
/* 通过子元素选择元素 */
p > span {
font-weight: bold;
}
/* 通过相邻元素选择元素 */
p + ul {
margin-top: 10px;
}
/* 通过伪类选择元素 */
a:hover {
color: white;
}
/* 通过伪元素选择元素 */
p::before {
content: "前缀文字";
} 这些只是一些可用的选择符。你可以使用它们中的多个来组合选择元素,例如:
ul li {
list-style-type: circle;
}
a.active:hover {
color: green;
} 上面的示例展示了通过嵌套层次选择元素以及组合多个选择符来选择特定元素的方式。