CSS伪类和伪元素
本文解释了CSS伪类和伪元素。
您可以了解伪类(如:hover)和伪元素(如::before)。
YouTube Video
预览的HTML
css-pseudo.html
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>CSS Pseudo Class/Element</title>
7 <link rel="stylesheet" href="css-base.css">
8 <link rel="stylesheet" href="css-pseudo-class.css">
9</head>
10<body>
11 <!-- Header -->
12 <header>
13 <h1>CSS Pseudo Class/Element</h1>
14 </header>
15
16 <!-- Main content -->
17 <main>
18 <header><h2>Pseudo Class</h2></header>
19 <article>
20 <h3>:hover</h3>
21 <section>
22 <header><h4>CSS</h4></header>
23<pre class="sample">
24a:hover {
25 color: red;
26}
27</pre>
28 <header><h4>HTML</h4></header>
29 <pre><a href="#">Example of hover</a></pre>
30 <header><h4>HTML+CSS</h4></header>
31 <section class="sample-view">
32 <a href="#">Example of hover</a><br>
33 </section>
34 </section>
35 </article>
36 <article>
37 <h3>:focus</h3>
38 <section>
39 <header><h4>CSS</h4></header>
40<pre class="sample">
41input:focus {
42 outline: 2px solid blue;
43}
44</pre>
45 <header><h4>HTML</h4></header>
46 <pre><input type="text" placeholder="Example of focus"></pre>
47 <header><h4>HTML+CSS</h4></header>
48 <section class="sample-view">
49 <input type="text" placeholder="Example of focus">
50 </section>
51 </section>
52 </article>
53 <article>
54 <h3>:active</h3>
55 <section>
56 <header><h4>CSS</h4></header>
57<pre class="sample">
58button:active {
59 background-color: green;
60}
61</pre>
62 <header><h4>HTML</h4></header>
63 <pre><button type="button">Example of active</button></pre>
64 <header><h4>HTML+CSS</h4></header>
65 <section class="sample-view">
66 <button type="button">Example of active</button>
67 </section>
68 </section>
69 </article>
70 <article>
71 <h3>:empty</h3>
72 <section>
73 <header><h4>CSS</h4></header>
74<pre class="sample">
75.box:empty {
76 background-color: lightgray;
77 border: 1px dashed black;
78 height: 50px;
79 width: 50px;
80}
81</pre>
82 <header><h4>HTML</h4></header>
83<pre>
84<p class="box"></p>
85<p class="box"> </p>
86</pre>
87 <header><h4>HTML+CSS</h4></header>
88 <section class="sample-view">
89 <p class="box"></p>
90 <p class="box"> </p>
91 </section>
92 </section>
93 </article>
94 <article>
95 <h3>:valid / :invalid</h3>
96 <section>
97 <header><h4>CSS</h4></header>
98<pre class="sample">
99input:valid {
100 border: 2px solid green;
101 background-color: #eaffea;
102}
103
104input:invalid {
105 border: 2px solid red;
106 background-color: #ffeaea;
107}
108</pre>
109 <header><h4>HTML</h4></header>
110<pre>
111<form>
112 <label>Username (min. 5 characters):</label>
113 <input type="text" pattern=".{5,}" required>
114 <button type="submit">Submit</button>
115</form>
116</pre>
117 <header><h4>HTML+CSS</h4></header>
118 <section class="sample-view">
119 <form>
120 <label>Username (min. 5 characters):</label>
121 <input type="text" pattern=".{5,}" required>
122 <button type="submit">Submit</button>
123 </form>
124 </section>
125 </section>
126 </article>
127 <article>
128 <h3>:in-range / :out-of-range</h3>
129 <section>
130 <header><h4>CSS</h4></header>
131<pre class="sample">
132input[type="number"]:in-range {
133 border: 2px solid blue;
134 background-color: #eaf4ff;
135}
136
137input[type="number"]:out-of-range {
138 border: 2px solid orange;
139 background-color: #fff4e6;
140}
141</pre>
142 <header><h4>HTML</h4></header>
143<pre>
144<form>
145 <label>Age (between 18 and 99):</label>
146 <input type="number" min="18" max="99" required>
147 <button type="submit">Submit</button>
148</form>
149</pre>
150 <header><h4>HTML+CSS</h4></header>
151 <section class="sample-view">
152 <form>
153 <label>Age (between 18 and 99):</label>
154 <input type="number" min="18" max="99" required>
155 <button type="submit">Submit</button>
156 </form>
157 </section>
158 </section>
159 </article>
160 <article>
161 <h3>:nth-child(n)</h3>
162 <section>
163 <header><h4>CSS</h4></header>
164<pre class="sample">
165li:nth-child(2) {
166 color: blue;
167 font-weight: bold;
168}
169
170li:nth-child(odd) {
171 background-color: lightblue;
172}
173
174li:nth-child(even) {
175 background-color: steelblue;
176}
177</pre>
178 <header><h4>HTML</h4></header>
179<pre>
180<ul>
181 <li>First Item</li>
182 <li>Second Item</li>
183 <li>Third Item</li>
184 <li>Fourth Item</li>
185</ul>
186</pre>
187 <header><h4>HTML+CSS</h4></header>
188 <section class="sample-view">
189 <ul>
190 <li>First Item</li>
191 <li>Second Item</li>
192 <li>Third Item</li>
193 <li>Fourth Item</li>
194 </ul>
195 </section>
196 </section>
197 </article>
198 <article>
199 <h3>:first-child</h3>
200 <section>
201 <header><h4>CSS</h4></header>
202<pre class="sample">
203small:first-child {
204 font-weight: bold;
205 color: steelblue;
206}
207</pre>
208 <header><h4>HTML</h4></header>
209<pre>
210 <small>First Small Text</small><br>
211 <small>Second Small Text</small><br>
212 <small>Third Small Text</small>
213</pre>
214 <header><h4>HTML+CSS</h4></header>
215 <section class="sample-view">
216 <small>First Small Text</small><br>
217 <small>Second Small Text</small><br>
218 <small>Third Small Text</small>
219 </section>
220 </section>
221 </article>
222 <article>
223 <h3>:last-child</h3>
224 <section>
225 <header><h4>CSS</h4></header>
226<pre class="sample">
227small:last-child {
228 font-weight: bold;
229 color: seagreen;
230}
231</pre>
232 <header><h4>HTML</h4></header>
233<pre>
234 <small>First Small Text</small><br>
235 <small>Second Small Text</small><br>
236 <small>Third Small Text</small>
237</pre>
238 <header><h4>HTML+CSS</h4></header>
239 <section class="sample-view">
240 <small>First Small Text</small><br>
241 <small>Second Small Text</small><br>
242 <small>Third Small Text</small>
243 </section>
244 </section>
245 </article>
246 <article>
247 <h3>:has()</h3>
248 <section>
249 <header><h4>CSS</h4></header>
250<pre class="sample">
251p:has(span.child) {
252 border: 2px solid black;
253}
254</pre>
255 <header><h4>HTML</h4></header>
256<pre>
257<p>Paragraph with <span class="child">span element</span></p>
258<p>Paragraph without span element</p>
259</pre>
260 <header><h4>HTML+CSS</h4></header>
261 <section class="sample-view">
262 <p>Paragraph with <span class="child">span element</span></p>
263 <p>Paragraph without span element</p>
264 </section>
265 </section>
266 </article>
267 <article>
268 <h3>:not()</h3>
269 <section>
270 <header><h4>CSS</h4></header>
271<pre class="sample">
272q:not(.quote2) {
273 font-weight: bold;
274 color: steelblue;
275}
276</pre>
277 <header><h4>HTML</h4></header>
278<pre>
279 <q class="quote1">First Quote</q><br>
280 <q class="quote2">Second Quote</q><br>
281 <q class="quote3">Third Quote</q>
282</pre>
283 <header><h4>HTML+CSS</h4></header>
284 <section class="sample-view">
285 <q class="quote1">First Quote</q><br>
286 <q class="quote2">Second Quote</q><br>
287 <q class="quote3">Third Small Text</q>
288 </section>
289 </section>
290 </article>
291 <article>
292 <h3>:where() / :is()</h3>
293 <section>
294 <header><h4>CSS</h4></header>
295<pre class="sample">
296:where(p, .detail) span {
297 color: darkcyan;
298 text-decoration: line-through;
299}
300:is(p, .detail) span {
301 font-style: italic;
302}
303p span {
304 text-decoration: none; /* Applied */
305 font-style: normal; /* Not Applied */
306}
307</pre>
308 <header><h4>HTML</h4></header>
309<pre>
310<p class="detail">This is a <span>paragraph</span> with the "detail" class.</p>
311<p>This is a <span>paragraph</span> without the "detail" class.</p>
312</pre>
313 <header><h4>HTML+CSS</h4></header>
314 <section class="sample-view">
315 <p class="detail">This is a <span>paragraph</span> with the "detail" class.</p>
316 <p>This is a <span>paragraph</span> without the "detail" class.</p>
317 </section>
318 </section>
319 </article>
320 <article>
321 <h3>:checked</h3>
322 <section>
323 <header><h4>CSS</h4></header>
324<pre class="sample">
325input:checked {
326 outline: 4px solid red;
327}
328</pre>
329 <header><h4>HTML</h4></header>
330<pre>
331 <label><input type="checkbox">First Checkbox</label>
332 <label><input type="checkbox">Second Checkbox</label>
333</pre>
334 <header><h4>HTML+CSS</h4></header>
335 <section class="sample-view">
336 <label><input type="checkbox">First Checkbox</label>
337 <label><input type="checkbox">Second Checkbox</label>
338 </section>
339 </section>
340 </article>
341 <article>
342 <h3>:disabled</h3>
343 <section>
344 <header><h4>CSS</h4></header>
345<pre class="sample">
346button:disabled {
347 background-color: gray;
348 cursor: not-allowed;
349}
350</pre>
351 <header><h4>HTML</h4></header>
352 <pre><button type="button" disabled>Example of active</button></pre>
353 <header><h4>HTML+CSS</h4></header>
354 <section class="sample-view">
355 <button type="button" disabled>Example of active</button>
356 </section>
357 </section>
358 </article>
359 <article>
360 <h3>:visited</h3>
361 <section>
362 <header><h4>CSS</h4></header>
363<pre class="sample">
364.link:visited {
365 color: purple;
366}
367</pre>
368 <header><h4>HTML</h4></header>
369 <pre><a href="https://example.com" class="link">Example</a></pre>
370 <header><h4>HTML+CSS</h4></header>
371 <section class="sample-view">
372 <a href="https://example.com" class="link">Example</a>
373 </section>
374 </section>
375 </article>
376 <article>
377 <h3>:first-of-type</h3>
378 <section>
379 <header><h4>CSS</h4></header>
380<pre class="sample">
381.first-example :first-of-type {
382 font-weight: bold;
383 color: steelblue;
384}
385</pre>
386 <header><h4>HTML</h4></header>
387<pre>
388<p class="first-example">
389 <var>First Variable</var>, <code>First Code Snippet</code><br>
390 <var>Second Variable</var>, <code>Second Code Snippet</code><br>
391 <var>Third Variable</var>, <code>Third Code Snippet</code>
392</p>
393</pre>
394 <header><h4>HTML+CSS</h4></header>
395 <section class="sample-view">
396 <p class="first-example">
397 <var>First Variable</var>, <code>First Code Snippet</code><br>
398 <var>Second Variable</var>, <code>Second Code Snippet</code><br>
399 <var>Third Variable</var>, <code>Third Code Snippet</code>
400 </p>
401 </section>
402 </section>
403 </article>
404 <article>
405 <h3>:last-of-type</h3>
406 <section>
407 <header><h4>CSS</h4></header>
408<pre class="sample">
409.last-example :last-of-type {
410 font-weight: bold;
411 color: seagreen;
412}
413</pre>
414 <header><h4>HTML</h4></header>
415<pre>
416<p class="last-example">
417 <var>First Variable</var>, <code>First Code Snippet</code><br>
418 <var>Second Variable</var>, <code>Second Code Snippet</code><br>
419 <var>Third Variable</var>, <code>Third Code Snippet</code>
420</p>
421</pre>
422 <header><h4>HTML+CSS</h4></header>
423 <section class="sample-view">
424 <p class="last-example">
425 <var>First Variable</var>, <code>First Code Snippet</code><br>
426 <var>Second Variable</var>, <code>Second Code Snippet</code><br>
427 <var>Third Variable</var>, <code>Third Code Snippet</code>
428 </p>
429 </section>
430 </section>
431 </article>
432 <article>
433 <h3>:nth-of-type(n)</h3>
434 <section>
435 <header><h4>CSS</h4></header>
436<pre class="sample">
437.nth-example :nth-of-type(2) {
438 font-weight: bold;
439 color: lightsalmon;
440}
441</pre>
442 <header><h4>HTML</h4></header>
443<pre>
444<p class="nth-example">
445 <var>First Variable</var>, <code>First Code Snippet</code><br>
446 <var>Second Variable</var>, <code>Second Code Snippet</code><br>
447 <var>Third Variable</var>, <code>Third Code Snippet</code>
448</p>
449</pre>
450 <header><h4>HTML+CSS</h4></header>
451 <section class="sample-view">
452 <p class="nth-example">
453 <var>First Variable</var>, <code>First Code Snippet</code><br>
454 <var>Second Variable</var>, <code>Second Code Snippet</code><br>
455 <var>Third Variable</var>, <code>Third Code Snippet</code>
456 </p>
457 </section>
458 </section>
459 </article>
460
461 <header><h2>Pseudo Element</h2></header>
462 <article>
463 <h3>::before</h3>
464 <section>
465 <header><h4>CSS</h4></header>
466<pre class="sample">
467span.note::before {
468 content: "Note: ";
469 color: red;
470}
471</pre>
472 <header><h4>HTML</h4></header>
473 <pre><span class="note">Example Text</span></pre>
474 <header><h4>HTML+CSS</h4></header>
475 <section class="sample-view">
476 <span class="note">Example Text</a><br>
477 </section>
478 </section>
479 </article>
480 <article>
481 <h3>::after</h3>
482 <section>
483 <header><h4>CSS</h4></header>
484<pre class="sample">
485span.five-stars::after {
486 content: "*****";
487 color: gold;
488}
489</pre>
490 <header><h4>HTML</h4></header>
491 <pre><span class="five-stars">Example Text</span></pre>
492 <header><h4>HTML+CSS</h4></header>
493 <section class="sample-view">
494 <span class="five-stars">Example Text</a>
495 </section>
496 </section>
497 </article>
498 <article>
499 <h3>::placeholder</h3>
500 <section>
501 <header><h4>CSS</h4></header>
502<pre class="sample">
503input::placeholder {
504 color: rgba(0, 150, 150, 0.7);
505 font-size: 14px;
506 font-style: italic;
507}
508</pre>
509 <header><h4>HTML</h4></header>
510 <pre><input type="text" placeholder="Enter your name"></pre>
511 <header><h4>HTML+CSS</h4></header>
512 <section class="sample-view">
513 <input type="text" placeholder="Enter your name">
514 </section>
515 </section>
516 </article>
517 <article>
518 <h3>::first-letter</h3>
519 <section>
520 <header><h4>CSS</h4></header>
521<pre class="sample">
522p.first::first-letter {
523 font-size: 2em;
524 color: blue;
525}
526</pre>
527 <header><h4>HTML</h4></header>
528<pre>
529<p class="first">
530 <span>First Text</span><br>
531 <span>Second Text</span><br>
532 <span>Third Text</span>
533</p>
534</pre>
535 <header><h4>HTML+CSS</h4></header>
536 <section class="sample-view">
537 <p class="first">
538 <span>First Text</span><br>
539 <span>Second Text</span><br>
540 <span>Third Text</span>
541 </p>
542 </section>
543<pre class="sample-error">
544/* Not Valid: `::first-letter` can only be applied to
545 block-level elements */
546span::first-letter {
547 font-size: 2em;
548 color: red;
549}
550</pre>
551 </section>
552 </article>
553 <article>
554 <h3>::first-line</h3>
555 <section>
556 <header><h4>CSS</h4></header>
557<pre class="sample">
558p.second::first-line {
559 font-weight: bold;
560 color: green;
561}
562</pre>
563 <header><h4>HTML</h4></header>
564<pre>
565<p class="second">
566 First Text<br>
567 Second Text<br>
568 Third Text
569</p>
570</pre>
571 <header><h4>HTML+CSS</h4></header>
572 <section class="sample-view">
573 <p class="second">
574 First Text<br>
575 Second Text<br>
576 Third Text
577 </p>
578 </section>
579<pre class="sample-error">
580/* Not Valid: `::first-line` can only be applied to block-level elements */
581span::first-line {
582 font-size: 2em;
583 color: red;
584}
585</pre>
586 </section>
587 </article>
588 <article>
589 <h3>::selection</h3>
590 <section>
591 <header><h4>CSS</h4></header>
592<pre class="sample">
593span::selection {
594 background-color: yellow;
595 color: black;
596}
597</pre>
598 <header><h4>HTML</h4></header>
599<pre>
600<p>
601 <span>First Text</span><br>
602 <span>Second Text</span><br>
603 <span>Third Text</span>
604</p>
605</pre>
606 <header><h4>HTML+CSS</h4></header>
607 <section class="sample-view">
608 <p>
609 <span>First Text</span><br>
610 <span>Second Text</span><br>
611 <span>Third Text</span>
612 </p>
613 </section>
614 </section>
615 </article>
616 </main>
617</body>
618</html>伪类
CSS伪类是根据特定条件(如元素状态或层次结构)为元素应用样式的函数。它们允许您根据元素状态或位置添加特殊样式,对于表达常规类或ID无法指定的动态情况非常有用。伪类使得根据特定条件实现动态UI和样式变得简单,允许设计响应用户交互或内容结构。
伪类的基本语法
伪类的基本语法如下。
1selector:pseudo-class {
2 /* Styles */
3}selector是目标元素。pseudo-class部分表示一个指示特定状态或条件的伪类。:用于选择伪类。
常用伪类
:hover
1a:hover {
2 color: red;
3}:hover当鼠标光标悬停在元素上时应用样式。- 在此示例中,当鼠标悬停在链接上时,文本颜色变为红色。
:focus
1input:focus {
2 outline: 2px solid blue;
3}:focus当元素获得焦点时应用样式。它主要用于表单元素和链接。- 在此示例中,当
input元素获得焦点时会出现蓝色轮廓。
:active
1button:active {
2 background-color: green;
3}:active当元素处于活动状态时(正在被点击时)应用样式。- 在此示例中,当按钮被点击时,背景色变为绿色。
:empty
1.box:empty {
2 background-color: lightgray;
3 border: 1px dashed black;
4 height: 50px;
5 width: 50px;
6}:empty伪类用于为没有子元素的元素应用样式。目标元素不能包含文本节点、空白或注释。- 在此示例中,如果具有
box类的元素为空,则会为该元素应用灰色背景和黑色虚线边框。如果存在空白,则不会应用样式。
:valid / :invalid
1input:valid {
2 border: 2px solid green;
3 background-color: #eaffea;
4}
5
6input:invalid {
7 border: 2px solid red;
8 background-color: #ffeaea;
9}-
:valid是应用于具有有效输入值的表单元素的样式。当符合由 HTML 属性(例如pattern或type(如email))指定的条件时,将应用:valid样式。 -
:invalid是应用于具有无效输入值的表单元素的样式。当表单元素不符合 HTML 验证规则(例如type="email")时,将应用:invalid样式。 -
在此示例中,如果输入的文本长度为 5 个或更多字符,则会显示绿色边框,这由
pattern属性指定。如果输入的文本长度少于 5 个字符,则会显示红色边框。
:in-range / :out-of-range
1input[type="number"]:in-range {
2 border: 2px solid blue;
3 background-color: #eaf4ff;
4}
5
6input[type="number"]:out-of-range {
7 border: 2px solid orange;
8 background-color: #fff4e6;
9}-
:in-range是当数值输入元素的值在指定范围内时应用的样式。:in-range样式会在 HTML 中满足min和max属性规定的条件时应用。 -
:out-of-range是当数值输入元素的值超出指定范围时应用的样式。:out-of-range样式会在指定的min或max属性范围之外时应用。 -
在此示例中,当输入的值在
min="18"和max="99"属性指定的范围内(包括 18 到 99)时,会显示蓝色边框。当输入的值超出范围时,会显示橙色边框。
:nth-child()
1li:nth-child(2) {
2 color: blue;
3 font-weight: bold;
4}:nth-child()根据子元素在其父元素中的位置应用样式。您可以为n指定一个数字或表达式。- 在此示例中,第二个列表项的文本变为蓝色。
应用于奇偶元素
1li:nth-child(odd) {
2 background-color: lightblue;
3}
4
5li:nth-child(even) {
6 background-color: steelblue;
7}- 通过指定
even或odd,您可以将样式应用于奇数或偶数元素。 - 在这个例子中,偶数行和奇数行应用了不同的背景颜色。例如,这可以用于条纹表格设计。
:first-child
1small:first-child {
2 font-weight: bold;
3 color: steelblue;
4}:first-child将样式应用于父元素的第一个子元素。- 在此示例中,第一个
small元素的文本被设置为加粗并呈现蓝色阴影。
:last-child
1small:last-child {
2 font-weight: bold;
3 color: seagreen;
4}:last-child将样式应用于父元素的最后一个子元素。- 在此示例中,最后一个
small元素的文本被设置为加粗并呈现绿色阴影。
:has()
1p:has(span.child) {
2 border: 2px solid black;
3}- CSS中的
:has()伪类充当父选择器。使用此伪类,您可以根据子元素的存在选择父元素。 - 在此示例中,
border应用于包含带有.child类的span元素的p元素。
:not()
1q:not(.quote2) {
2 font-weight: bold;
3 color: steelblue;
4}:not()将样式应用于不符合特定条件的元素。- 在此示例中,
q元素的文本(不包括具有quote2类的元素)被设置为加粗并呈现蓝色阴影。
:where() / :is()
1:where(p, .detail) span {
2 color: darkcyan;
3 text-decoration: line-through;
4}
5:is(p, .detail) span {
6 font-style: italic;
7}
8p span {
9 text-decoration: none; /* Applied */
10 font-style: normal; /* Not Applied */
11}:where()伪类是一个特殊的CSS选择器,它将特异性设置为零。当您想统一处理多个选择器并应用样式而不影响所选元素的特异性时,这非常有用。:is()伪类将多个选择器组合在一起,并选择符合任一条件的元素。:is()伪类本身具有与普通选择器相同的特异性,但会根据其内部选择器中最高的特异性应用样式。- 在此示例中,使用
:where()和:is()为detail类中的<p>和<span>元素应用样式。:where()设置的text-decoration样式会被后续的CSS覆盖。然而,由于:is()设置的样式具有更高的特异性,因此不会被后续的CSS覆盖。
:checked
1input:checked {
2 outline: 4px solid red;
3}:checked在复选框或单选按钮被选中时应用样式。- 在此示例中,选中的输入元素会显示红色边框。
:disabled
1button:disabled {
2 background-color: gray;
3 cursor: not-allowed;
4}:disabled将样式应用于被禁用的表单元素。- 在这个例子中,被禁用的按钮应用了灰色背景和“禁止”光标。
:visited
1.link:visited {
2 color: purple;
3}:visited用于为已访问的链接应用样式。- 在此示例中,点击并访问链接后,
<a>元素的文本颜色会变为紫色。
隐私和样式限制
:visited由于隐私保护原因存在一些样式限制。这样可以防止外部网站推断用户的访问历史。使用 :visited 可以更改的属性仅限于以下内容:。
color
background-color
border-color
outline-color
column-rule-color
text-decoration-color
fill
stroke另一方面,除了这些属性(如 display、visibility、position 等)之外的属性无法通过:visited进行更改。这样可以防止通过访问历史泄露用户隐私。
:first-of-type
1.first-example :first-of-type {
2 font-weight: bold;
3 color: steelblue;
4}:first-of-type选择父元素中第一个属于该类型(具有相同标签名称)的元素。- 在此示例中,
first-example类中的第一个<var>元素和<code>元素的文本被设置为蓝色加粗样式。
:last-of-type
1.last-example :last-of-type {
2 font-weight: bold;
3 color: seagreen;
4}:last-of-type选择父元素中最后一个属于该类型的元素。- 在此示例中,
last-example类中的最后一个<var>元素和<code>元素的文本被设置为绿色加粗样式。
:nth-of-type()
1.nth-example :nth-of-type(2) {
2 font-weight: bold;
3 color: lightsalmon;
4}:nth-of-type()将样式应用于同一类型的第 n 个出现的元素。- 在此示例中,
nth-example类中的第二个<var>元素和<code>元素的文本被设置为橙色加粗样式。
伪元素
在 CSS 中,伪元素用于对选定元素的特定部分应用样式。与伪类不同,伪元素关注元素的特定部分,而不是整个元素。例如,它们用于在元素之前或之后添加内容,或者应用样式到特定字符或行。
伪元素的基本语法
伪元素通常在选择器后使用 ::(双冒号)书写。这是为了将它们与伪类区分开来。
1selector::pseudo-element {
2 /* Styles */
3}selector是目标元素。pseudo-element部分表示一个引用元素特定部分或方面的伪元素。- 你可以通过使用
::来指定一个伪元素。
常用伪元素
::before
1span.note::before {
2 content: "Note: ";
3 color: red;
4}-
::before在元素的内容之前插入虚拟内容,并对该部分应用样式。例如,它对于插入图标或标签很有用。 -
在此示例中,带有类名
note的<span>元素前插入了文本“Note:”。这里指定的内容不存在于实际的 HTML 中;它是通过 CSS 虚拟添加的。这意味着您无法直接操作通过 JavaScript 等添加的内容。
::after
1span.five-stars::after {
2 content: "*****";
3 color: gold;
4}::after在元素的内容之后插入虚拟内容。通常用于在元素后添加补充或装饰内容。- 在此示例中,一个金色星星图标被添加到具有
five-stars类的<span>元素之后。
::placeholder
1input::placeholder {
2 color: rgba(0, 150, 150, 0.7);
3 font-size: 14px;
4 font-style: italic;
5}::placeholder伪元素用于设置表单元素中占位符文本的样式。默认情况下,占位符文本通常以浅灰色显示,但通过使用::placeholder伪元素,您可以更改其样式以匹配您的设计。- 在此示例中,
input元素的占位符文本颜色设置为 70% 透明的蓝色,字体大小为14px,并设为斜体。
::first-letter
1p.first::first-letter {
2 font-size: 2em;
3 color: blue;
4}::first-letter应用于块级元素的首字母样式。它用于像杂志或报纸中看到的排版效果,其中首字母被强调。请注意,此伪元素仅对块级元素生效,不能直接应用于像<span>这样的行内元素。- 在此示例中,段落的首字母将设置为两倍大小,并加粗且为蓝色。
::first-line
1p.second::first-line {
2 font-weight: bold;
3 color: green;
4}::first-line用于设置块级元素的首行样式。它特别适用于文本段落,允许您为首行应用不同的样式。- 在此示例中,段落的首行将设置为绿色加粗样式。
::selection
1span::selection {
2 background-color: yellow;
3 color: black;
4}::selection用于设置用户选择(高亮)文本部分的样式。通过此设置,您可以更改文本被选中时的背景颜色和文字颜色。- 在此示例中,当
<span>元素的文本被选中时,背景变为黄色,文字变为黑色。
您可以在我们的YouTube频道上使用Visual Studio Code跟随上述文章进行学习。 请也查看我们的YouTube频道。