伪元素和 CSS 类
cssweb developmentfront end technology更新于 2025/1/10 20:28:17
CSS 伪元素既可以应用于元素,也可以应用于 CSS 类。
示例
让我们看一个在 CSS 类上使用 CSS 伪元素的示例 −
<!DOCTYPE html>
<html>
<head>
<title>CSS Pseudo Elements and CSS Classes</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
box-sizing: border-box;
}
input[type="button"] {
border-radius: 10px;
}
.child{
line-height: .1em;
border: 4px solid black;
}
::selection{
color:white;
}
.child:nth-child(1), h1::selection{
background-color: #FF8A00;
}
.child:nth-child(2), div::selection{
background-color: #F44336;
}
.child:nth-child(3), p::selection{
background-color: #C303C3;
}
.child:nth-child(4), article::selection{
background-color: #4CAF50;
}
.child:nth-child(5), code::selection{
background-color: #FEDC11;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>CSS Pseudo Elements and CSS Classes</legend>
<div id="container">
<h1>A Heading</h1>
<div>Welcome to DIV World</div>
<p>Every one is welcome in this town. Make yourself comfertable</p>
<article>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</article>
<code>function helloWorld(){ document.write("Hello World");}</code>
<div><span class="child">h1</span><span class="child">div</span><span class="child">p</span><span class="child">article</span><span class="child">code</span></div>
</div>
</body></html>
输出
这将产生以下输出 −
初始布局 −

选择 <h1> 元素和 <div>元素 −


选择 <p> 元素和 <article>元素 −


选择 <code> 元素 −

相关文章
有用资源
css 参考教程 - 该教程包含有关 css 的更多信息:https://www.cainiaomax.com/css/

