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

2022-08-13 zhangli 0 0 阅读需要2-5分钟

::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;  }

 

相关标签

相关文章

无相关信息
  • 网站用户提交姓名和电话号码表单验证JS

    网站一个用户留信息的表单,功能是表单有2个字段一个是姓名,一个是电话号码,用户提交表单后,JS验证姓名字段是否为空,电话号码字段验证填写的是不是电话号码格式。

  • 网站建设客户需求资料提供清单

    我们为客户提供网站建设服务,下面罗列了需要客户提供给我们的资料,这样就可以快速的搭建网站了。

  • 鼠标hover后图片被黑色遮挡动画效果

    一个鼠标经过后,图片被黑色层遮挡并且放大的效果CSS

  • justify-content:space-between使用方法

    涵义justify-content 用于设置或检索弹性盒子元素在主轴(横轴)方向上的对齐方式。

  • Owl Carousel参数说明和使用

    Owl Carousel是个人比较喜欢使用的一个强大、实用但小巧的 jQuery 幻灯片插件,它具有方便,兼容性好,特别是向我所有网站都是使用 bootstrap的自适应网站,就需要再使用一些幻灯片或者特效时候进行PC和移动端的全面兼容,而这个就满足了我的需求,由于经常使用,单每次都要隔一段时间,基本就把上一次使用熟练的参数和使用方法都忘记光了,所以做一份这样的参数记录表帮助自己能更好的记录和使用。

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

    ::before和::after 是用来给元素添加额外内容的,因为只存在于作用元素内容的前后.在css中,::before和::after 是一个伪类元素,代表生成的内容元素,表示相应元素的可抽象样式的第一个子元素,即:所选元素的第一个子元素。

  • 2016 Google Adsense付款方式总结提现西联汇款经验分享

    第一次去Adsense取现,在之前也看了很多的网上攻略,如何取现,了解了Adsense西联汇款付款方式,怀着刚刚花了2天时间取来的第一笔adsense收入来分享下这段不简单的网赚之路。

  • google analysics分析,网站优化推广转化率分析工具

      章力强烈推荐google analysics分析工具,是你网站优化推广转化率分析工具不得不用的工具,除了国内常用的CNZZ,51la等常用统计的工具以外,其实我现在发现google analysics分析

  • 卓越excms发展建议

    偶然间了解到了卓越excms,下载了测试下,还不错,作为一个新的cms个人觉得在基础框架方面都是非常不错的,比起国内某些老牌CMS强很多,当然ecms还需要很多需要完善的地方,以下是给excms的一点建议:

  • 电子商务网站的跳出率

    什么是跳出率?首先我们来看看  跳出,顾名思义就是指用户进入网站又退出的意思,跳出率就是指用户进入你的网站仅浏览了一个页面就离开,所占这个页面总访问次数的百分比。跳出次

  • google adsense广告不显示问题和解决方法

    在11月某天网站google adsense广告突然无法显示广告,查看adsense后台,显示没有数据,查收邮箱没有收到账号被K的邮件,并且我也没有作弊,对网站也没有动过,检查了一系列相关,包括代码问题,网站JS都没有问题

  • 网站运营是做内容为主,还是推广为主

    做网站,网站内容和网站推广是最重要的两个因素,探讨谁比较重要,其实只是想提醒大家,根据自己的网站推广方案做出选择,达到网站的目的就行!

  • 网站运营是做内容为主,还是推广为主

    做网站,网站内容和网站推广是最重要的两个因素,探讨谁比较重要,其实只是想提醒大家,根据自己的网站推广方案做出选择,达到网站的目的就行!

  • 如何建一个营销型企业网站

    网站建设的内容包括网页设计、网页制作、虚拟主机建设、网站程序开发、网站推广等等。

  • 卓越excms发展建议

    偶然间了解到了卓越excms,下载了测试下,还不错,作为一个新的cms个人觉得在基础框架方面都是非常不错的,比起国内某些老牌CMS强很多,当然ecms还需要很多需要完善的地方,以下是给excms的一点建议:

  • 大型网站建设管理规范经验和好处分享

    在开发这个大型网站前端的时候就开始发现规划和标准的重要性,不然代码太乱了,利用率也不高,管理乱七八糟,更重要的是开发好一段时间,对代码熟悉度不高了,后面在调整就更痛苦了,所以自己在写这个代码的时候开始给自己做标准化的管理规范,好处就是方便,代码易懂,扩展方便,使用高效。

  • 电子商务网站的跳出率

    什么是跳出率?首先我们来看看  跳出,顾名思义就是指用户进入网站又退出的意思,跳出率就是指用户进入你的网站仅浏览了一个页面就离开,所占这个页面总访问次数的百分比。跳出次

  • 传统企业与电子商务结合的问答

      关于传统企业与电子商务的问答问:电子商务已经成为趋势,不是是否做电子商务,而是什么时间做、投入多少去做,你认为企业选择在什么时间做比较合适,而应该如何布局实施?

  • 网站用户提交姓名和电话号码表单验证JS

    网站一个用户留信息的表单,功能是表单有2个字段一个是姓名,一个是电话号码,用户提交表单后,JS验证姓名字段是否为空,电话号码字段验证填写的是不是电话号码格式。

  • jquery-custom-scrollbar网页自定义滚动条样式

    之前大张力有一个网站项目就是需要像上图一样效果的,很漂亮的自定义滚动条,那时候没找到合适的,智能使用默认的浏览器滚动条,今天看到某个页面上很漂亮的滚动条,就查看了下代码,

  • 我的adsense账户被盗申述信

    我相信你们能理解一个GOOGLE ADSENSE 客户的心情,特别是一个账户即将到达100美金,第一次即将获得100美金的小站长,特别是为此还努力了3年的小站长。希望你们能帮我找回这个账户。

  • google analysics分析,网站优化推广转化率分析工具

      章力强烈推荐google analysics分析工具,是你网站优化推广转化率分析工具不得不用的工具,除了国内常用的CNZZ,51la等常用统计的工具以外,其实我现在发现google analysics分析

  • 网站运营是做内容为主,还是推广为主

    做网站,网站内容和网站推广是最重要的两个因素,探讨谁比较重要,其实只是想提醒大家,根据自己的网站推广方案做出选择,达到网站的目的就行!

  • 如何增加网站流量博客人气

     还在为你的网站博客没有人气而烦恼吗?如何增加网站博客人气?下面章力教大家一些方法来推广你的网站博客,让你被更多的人了解。

TOP