:before和:after的用法

css中的伪类:before和:after,意思是在指定元素的前后增加一些指定的内容,在这里,我主要介绍元素前后画线的效果。

1、元素下画线

css如下:

.flex-wrap { 
    display: -webkit-flex; 
    display: -ms-flexbox; 
    display: -webkit-box; 
    display: -ms-box; 
    display: box; 
    display: flex; 
}
.flex-con { 
    -webkit-box-flex: 1; 
    -ms-box-flex: 1; 
    -webkit-flex: 1; 
    -ms-flex: 1; 
    box-flex: 1; 
    flex: 1; 
}
.flex-vertical { 
    -webkit-box-orient: vertical; 
    -webkit-flex-direction: column; 
    -ms-flex-direction: column; 
    -webkit-flex-flow: column; 
    -flex-direction: column; 
    flex-flow: column; 
}
.tab-wrap{
    height:60px;
    text-align: center;
}
.tab-wrap .tab-item{
    height:60px;
}
.tab-wrap .tab-item span{
    display: inline-block;
    height:60px;
    line-height:60px;
    font-size:16px;
    color:#666;
    position:relative;
}
.tab-wrap .tab-item.active span{
    color:red;
}
.tab-wrap .tab-item.active span:after{
    content:'';
    width:100%;
    height:4px;
    background-color:red;
    position:absolute;
    bottom:4px;
    left:0;
}

html如下:

<div class="tab-wrap flex-wrap">

  <div class="tab-item flex-con active">

    <span>分类1</span>

  </div>
  <div class="tab-item flex-con active">

    <span>分类2</span>

  </div>

  <div class="tab-item flex-con">

    <span>分类3</span>

  </div>

</div>

效果如下:

image

2、元素左右画线
css如下:


.process .item{
    padding:20px 0;
    position:relative;
}
.process .num{
    display: inline-block;
    width:30px;
    height:30px;
    text-align: center;
    line-height:30px;
    color:#fff;
    background-color:#3291F2;
    box-sizing:0;
    border:1px solid #fff;
    border-radius:50%;
    position:relative;
    z-index:2;
}
.process .text{
    padding-left:18px;
}
.process .item:before,.process .item:after{
    content:'';
    height:50%;
    width:2px;
    background-color:#3291F2;
    position:absolute;
    left:15px;
    z-index:1;
}
.process .item:before{
    bottom:0;
}
.process .item:after{
    top:0;
}
.process .item:last-child:before{
    height:0;
}
.process .item:first-child:after{
    height:0;
}

html如下

<div class="process">
    <div class="item">
        <span class="num">1</span>
        <span class="text">流程1</span>
    </div> 
    <div class="item">
        <span class="num">2</span>
        <span class="text">流程2</span>
    </div>
    <div class="item">
            <span class="num">3</span>
        <span class="text">流程3</span>
    </div>
    <div class="item">
      <span class="num">4</span>
      <span class="text">流程4</span>
    </div>
</div>

效果如下:


image

:before和:after可以实现很多效果,之后可以一一探讨~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,816评论 1 92
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,811评论 1 45
  • 学会使用CSS选择器熟记CSS样式和外观属性熟练掌握CSS各种选择器熟练掌握CSS各种选择器熟练掌握CSS三种显示...
    七彩小鹿阅读 6,351评论 2 66
  • 本文转载自:众成翻译译者:为之漫笔链接:http://www.zcfy.cc/article/239原文:http...
    极乐君阅读 7,418评论 1 62
  • 转自理解伪元素 :before 和 :after层叠样式表(CSS)的主要目的是给HTML元素添加样式,然而,在一...
    种谔阅读 887评论 0 3