技術共有

CSS の ::before および ::after 疑似要素

2024-07-12

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

目次

1.CSS疑似要素

2. 導入前 :: 導入後

1、::前

2、::後

3. コンテンツの共通属性値

3. ::before ::after アプリケーション シナリオ

1.統一文字を設定する

2. 背景を通して写真を追加する

3. 飾り線を入れる

4. 右側の矢印を展開します

5. ダイアログトライアングル

6. アイコンの挿入


1.CSS疑似要素

CSS 疑似要素は、CSS の特別なセレクターを使用して作成された仮想要素を指し、実際の既存の HTML 要素ではありません。

  • 文書の特定の部分を選択して操作して、特殊効果を実現するために使用されます。
  • 疑似要素を使用すると、追加の HTML タグを追加せずにドキュメント内のコンテンツのスタイルを設定できます。
  • 疑似要素は、要素のレイアウト、位置、幅、高さ、背景など、HTML でサポートされているさまざまな属性を設定できる要素でもあります。

この記事では主に、2 つの疑似要素::before と ::after の関連コンテンツといくつかの使用シナリオを紹介します。

2. 導入前 :: 導入後

::before ::after 擬似要素は、要素の前または後に指定されたコンテンツを挿入するために使用されます。

  • content 属性を使用して、挿入するコンテンツを指定します。
  • content 属性と一緒に使用する必要があります、content 属性値は空にすることができます。
  • 疑似要素の表示属性値のデフォルトは inline

1、::前

::before セレクターは、指定された要素の前にコンテンツを挿入するために使用されます。

(1) 文法

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

(2) 例

ページ上のすべての p 要素の前にコンテンツを挿入します。

  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 セレクターは、指定された要素の後にコンテンツを挿入するために使用されます。

(1) 文法

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

(2) 例

ページ上のすべての p 要素の後にコンテンツを挿入します。

  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. コンテンツの共通属性値

::before ::after は content 属性と一緒に使用する必要があります。 content の一般的な属性値は次のとおりです。

シリアルナンバー属性値説明する
1テキストコンテンツを設定します。
2url("url")写真などのメディア ファイルの URL リンクを設定します。
3引用符を開く先頭の引用符として設定します。
4引用終了逆引用符に設定します。
5属性(属性)要素の属性属性を文字列として返します。
6カウンターカウンターを設定します。
7なしコンテンツを null 値に設定します。
8普通:before および :after 擬似クラス要素では、none とみなされます。つまり、null 値でもあります。

(1) テキスト内容を設定する

content 属性値を文字列タイプに設定して、擬似要素にテキストを追加します。

  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) メディアリンクの設定

url() 属性値を使用して、メディア ファイルのコンテンツを疑似要素としてインポートできます。

  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>

URL を使用して追加される画像のサイズは設定できないことに注意してください。画像は背景を通して追加することをお勧めします。

(3) || の前に引用符を置きます。

open-quote または close-quote 属性値を使用して、疑似要素の内容を前引用符または後ろ引用符に設定できます。

  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) 要素の属性を取得する

attr() を通じて要素の属性値を取得し (文字列の形式で返されます)、それを擬似要素のコンテンツとして設定します。

  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) カウンタの設定

  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 アプリケーション シナリオ

これら 2 つの疑似要素::before ::after の使用は非常に簡単ですが、柔軟に適用できれば、非常に優れた CSS 効果を実現できます。

1.統一文字を設定する

  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. 背景を通して写真を追加する

  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. 飾り線を入れる

  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. 右側の矢印を展開します

  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. ダイアログトライアングル

  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. アイコンの挿入

  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>

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

毎日少しずつ進歩していきましょう~!

実践的なCSSのヒント〜!