您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

before && after 位置

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

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

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

.demo:before{

}
.demo:after{
    
}

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

<div class="demo"></div>
 .demo:before{
    content: '姓名:';
}

图:

<!DOCTYPE html>
<html lang="en">
<head>
    < charset="UTF-8">
    < name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>@H_842_@
        .demo:before{
            content: '姓名:';
        }   
    </style>
</head>
<body>
    <div class="demo"></div>
</body>
</html>
 .demo:after{
    content: '很好';
}

图:

<!DOCTYPE html>
<html lang="en">
<head>
    < charset="UTF-8">
    < name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>@H_842_@
        .demo:after{
            content: '很好';
        }  
    </style>
</head>
<body>
    <div class="demo"></div>
</body>
</html>

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

 <div class="demo">
    <div class="item"></div>
    <div class="item"></div>        
</div>
<div class=""></div>
.demo:after{
    content: '';
    display: block;
    clear: both;
}
.item{
    float: left;
}

图:

<!DOCTYPE html>
<html lang="en">
<head>
    < charset="UTF-8">
    < name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>@H_842_@
       .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>

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

<div class="demo"></div>
.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;
}

<!DOCTYPE html>
<html lang="en">
<head>
    < charset="UTF-8">
    < name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>@H_842_@
       .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>
.demo::before{

}
.demo::after{
    
}

联系我
置顶