Technology Sharing

::before and ::after pseudo-elements in CSS

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Table of contents

1. CSS Pseudo-elements

2. Introduction to ::before ::after

1、::before

2、::after

3. Common attribute values ​​of content

3. ::before ::after application scenarios

1. Set unified characters

2. Add pictures via background

3. Add decorative lines

4. Expand arrow on the right

5. Dialog box small triangle

6. Insert icon


1. CSS Pseudo-elements

CSS pseudo-elements are virtual elements created by using some special selectors in CSS. They are not actual HTML elements.

  • Used to select and operate specific parts of the document to achieve some special effects;
  • Pseudo-elements allow you to style the content in a document without adding extra HTML tags.
  • Pseudo-elements are also a type of element that can set various attributes supported in HTML, including element layout, positioning, width, height, background, etc.

This article mainly introduces the relevant content and some usage scenarios of the two pseudo-elements::before and::after;

2. Introduction to ::before ::after

::before ::after pseudo-elements are used to insert specified content before or after an element;

  • Use the content attribute to specify the content to be inserted;
  • Must be used with the content attribute, the attribute value of content can be empty;
  • The default value of the display attribute of the pseudo-element is inline

1、::before

The ::before selector is used to insert content before the specified element;

(1) Syntax

  1. 元素::before{
  2. content: "要插入的内容";
  3. /* 其他属性 */
  4. }

(2) Example

Insert content before all p elements on the page;

  1. <style>
  2. p::before{
  3. content: "使用::before伪元素插入的内容——";
  4. /* 其他属性 */
  5. }
  6. </style>
  7. <body>
  8. <div>
  9. <p>第一个P标签中的内容</p>
  10. <p>第二个P标签中的内容</p>
  11. <p>第三个P标签中的内容</p>
  12. </div>
  13. </body>

2、::after

The ::after selector is used to insert content after the specified element;

(1) Syntax

  1. 元素::after{
  2. content: "要插入的内容";
  3. /* 其他属性 */
  4. }

(2) Example

Insert content after all p elements on the page;

  1. <style>
  2. p::after{
  3. content: "——使用::after伪元素插入的内容";
  4. /* 其他属性 */
  5. }
  6. </style>
  7. <body>
  8. <div>
  9. <p>第一个P标签中的内容</p>
  10. <p>第二个P标签中的内容</p>
  11. <p>第三个P标签中的内容</p>
  12. </div>
  13. </body>

3. Common attribute values ​​of content

::before ::after must be used together with the content attribute. The following are common attribute values ​​of content:

Serial numberProperty Valueillustrate
1stringSet the text content;
2url("url")Set URL links for media files such as images;
3open-quoteSet to the leading quote;
4close-quoteSet to back quote;
5attr(attribute)Return the attribute of the element as a string;
6counterSet the counter;
7noneSet content to an empty value;
8normalIn the :before and :after pseudo-class elements, it will be regarded as none, that is, it is also an empty value;

(1) Set the text content

Set the content attribute value to string type to add text to the pseudo-element;

  1. <style>
  2. span::before{
  3. content: "使用::before添加的文本前缀——————";
  4. }
  5. span::after{
  6. content: "————使用::after添加的文本后缀";
  7. }
  8. </style>
  9. ......
  10. <body>
  11. <span class="box">我是HTML元素中的文本</span>
  12. </body>

(2) Set up media links

Through the url() attribute value, you can import the media file as the content of the pseudo element;

  1. <style>
  2. .container {
  3. margin: 100px;
  4. }
  5. .avatar::after{
  6. content: url("D:\test\girl.png");
  7. display: block;
  8. }
  9. </style>
  10. ......
  11. <body>
  12. <div class="container">
  13. <div class="avatar">示例图片</div>
  14. </div>
  15. </body>

Note that the image added using the URL cannot be resized. It is best to add the image through the background.

(3) Set the leading|| trailing quotation marks

Through the open-quote or close-quote attribute value, you can set the content of the pseudo-element to the opening or closing quotation marks;

  1. <style>
  2. p:nth-child(1)::before{
  3. content:open-quote;
  4. /* 其他属性 */
  5. }
  6. p:nth-child(2)::after{
  7. content:close-quote;
  8. }
  9. </style>
  10. ......
  11. <body>
  12. <div>
  13. <p>添加前引号</p>
  14. <p>添加后引号</p>
  15. </div>
  16. </body>

(4) Get element attributes

Get an attribute value of the element through attr() (returned as a string) and set it as the content of the pseudo-element;

  1. <style>
  2. a:after {
  3. content: " (" attr(href) ")";
  4. }
  5. </style>
  6. ......
  7. <body>
  8. <div><a href="https://www.csdn.net">CSDN</a>点击跳转至CSDN...</div>
  9. <div><a href="https://www.baidu.com">百度</a>点击跳转至百度...</div>
  10. </body>

(5) Set the counter

  1. <style>
  2. div {
  3. counter-increment: index;
  4. }
  5. div:before {
  6. content:counter(index);
  7. }
  8. </style>
  9. ......
  10. <body>
  11. <div>、、、、、、我是第1个div、、、、、、</div>
  12. <div>、、、、、、我是第2个div、、、、、、</div>
  13. <div>、、、、、、我是第3个div、、、、、、</div>
  14. <div>、、、、、、我是第4个div、、、、、、</div>
  15. </body>

3. ::before ::after application scenarios

Although the usage of the two pseudo-elements ::before ::after is very simple, if you can use them flexibly, you can achieve some very good CSS effects;

1. Set unified characters

  1. <style>
  2. p::before{
  3. content: "* ";
  4. color: red;
  5. font-size: 24px;
  6. /* 其他属性 */
  7. }
  8. p::after{
  9. content: ":____________";
  10. /* 其他属性 */
  11. }
  12. </style>
  13. ...
  14. <body>
  15. <div>
  16. <p>姓名</p>
  17. <p>年龄</p>
  18. <p>出生日期</p>
  19. <p>居住地址</p>
  20. </div>
  21. </body>

2. Add pictures via background

  1. <style>
  2. .container{
  3. margin: 100px;
  4. }
  5. .container::after{
  6. content: "";
  7. display:block;
  8. width: 260px;
  9. height: 260px;
  10. background-image: url("D:\test\girl.png");
  11. background-position: center;
  12. background-size: cover;
  13. }
  14. </style>
  15. ......
  16. <body>
  17. <div class="container">通过背景添加图片</div>
  18. </body>

3. Add decorative lines

  1. <style>
  2. .line{
  3. display: flex;
  4. align-items: center;
  5. margin: 60px;
  6. height: 40px;
  7. font-size: 18px;
  8. }
  9. .line::before, .line::after{
  10. content: "";
  11. width: 300px;
  12. border-top: 6px double;
  13. margin: 5px;
  14. }
  15. </style>
  16. ......
  17. <body>
  18. <div class="line">添加装饰线</div>
  19. </body>

4. Expand arrow on the right

  1. <style>
  2. .container{
  3. display: flex;
  4. flex-direction: column;
  5. align-items: center;
  6. justify-content: center;
  7. width: 400px;
  8. margin: 100px auto;
  9. padding: 30px 0;
  10. border-radius: 8px;
  11. box-shadow: 0 0 4px 1px #acacac;
  12. }
  13. .setting-item{
  14. position: relative;
  15. align-items: center;
  16. display: flex;
  17. width: 300px;
  18. height: 40px;
  19. margin-bottom: 20px;
  20. border-bottom: 1px solid #ccc;
  21. }
  22. .setting-item::after{
  23. position: absolute;
  24. right: 0;
  25. content: "";
  26. width: 8px;
  27. height: 8px;
  28. border-top: 1px solid #666;
  29. border-right: 1px solid #666;
  30. transform: rotate(45deg);
  31. }
  32. </style>
  33. ......
  34. <body>
  35. <div class="container">
  36. <div class="setting-item">账号设置</div>
  37. <div class="setting-item">权限管理</div>
  38. <div class="setting-item">相关服务</div>
  39. <div class="setting-item">帮助与反馈</div>
  40. <div class="setting-item">......</div>
  41. </div>
  42. </body>

5. Dialog box small triangle

  1. <style>
  2. .container {
  3. width: 400px;
  4. margin: 100px auto;
  5. padding: 30px 0;
  6. border-radius: 8px;
  7. box-shadow: 0 0 4px 1px yellowgreen;
  8. }
  9. .left-box,.right-box {
  10. display: flex;
  11. }
  12. .right-box {
  13. justify-content: end;
  14. }
  15. span {
  16. position: relative;
  17. display: flex;
  18. align-items: center;
  19. background-color: yellowgreen;
  20. border-radius: 6px;
  21. margin: 4px 14px;
  22. padding: 16px;
  23. }
  24. .left-box span::before, .right-box span::after{
  25. position: absolute;
  26. content: "";
  27. width: 12px;
  28. height: 12px;
  29. background-color: yellowgreen;
  30. transform: rotate(45deg);
  31. }
  32. .left-box span::before{
  33. left: -6px;
  34. }
  35. .right-box span::after {
  36. right: -6px;
  37. }
  38. </style>
  39. ......
  40. <body>
  41. <div class="container">
  42. <div class="left-box">
  43. <span>Nice to meet you!</span>
  44. </div>
  45. <div class="right-box">
  46. <span>Nice to meet you, too!</span>
  47. </div>
  48. </div>
  49. </body>

6. Insert icon

  1. <style>
  2. .login-box{
  3. display: flex;
  4. flex-direction: column;
  5. align-items: center;
  6. justify-content: center;
  7. width: 400px;
  8. height: 400px;
  9. margin: 100px auto;
  10. border-radius: 8px;
  11. box-shadow: 0 0 4px 1px #acacac;
  12. }
  13. .title{
  14. font-size: 24px;
  15. font-weight: 700;
  16. margin-bottom: 40px;
  17. }
  18. .account, .pwd, .login-btn, .forgot-pwd{
  19. width: 300px;
  20. height: 40px;
  21. line-height: 40px;
  22. }
  23. .account, .pwd{
  24. display: flex;
  25. align-items: center;
  26. border-bottom: 1px solid #ccc;
  27. font-size: 14px;
  28. color: #888;
  29. }
  30. .pwd{
  31. margin-top: 20px;
  32. }
  33. .account::before, .pwd::before{
  34. content: '';
  35. display: inline-block;
  36. width: 24px;
  37. height: 24px;
  38. background-repeat: no-repeat;
  39. background-position: center center;
  40. background-size: contain;
  41. margin-right: 8px;
  42. }
  43. .account::before{
  44. background-image: url("D:\test\user.svg");
  45. }
  46. .pwd::before {
  47. background-image: url("D:\test\pwd.svg");
  48. }
  49. .login-btn{
  50. text-align: center;
  51. color: #fff;
  52. font-size: 16px;
  53. font-weight: 700;
  54. background: #2687F0;
  55. border-radius: 5px;
  56. margin-top: 40px;
  57. }
  58. .forgot-pwd{
  59. text-align: right;
  60. font-size: 14px;
  61. color: #888;
  62. margin-top: 20px;
  63. }
  64. </style>
  65. ......
  66. <body>
  67. <div class="login-box">
  68. <div class="title">XXX 管理系统</div>
  69. <div class="account">请输入账号</div>
  70. <div class="pwd">请输入密码</div>
  71. <div class="login-btn">登 录</div>
  72. <div class="forgot-pwd">忘记密码</div>
  73. </div>
  74. </body>

=========================================================================

Make a little progress every day~!

A practical CSS trick~!