내 연락처 정보
우편메소피아@프로톤메일.com
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
목차
CSS 의사 요소는 CSS의 일부 특수 선택기를 사용하여 생성된 가상 요소를 나타내며 실제 존재하는 HTML 요소는 아닙니다.
이 기사에서는 주로 두 가상 요소::before 및 ::after의 관련 내용과 일부 사용 시나리오를 소개합니다.
::before ::after 의사 요소는 요소 앞이나 뒤에 지정된 내용을 삽입하는 데 사용됩니다.
::before 선택기는 지정된 요소 앞에 콘텐츠를 삽입하는 데 사용됩니다.
(1) 문법
- 元素::before{
- content: "要插入的内容";
- /* 其他属性 */
- }
(2) 예
페이지의 모든 p 요소 앞에 콘텐츠를 삽입합니다.
- <style>
- p::before{
- content: "使用::before伪元素插入的内容——";
- /* 其他属性 */
- }
- </style>
- <body>
- <div>
- <p>第一个P标签中的内容</p>
- <p>第二个P标签中的内容</p>
- <p>第三个P标签中的内容</p>
- </div>
- </body>
::after 선택자는 지정된 요소 뒤에 콘텐츠를 삽입하는 데 사용됩니다.
(1) 문법
- 元素::after{
- content: "要插入的内容";
- /* 其他属性 */
- }
(2) 예
페이지의 모든 p 요소 뒤에 콘텐츠를 삽입합니다.
- <style>
- p::after{
- content: "——使用::after伪元素插入的内容";
- /* 其他属性 */
- }
- </style>
- <body>
- <div>
- <p>第一个P标签中的内容</p>
- <p>第二个P标签中的内容</p>
- <p>第三个P标签中的内容</p>
- </div>
- </body>
::before ::after는 content 속성과 함께 사용해야 합니다. 다음은 콘텐츠의 공통 속성 값입니다.
일련번호 | 속성 값 | 설명하다 |
---|---|---|
1 | 끈 | 텍스트 내용을 설정합니다. |
2 | url("url") | 사진과 같은 미디어 파일에 대한 URL 링크를 설정합니다. |
3 | 오픈-쿼트 | 주요 인용문으로 설정합니다. |
4 | 닫기-인용문 | 역따옴표로 설정합니다. |
5 | 속성(기인하다) | 요소의 속성 속성을 문자열로 반환합니다. |
6 | 카운터 | 카운터를 설정합니다. |
7 | 없음 | 콘텐츠를 null 값으로 설정합니다. |
8 | 정상 | :before 및 :after 의사 클래스 요소에서는 없음으로 간주됩니다. 즉, null 값이기도 합니다. |
(1) 텍스트 내용 설정
의사 요소에 텍스트를 추가하려면 컨텐츠 속성 값을 문자열 유형으로 설정하십시오.
- <style>
- span::before{
- content: "使用::before添加的文本前缀——————";
- }
- span::after{
- content: "————使用::after添加的文本后缀";
- }
- </style>
- ......
- <body>
- <span class="box">我是HTML元素中的文本</span>
- </body>
(2) 미디어 링크 설정
url() 속성 값을 통해 의사 요소인 미디어 파일의 내용을 가져올 수 있습니다.
- <style>
- .container {
- margin: 100px;
- }
- .avatar::after{
- content: url("D:\test\girl.png");
- display: block;
- }
- </style>
- ......
- <body>
- <div class="container">
- <div class="avatar">示例图片</div>
- </div>
- </body>
URL을 사용하여 추가된 이미지의 크기는 설정할 수 없습니다. 배경을 통해 이미지를 추가하는 것이 가장 좋습니다.
(3) || 앞에 따옴표를 설정합니다.
여는 따옴표 또는 닫는 따옴표 속성 값을 통해 의사 요소의 내용을 앞따옴표 또는 뒷따옴표로 설정할 수 있습니다.
- <style>
- p:nth-child(1)::before{
- content:open-quote;
- /* 其他属性 */
- }
- p:nth-child(2)::after{
- content:close-quote;
- }
- </style>
- ......
- <body>
- <div>
- <p>添加前引号</p>
- <p>添加后引号</p>
- </div>
- </body>
(4) 요소 속성 가져오기
attr()을 통해 요소의 속성 값을 얻고(문자열 형식으로 반환됨) 이를 의사 요소의 콘텐츠로 설정합니다.
- <style>
- a:after {
- content: " (" attr(href) ")";
- }
- </style>
- ......
- <body>
- <div><a href="https://www.csdn.net">CSDN</a>点击跳转至CSDN...</div>
- <div><a href="https://www.baidu.com">百度</a>点击跳转至百度...</div>
- </body>
(5) 카운터 설정
- <style>
- div {
- counter-increment: index;
- }
- div:before {
- content:counter(index);
- }
- </style>
- ......
- <body>
- <div>、、、、、、我是第1个div、、、、、、</div>
- <div>、、、、、、我是第2个div、、、、、、</div>
- <div>、、、、、、我是第3个div、、、、、、</div>
- <div>、、、、、、我是第4个div、、、、、、</div>
- </body>
이 두 가상 요소::before ::after를 사용하는 것은 매우 간단하지만 유연하게 적용할 수 있다면 아주 좋은 CSS 효과를 얻을 수 있습니다.
- <style>
- p::before{
- content: "* ";
- color: red;
- font-size: 24px;
- /* 其他属性 */
- }
- p::after{
- content: ":____________";
- /* 其他属性 */
- }
- </style>
- ...
- <body>
- <div>
- <p>姓名</p>
- <p>年龄</p>
- <p>出生日期</p>
- <p>居住地址</p>
- </div>
- </body>
- <style>
- .container{
- margin: 100px;
- }
- .container::after{
- content: "";
- display:block;
- width: 260px;
- height: 260px;
- background-image: url("D:\test\girl.png");
- background-position: center;
- background-size: cover;
- }
- </style>
- ......
- <body>
- <div class="container">通过背景添加图片</div>
- </body>
- <style>
- .line{
- display: flex;
- align-items: center;
- margin: 60px;
- height: 40px;
- font-size: 18px;
- }
- .line::before, .line::after{
- content: "";
- width: 300px;
- border-top: 6px double;
- margin: 5px;
- }
-
- </style>
- ......
- <body>
- <div class="line">添加装饰线</div>
- </body>
- <style>
- .container{
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
-
- width: 400px;
- margin: 100px auto;
- padding: 30px 0;
- border-radius: 8px;
- box-shadow: 0 0 4px 1px #acacac;
- }
-
- .setting-item{
- position: relative;
- align-items: center;
- display: flex;
- width: 300px;
- height: 40px;
- margin-bottom: 20px;
- border-bottom: 1px solid #ccc;
- }
-
- .setting-item::after{
- position: absolute;
- right: 0;
- content: "";
- width: 8px;
- height: 8px;
- border-top: 1px solid #666;
- border-right: 1px solid #666;
- transform: rotate(45deg);
- }
-
- </style>
- ......
- <body>
- <div class="container">
- <div class="setting-item">账号设置</div>
- <div class="setting-item">权限管理</div>
- <div class="setting-item">相关服务</div>
- <div class="setting-item">帮助与反馈</div>
- <div class="setting-item">......</div>
- </div>
- </body>
- <style>
- .container {
- width: 400px;
- margin: 100px auto;
- padding: 30px 0;
- border-radius: 8px;
- box-shadow: 0 0 4px 1px yellowgreen;
- }
-
- .left-box,.right-box {
- display: flex;
- }
-
- .right-box {
- justify-content: end;
- }
-
- span {
- position: relative;
- display: flex;
- align-items: center;
-
- background-color: yellowgreen;
- border-radius: 6px;
- margin: 4px 14px;
- padding: 16px;
- }
-
- .left-box span::before, .right-box span::after{
- position: absolute;
- content: "";
- width: 12px;
- height: 12px;
- background-color: yellowgreen;
- transform: rotate(45deg);
- }
-
- .left-box span::before{
- left: -6px;
- }
- .right-box span::after {
- right: -6px;
- }
- </style>
-
- ......
-
- <body>
- <div class="container">
- <div class="left-box">
- <span>Nice to meet you!</span>
- </div>
- <div class="right-box">
- <span>Nice to meet you, too!</span>
- </div>
- </div>
- </body>
- <style>
- .login-box{
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
-
- width: 400px;
- height: 400px;
- margin: 100px auto;
- border-radius: 8px;
- box-shadow: 0 0 4px 1px #acacac;
- }
- .title{
- font-size: 24px;
- font-weight: 700;
- margin-bottom: 40px;
- }
- .account, .pwd, .login-btn, .forgot-pwd{
- width: 300px;
- height: 40px;
- line-height: 40px;
- }
-
- .account, .pwd{
- display: flex;
- align-items: center;
- border-bottom: 1px solid #ccc;
- font-size: 14px;
- color: #888;
- }
- .pwd{
- margin-top: 20px;
- }
- .account::before, .pwd::before{
- content: '';
- display: inline-block;
- width: 24px;
- height: 24px;
- background-repeat: no-repeat;
- background-position: center center;
- background-size: contain;
- margin-right: 8px;
- }
- .account::before{
- background-image: url("D:\test\user.svg");
- }
- .pwd::before {
- background-image: url("D:\test\pwd.svg");
- }
-
- .login-btn{
- text-align: center;
- color: #fff;
- font-size: 16px;
- font-weight: 700;
- background: #2687F0;
- border-radius: 5px;
- margin-top: 40px;
- }
-
- .forgot-pwd{
- text-align: right;
- font-size: 14px;
- color: #888;
- margin-top: 20px;
- }
- </style>
- ......
- <body>
- <div class="login-box">
- <div class="title">XXX 管理系统</div>
- <div class="account">请输入账号</div>
- <div class="pwd">请输入密码</div>
- <div class="login-btn">登 录</div>
- <div class="forgot-pwd">忘记密码</div>
- </div>
- </body>
=========================================================================
매일 조금씩 발전해보세요~!
실용적인 CSS 팁~!