기술나눔

::CSS의 의사 요소 앞과 ::뒤

2024-07-12

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

목차

1. CSS 의사 요소

2. ::전 ::후 소개

1、::전에

2、::후에

3. 콘텐츠의 공통 속성값

3. ::이전::이후 적용 시나리오

1. 통일된 캐릭터 설정

2. 배경을 통해 사진 추가

3. 장식선 추가

4. 오른쪽 화살표 확장

5. 대화 삼각형

6. 아이콘 아이콘 삽입


1. CSS 의사 요소

CSS 의사 요소는 CSS의 일부 특수 선택기를 사용하여 생성된 가상 요소를 나타내며 실제 존재하는 HTML 요소는 아닙니다.

  • 일부 특수 효과를 얻기 위해 문서의 특정 부분을 선택하고 조작하는 데 사용됩니다.
  • 의사 요소를 사용하면 추가 HTML 태그를 추가하지 않고도 문서의 콘텐츠 스타일을 지정할 수 있습니다.
  • 의사 요소는 요소의 레이아웃, 위치, 너비, 높이, 배경 등을 포함하여 HTML에서 지원되는 다양한 속성을 설정할 수 있는 요소이기도 합니다.

이 기사에서는 주로 두 가상 요소::before 및 ::after의 관련 내용과 일부 사용 시나리오를 소개합니다.

2. ::전 ::후 소개

::before ::after 의사 요소는 요소 앞이나 뒤에 지정된 내용을 삽입하는 데 사용됩니다.

  • 삽입할 콘텐츠를 지정하려면 content 속성을 사용하세요.
  • content 속성과 함께 사용해야 합니다., 콘텐츠 속성 값은 비어 있을 수 있습니다.
  • 의사 요소의 표시 속성 값은 기본적으로 인라인으로 설정됩니다.

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 속성과 함께 사용해야 합니다. 다음은 콘텐츠의 공통 속성 값입니다.

일련번호속성 값설명하다
1텍스트 내용을 설정합니다.
2url("url")사진과 같은 미디어 파일에 대한 URL 링크를 설정합니다.
3오픈-쿼트주요 인용문으로 설정합니다.
4닫기-인용문역따옴표로 설정합니다.
5속성(기인하다)요소의 속성 속성을 문자열로 반환합니다.
6카운터카운터를 설정합니다.
7없음콘텐츠를 null 값으로 설정합니다.
8정상:before 및 :after 의사 클래스 요소에서는 없음으로 간주됩니다. 즉, null 값이기도 합니다.

(1) 텍스트 내용 설정

의사 요소에 텍스트를 추가하려면 컨텐츠 속성 값을 문자열 유형으로 설정하십시오.

  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) || 앞에 따옴표를 설정합니다.

여는 따옴표 또는 닫는 따옴표 속성 값을 통해 의사 요소의 내용을 앞따옴표 또는 뒷따옴표로 설정할 수 있습니다.

  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를 사용하는 것은 매우 간단하지만 유연하게 적용할 수 있다면 아주 좋은 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 팁~!