详情页

CSS种::before和::after使用方法

时间:2023年11月28日

编辑:佚名

::before和::after 是用来给元素添加额外内容的,因为只存在于作用元素内容的前后
在css中,::before和::after 是一个伪类元素,代表生成的内容元素,表示相应元素的可抽象样式的第一个子元素,即:所选元素的第一个子元素。
伪元素可用于插入几乎任何类型的内容,包括字符,图片,字符串
.element :: before {  
  content:url(path / to / image.png); / *图像,例如,图标* /
}
.element :: before {   
  content:“注意:” ; / *一个字符串* /
}
.element :: before {  
  content:“\ 201C” ; / *也算作一个字符串。转义Unicode会将其渲染为字符* /
}
说明:
1、伪类元素必须要配合content属性一起使用
2、伪类元素是css渲染层加入的,不能通过js来操作
3、伪类对象特效通常通过:hover伪类样式来激活
案例参考


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>before 和 after</title>
    <style>
    .wrapper * {
        box-sizing: border-box;
    }    
    .wrapper>div {
        height: 250px;
        width: 250px;
        border: 1px solid #ccc;
        background: #000;
        margin: 10px;
        float: left;
        color: #D9D9D9;
        padding: 30px;
        text-align: center;
    }
    /*基础用法1*/    
    .base1:before {
        content: "我是::before";
        color: #FB0D0D;
    }    
    .base1:after {
        content: "我是::after";
        color: #f70;
    }
    /*基础用法2*/
    
    .base2:before {
        content: '\ABCD\ABCD\ABCD\ABCD';
        white-space: pre;
        color: #FB0D0D;
    }
    
    .base2:after {
        content: '\609\609\609\609\609';
        white-space: pre;
        color: #f70;
    }
    /*::before , ::after添加背景图*/
    
    .base3:before {
        content: url(icon-plus.png);
    }
    
    .base3:after {
        content: url(icon-plus.png);
    }
    /*取自定义属性*/
    
    .base4:before {
        content: attr(title);
        color: #E8E3AA;
    }
    
    .base4:after {
        content: attr(data-test);
        color: #D8CF23;
    }
    /*小试身手合集*/
    
    .base5,
    .base6,
    .base7,
    .base8 {
        position: relative;
    }
    /*小试身手1*/    
    .base5:before {
        content: "";
        display: block;
        position: absolute;
        top: 0;
        left: 0;
        height: 50px;
        width: 50px;
        border-top: 5px solid #f70;
        border-left: 5px solid #f70;
    }
    
    .base5:after {
        content: "";
        display: block;
        position: absolute;
        right: 0;
        bottom: 0;
        height: 50px;
        width: 50px;
        border-right: 5px solid #f70;
        border-bottom: 5px solid #f70;
    }
    /*小试身手2*/    
    .base6:before {
        content: "";
        display: block;
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background: -webkit-linear-gradient(45deg, rgba(75, 65, 45, .3), rgba(123, 456, 789, .3), rgba(854, 183, 729, .3));
        background: linear-gradient(45deg, rgba(75, 65, 45, .3), rgba(123, 456, 789, .3), rgba(854, 183, 729, .3));
        z-index: 1;
    }
    
    .base6:after {
        content: "";
        display: block;
        position: absolute;
        bottom: 0;
        right: 0;
        width: 100%;
        height: 100%;
        background: -webkit-linear-gradient(-45deg, rgba(50, 125, 55, .7), rgba(55, 3, 45, .5), rgba(99, 12, 3, .8));
        background: linear-gradient(-45deg, rgba(50, 125, 55, .7), rgba(55, 3, 45, .5), rgba(99, 12, 3, .8));
        z-index: 1;
    }
    /*小试身手3*/    
    .base7:before {
        content: "";
        display: block;
        position: absolute;
        width: 50px;
        height: 50px;
        -webkit-animation: circle 2s ease-in-out infinite;
        -moz-animation: circle 2s ease-in-out infinite;
        -ms-animation: circle 2s ease-in-out infinite;
        -o-animation: circle 2s ease-in-out infinite;
        animation: circle 2s ease-in-out infinite;
        background: #C3172C;
    }
    
    .base7:after {
        content: "";
        display: block;
        position: absolute;
        content: "";
        background: #14965E;
        display: block;
        position: absolute;
        width: 50px;
        height: 50px;
        -webkit-animation: circle 2s ease-in-out infinite;
        -moz-animation: circle 2s ease-in-out infinite;
        -ms-animation: circle 2s ease-in-out infinite;
        -o-animation: circle 2s ease-in-out infinite;
        animation: circle 2s ease-in-out infinite;
    }
    /*小试身手4*/    
    .base8:before {
        content: "1";
        display: block;
        position: absolute;
        height: 100%;
        width: 10px;
        background: #6F0ECF;
        left: 0;
        top: 0;
        margin-left: -10px;
    }    
    .base8:hover:before {
        background: #9F81DE;
        -webkit-transform: rotate(-90deg) translate(-30%, 30%);
        transform: rotate(-90deg) translate(-30%, 30%);
        -webkit-transition: all 2s ease-in;
        transition: all 2s ease-in;
    }    
    .base8:after {
        content: ".";
        display: block;
        position: absolute;
        height: 100%;
        width: 10px;
        background: #6F0ECF;
        right: 0;
        bottom: 0;
        margin-right: -10px;
    }    
    .base8:hover:after {
        background: #9F81DE;
        -webkit-transform: rotate(-90deg) translate(-30%, 30%);
        transform: rotate(-90deg) translate(-30%, 30%);
        -webkit-transition: all 2s ease-in;
        transition: all 2s ease-in;
    }    
    @-webkit-keyframes circle {
        from {
            border-radius: 0%;
            top: 0;
        }
        35% {
            background: #2B2FDC;
            left: 30%;
            top: 50%;
        }
        75% {
            background: #AB9E9E;
            right: 0;
            bottom: 20%;
        }
        to {
            border-radius: 100%;
            top: 250px;
            left: 15%;
            bottom: 50%;
        }
    }
    
    @-moz-keyframes circle {
        from {
            border-radius: 0%;
            top: 0;
        }
        35% {
            background: #2B2FDC;
            left: 30%;
            top: 50%;
        }
        75% {
            background: #AB9E9E;
            right: 0;
            bottom: 20%;
        }
        to {
            border-radius: 100%;
            top: 250px;
            left: 15%;
            bottom: 50%;
        }
    }    
    @keyframes circle {
        from {
            border-radius: 0%;
            top: 0;
        }
        35% {
            background: #2B2FDC;
            left: 30%;
            top: 50%;
        }
        75% {
            background: #AB9E9E;
            right: 0;
            bottom: 20%;
        }
        to {
            border-radius: 100%;
            top: 250px;
            left: 15%;
            bottom: 50%;
        }
    }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="base1">添加文字</div>
    </div>
    <div class="wrapper">
        <div class="base2">添加unicode文字,图标的需要特殊字体库[font-face引入webfont,内部的unicode编码]</div>
    </div>
    <div class="wrapper">
        <div class="base3">添加背景图,但是不如用background好控制</div>
    </div>
    <div class="wrapper">
        <div class="base4" title="呵呵哒,萌萌哒" data-test="我是HTML5自定义属性">取自定义属性,取值不带双引号!!</div>
    </div>
    <div class="wrapper">
        <div class="base5">小试身手1:框框</div>
    </div>
    <div class="wrapper">
        <div class="base6">小试身手2:渐变</div>
    </div>
    <div class="wrapper">
        <div class="base7">小试身手3:变形</div>
    </div>
    <div class="wrapper">
        <div class="base8">小试身手4:追加</div>
    </div>
</body>
</html> 
::before和::after的与伪类的结合使用是有先后顺序的;
    .class:hover::before{} /*是有效的*/
    .class::before:hover{} /*是无效的*/    
内容居中标题案例

.title-line  { text-align: center; margin: 2rem 0; position: relative;   }
.title-line h2 {  font-size: 1.8rem; width: 10rem; height: 2rem; line-height: 2rem;margin: 0 auto; display: block; }
.title-line h2::before { display: inline-block; width: 5rem; height: 0.1rem; background: #eee; position: absolute; left: calc(50% - 10rem); content: ''; top: 1rem;   }
.title-line h2::after { display: inline-block; width: 5rem; height:0.1rem; background: #eee; position: absolute;right: calc(50% - 10rem);  content: ''; top: 1rem;  }
 
相关文章
猜你需要