before && after 位置

before && after

@H_404_3@

这两个伪类元素功能很相似,都是在元素内部插入新的内容。下面一起看下他们的区别和用法

@H_404_3@

1. 官方定义

@H_404_3@

before:元素的内容之前插入新内容
after:元素的内容之后插入新内容

@H_404_3@

2. 解释

@H_404_3@

beforeafter功能就是在元素的内部的原有内容之前,或者之后插入新的内容

@H_404_3@

3. 语法

@H_404_3@
.demo:before{

}
.demo:after{
    
}
@H_404_3@

解释:使用方法如上面,通过在元素选择器后面增加一个 : 来开始伪类的使用。

@H_404_3@

4. 兼容性

@H_404_3@
IE Edge Firefox Chrome Safari Opera ios android
all all all all all all all all
@H_404_3@@H_404_3@

5. 实例

@H_404_3@
<div class="demo"></div>
@H_404_3@
  1. 在元素内容之前插入文字:姓名。
@H_404_3@
 .demo:before{
    content: '姓名:';
}
@H_404_3@

效果图:

@H_404_3@

编程之家

@H_404_3@
元素内容之前插入文字:姓名 效果
@H_404_3@
@H_404_3@
<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .demo:before{
            content: '姓名:';
        }   
    </style>
</head>
<body>
    <div class="demo"></div>
</body>
</html>
@H_404_3@@H_404_3@ @H_404_3@
  1. 在元素内容之后插入:很好。
@H_404_3@
 .demo:after{
    content: '很好';
}
@H_404_3@

效果图:

@H_404_3@

编程之家

@H_404_3@
在元素内容之后插入:很好 效果
@H_404_3@
@H_404_3@
<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .demo:after{
            content: '很好';
        }  
    </style>
</head>
<body>
    <div class="demo"></div>
</body>
</html>
@H_404_3@@H_404_3@ @H_404_3@

6. 经验分享

@H_404_3@

这两个伪类当然不是仅仅插入内容这么简单,它还有其他的妙用。

@H_404_3@
  1. 使用伪类 after 清除元素内部浮动效果
@H_404_3@
 <div class="demo">
    <div class="item"></div>
    <div class="item"></div>        
</div>
<div class=""></div>
@H_404_3@
.demo:after{
    content: '';
    display: block;
    clear: both;
}
.item{
    float: left;
}
@H_404_3@

效果图:

@H_404_3@

编程之家

@H_404_3@
使用伪类 after 清除浮动 效果
@H_404_3@
@H_404_3@
<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
       .demo:after{
            content: '';
            display: block;
            clear: both;
        }
        .item{
            float: left;
        }
    </style>
</head>
<body>
    <div class="demo">
        <div class="item"></div>
        <div class="item"></div>        
    </div>
    <div class=""></div>
</body>
</html>
@H_404_3@@H_404_3@ @H_404_3@

说明:下面灰色部分是没有清除浮动的效果,上面是清除浮动的效果。因为清除了浮动所以 “网” 这个字换行了。

@H_404_3@
  1. 在元素内容开始前插入图片
@H_404_3@
<div class="demo"></div>
@H_404_3@
.demo:before{
    content: '';
    display:inline-block;
    width:px;
    height:px;
    font-size:px;
    line-height:px;
    background: url(//img.mukewang.com/wiki/5eea2f6809a8d35e00400040.jpg) center  no-repeat;
    background-size: cover;
}
@H_404_3@

编程之家

@H_404_3@
元素内容开始前插入图片 效果
@H_404_3@
@H_404_3@
<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
       .demo:before{
            content: '';
            display:inline-block;
            width:px;
            height:px;
            font-size:px;
            line-height:px;
            background: url(//img.mukewang.com/wiki/5eea2f6809a8d35e00400040.jpg) center  no-repeat;
            background-size: cover;
        }
    </style>
</head>
<body>
    <div class="demo"></div>
</body>
</html>
@H_404_3@@H_404_3@ @H_404_3@

7. 小结

@H_404_3@
  1. 注意:对于 IE8 及更早版本中的:before:after,必须声明 <!DOCTYPE>。
  2. 在元素选择器后面这样写也可以:
@H_404_3@
.demo::before{

}
.demo::after{
    
}
@H_404_3@