[title]定义[/title]

伪元素用于创建一些不在文档树中的元素,并为其添加样式。比如说,我们可以通过:before :after来在一个元素前、后增加一些文本,并为这些文本添加样式。虽然用户可以看到这些文本,但是这些文本实际上不在文档树中。

[title]伪元素的单双冒号[/title]

在CSS2之前规范不明确的时,伪元素使用单冒号(:)来表示;

在CSS3规范中的要求使用双冒号(::)表示伪元素,以此来区分伪元素和伪类;但为了向上兼容,现在用单冒号(:)也可以的。

[title]使用场景演示[/title]

1.用伪元素插入文字

h1:before {
 content:"你好";  /* 在标题前面插入文字 并设置样式 */ 
 color: pink;
 font-size: 20px;
 width: 40px;
 height: 40px;  
}

<h1>我是标题</h1>

效果如下:

 

2.插入iconfont字体库

@font-face {
    font-family: 'iconfont';
    src: url('http://at.alicdn.com/t/font_1274897_t3pj4anz7hg.eot');
    src: url('http://at.alicdn.com/t/font_1274897_t3pj4anz7hg.eot?#iefix') format('embedded-opentype'),
        url('http://at.alicdn.com/t/font_1274897_t3pj4anz7hg.woff2') format('woff2'),
        url('http://at.alicdn.com/t/font_1274897_t3pj4anz7hg.woff') format('woff'),
        url('http://at.alicdn.com/t/font_1274897_t3pj4anz7hg.ttf') format('truetype'),
        url('http://at.alicdn.com/t/font_1274897_t3pj4anz7hg.svg#iconfont') format('svg');
}

h1:before {
    font-family: "iconfont" !important;
    /* 一定要加 */
    content: "\e601";
    color: pink;
    font-size: 20px;
    width: 40px;
    height: 40px;
}

<h1>我是标题</h1>

根据效果图可以看见伪元素是inline元素类型

3.清除浮动

可解决浮动导致的父元素高度塌陷问题

.clear:after {
     content: "";
     display: block;
     clear: both;
 }
 ul{
     background:pink;
 }
 li{
     float:left;
     margin:0 20px;
}
 <ul class="clear">
     <li>hello world</li>
     <li>hello world</li>
     <li>hello world</li>
     <li>hello world</li>
 </ul>

4.分割线效果

p:before{
    content:'';
    display:inline-block;
    width:100px;
    height:2px;
    background:pink;
    margin:5px;
 }
 p:after{
    content:'';
    display:inline-block;
    width:100px;
    height:2px;
    background:pink;
    margin:5px;
 }


<p>分割线</p>

效果图:

5.对话框气泡效果

div {
    width: 180px;
    height: 36px;
    line-height: 36px;
    background: #c0eeff;
    border-radius: 5px;
    font-size: 14px;
    text-align: center;
    position: relative;
}

div:before {
    content: "";
    position: absolute;
    top: 10px;
    left: -14px;
    /* 画三角形 */
    width: 0;
    height: 0;
    border-top: 10px solid transparent;
    border-right: 20px solid #c0eeff;
    border-bottom: 10px solid transparent;
    border-left: 0 solid #c0eeff;

}


<div>快来和我聊天吧~</div>

效果图:

6.相片层叠效果

<style type="text/css">
            div {
                width: 400px;
                height: 300px;
                border: 8px solid #eee;
                position: relative;
                margin-top: 50px;
                margin-left: 50px;
            }
            
            div img {
                width: 100%;
                height: 100%;
            }
            
            div:before,
            div:after {
                content: "";
                position: absolute;
                top: 0;
                left: 0;
                z-index: -1;
                width: 400px;
                height: 300px;
                border: 8px solid #eee;
                transform: rotate(8deg);
            }
            
            div:after {
                transform: rotate(-8deg);
            }
        </style>
    </head>
    <body>
        <div>
            <img src="https://gitee.com/ay8yt/imageStore/raw/master/%E4%BA%8C%E9%98%B6%E6%AE%B5%E5%A4%A7%E7%BA%B2/%E5%9B%BE%E7%89%87/QQ%E6%88%AA%E5%9B%BE20200417104911.png">
        </div>

 

效果图:

[title]总结[/title]

使用伪元素可实现非常多的效果,也可减少网页中的标签的使用,应善于运用

文章转载自:https://zhuanlan.zhihu.com/p/132370231

 

发表回复