CSS 中的 ::before 和 ::after 伪元素

目录

一、CSS 伪元素

二、::before  ::after 介绍

1、::before

2、::after

3、content 常用属性值

三、::before  ::after 应用场景

1、设置统一字符

2、通过背景添加图片

3、添加装饰线

4、右侧展开箭头

5、对话框小三角

6、插入icon图标


一、CSS 伪元素

CSS伪元素是指,在CSS 中使用一些特殊的选择器,创建出来的虚拟元素, 并不是实际存在的HTML元素;

  • 用来选择和操作文档中的特定部分,实现一些特殊效果;
  • 伪元素使得在不增加额外HTML标签的情况下,对文档中的内容进行样式化;
  • 伪元素也是一种元素,可以设置html中支持的各种属性,包括元素的布局、定位、宽高、背景等等;

本文主要介绍::before 、::after 这个两个伪元素的相关内容和一些使用场景;

二、::before  ::after 介绍

::before  ::after 伪元素用来给元素前面或者后面插入指定内容;

  • 使用content属性来指定要插入的内容;
  • 必须配合content属性一起使用,content的属性值可以为空;
  • 伪元素的display属性值默认为inline

1、::before

::before选择器用来向指定元素之前插入内容;

(1)语法

元素::before{
    content: "要插入的内容";
    /* 其他属性 */
}

(2)示例

给页面所有的p元素前面插入内容;

<style>
    p::before{
        content: "使用::before伪元素插入的内容——";
        /* 其他属性 */
    }
</style>
<body>
    <div>
        <p>第一个P标签中的内容</p>
        <p>第二个P标签中的内容</p>
        <p>第三个P标签中的内容</p>
    </div>
</body>

2、::after

::after选择器用来向指定元素之后插入内容;

(1)语法

元素::after{
    content: "要插入的内容";
    /* 其他属性 */
}

(2)示例

给页面所有的p元素后面插入内容;

<style>
    p::after{
        content: "——使用::after伪元素插入的内容";
        /* 其他属性 */
    }
</style>
<body>
    <div>
        <p>第一个P标签中的内容</p>
        <p>第二个P标签中的内容</p>
        <p>第三个P标签中的内容</p>
    </div>
</body>

3、content 常用属性值

::before  ::after 必须配合content属性一起使用,以下是content的常用属性值:

序号属性值说明
1string设置文本内容;
2url("url")设置图片等媒体文件的URL链接;
3open-quote设置为前引号;
4close-quote设置为后引号;
5attr(attribute)将元素的 attribute 属性以字符串形式返回;
6counter设定计数器;
7none设置 content 为空值;
8normal在 :before 和 :after 伪类元素中会被视为 none,即也是空值;

(1)设置文本内容

设置content的属性值为string类型,即可给伪元素添加文本;

<style>
    span::before{
        content: "使用::before添加的文本前缀——————";
    }
    span::after{
        content: "————使用::after添加的文本后缀";
    }
</style>
......
<body>
    <span class="box">我是HTML元素中的文本</span>
</body>

(2)设置媒体链接

通过url()属性值,即可导入媒体文件为伪元素的内容;

<style>
    .container {
        margin: 100px;
    }
    .avatar::after{
        content: url("D:\\test\\girl.png");
        display: block;
    }
</style>
......
<body>
    <div class="container">
        <div class="avatar">示例图片</div>
    </div>
</body>

注意,这里使用url添加的图片不能设置大小,最好通过背景添加图片;

(3)设置前 || 后引号

通过open-quote或close-quote属性值,即可给设置伪元素的内容为前引号或后引号;

<style>
    p:nth-child(1)::before{
        content:open-quote;
        /* 其他属性 */
    }
    p:nth-child(2)::after{
        content:close-quote;
    }
</style>
......
<body>
    <div>
        <p>添加前引号</p>
        <p>添加后引号</p>
    </div>
</body>

(4)获取元素属性

通过attr()获取元素的某一个属性值(以字符串的形式返回),并设置为伪元素的内容;

<style>
    a:after {
        content: " (" attr(href) ")";
    }
</style>
......
<body>
    <div><a href="https://www.csdn.net">CSDN</a>点击跳转至CSDN...</div>
    <div><a href="https://www.baidu.com">百度</a>点击跳转至百度...</div>
</body>

(5)设置计数器

<style>
    div {
        counter-increment: index;
    }
    div:before {
        content:counter(index);
    }
</style>
......
<body>
    <div>、、、、、、我是第1个div、、、、、、</div>
    <div>、、、、、、我是第2个div、、、、、、</div>
    <div>、、、、、、我是第3个div、、、、、、</div>
    <div>、、、、、、我是第4个div、、、、、、</div>
</body>

三、::before  ::after 应用场景

虽然 ::before  ::after 这两个伪元素的使用方式非常简单,但若能灵活应用,就能实现一些很不错的CSS效果;

1、设置统一字符

<style>
    p::before{
        content: "* ";
        color: red;
        font-size: 24px;
        /* 其他属性 */
    }
    p::after{
        content: ":____________";
        /* 其他属性 */
    }
</style>
...
<body>
    <div>
        <p>姓名</p>
        <p>年龄</p>
        <p>出生日期</p>
        <p>居住地址</p>
    </div>
</body>

2、通过背景添加图片

<style>
    .container{
        margin: 100px;
    }
    .container::after{
        content: "";
        display:block;
        width: 260px;
        height: 260px;
        background-image: url("D:\\test\\girl.png");
        background-position: center;
        background-size: cover;
    }
</style>
......
<body>
    <div class="container">通过背景添加图片</div>
</body>

3、添加装饰线

<style>
    .line{
        display: flex;
        align-items: center;
        margin: 60px;
        height: 40px;
        font-size: 18px;
    }
    .line::before, .line::after{
        content: "";
        width: 300px;
        border-top: 6px double;
        margin: 5px;
    }
    
</style>
......
<body>
    <div class="line">添加装饰线</div>
</body>

4、右侧展开箭头

<style>
    .container{
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

        width: 400px;
        margin: 100px auto;
        padding: 30px 0;
        border-radius: 8px;
        box-shadow: 0 0 4px 1px #acacac;
    }

    .setting-item{
        position: relative;
        align-items: center;
        display: flex;
        width: 300px;
        height: 40px;
        margin-bottom: 20px;
        border-bottom: 1px solid #ccc;
    }

    .setting-item::after{
        position: absolute;
        right: 0;
        content: "";
        width: 8px;
        height: 8px;
        border-top: 1px solid #666;
        border-right: 1px solid #666;
        transform: rotate(45deg);
    }
    
</style>
......
<body>
    <div class="container">
        <div class="setting-item">账号设置</div>
        <div class="setting-item">权限管理</div>
        <div class="setting-item">相关服务</div>
        <div class="setting-item">帮助与反馈</div>
        <div class="setting-item">......</div>
    </div>
</body>

5、对话框小三角

<style>
    .container {
        width: 400px;
        margin: 100px auto;
        padding: 30px 0;
        border-radius: 8px;
        box-shadow: 0 0 4px 1px yellowgreen;
    }

    .left-box,.right-box {
        display: flex;
    }

    .right-box {
        justify-content: end;
    }

    span {
        position: relative;
        display: flex;
        align-items: center;

        background-color: yellowgreen;
        border-radius: 6px;
        margin: 4px 14px;
        padding: 16px;
    }

    .left-box span::before, .right-box span::after{
        position: absolute;
        content: "";
        width: 12px;
        height: 12px;
        background-color: yellowgreen;
        transform: rotate(45deg);
    }

    .left-box span::before{
        left: -6px;
    }
    .right-box span::after {
        right: -6px;
    }
</style>

......

<body>
    <div class="container">
        <div class="left-box">
            <span>Nice to meet you!</span>
        </div>
        <div class="right-box">
            <span>Nice to meet you, too!</span>
        </div>
    </div>
</body>

6、插入icon图标

<style>
    .login-box{
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

        width: 400px;
        height: 400px;
        margin: 100px auto;
        border-radius: 8px;
        box-shadow: 0 0 4px 1px #acacac;
    }
    .title{
        font-size: 24px;
        font-weight: 700;
        margin-bottom: 40px;
    }
    .account, .pwd, .login-btn, .forgot-pwd{
        width: 300px;
        height: 40px;
        line-height: 40px;
    }

    .account, .pwd{
        display: flex;
        align-items: center;
        border-bottom: 1px solid #ccc;
        font-size: 14px;
        color: #888;
    }
    .pwd{
        margin-top: 20px;
    }
    .account::before, .pwd::before{
        content: '';
        display: inline-block;  
        width: 24px;
        height: 24px;
        background-repeat: no-repeat;
        background-position: center center;
        background-size: contain;
        margin-right: 8px;
    }
    .account::before{
        background-image: url("D:\\test\\user.svg");
    }
    .pwd::before {
        background-image: url("D:\\test\\pwd.svg");
    }

    .login-btn{
        text-align: center;
        color: #fff;
        font-size: 16px;
        font-weight: 700;
        background: #2687F0;
        border-radius: 5px;
        margin-top: 40px;
    }

    .forgot-pwd{
        text-align: right;
        font-size: 14px;
        color: #888;
        margin-top: 20px;
    }
</style>
......
<body>
    <div class="login-box">
        <div class="title">XXX 管理系统</div>
        <div class="account">请输入账号</div>
        <div class="pwd">请输入密码</div>
        <div class="login-btn">登 录</div>
        <div class="forgot-pwd">忘记密码</div>
    </div>
</body>

=========================================================================

每天进步一点点~!

一个实用的CSS小技巧~!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/794366.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

C++系列-Vector(一)

&#x1f308;个人主页&#xff1a;羽晨同学 &#x1f4ab;个人格言:“成为自己未来的主人~” Vector的介绍及使用 Vector的介绍 当vector构建的参数类型为char类型时&#xff0c;它是和string是极其类似的&#xff0c;但是二者之间也有不同&#xff0c;比如&#xff0c…

在conda的环境中安装Jupyter及其他软件包

Pytorch版本、安装和检验 大多数软件包都是随Anaconda安装的&#xff0c;也可以根据需要手动安装一些其他软件包。 目录 创建虚拟环境 进入虚拟环境 安装Jupyter notebook 安装matplotlib 安装 pandas 创建虚拟环境 基于conda包的环境创建、激活、管理与删除http://t.cs…

04:定时器

定时器 1、定时器怎么定时2、怎样实现计数&#xff1f;2.1、控制寄存器TCON2.2、工作模式寄存器TCOM2.3、定时器T0 3、案例&#xff1a;通过定时器T0控制LED间隔1s亮灭 当定时器用的时候&#xff0c;靠内部震荡电路数数。当配置为定时器使用时&#xff0c;每经过1个机器周期&am…

全国排名第一的起名大师颜廷利:唯有量力而行,才能。。。

在探索成功与个人成长的旅程中&#xff0c;中国传统哲学提供了一个独特的视角&#xff1a;量力而行&#xff0c;以展现最靓丽的自我。这一理念不仅深植于中国丰富的文化传统之中&#xff0c;而且与现代社会的实用主义不谋而合。 中国最受欢迎的起名大师颜廷利教授&#xff0c;一…

雷士护眼大路灯值得买吗?书客、雷士、琪朗三大护眼落地灯横评实测!

雷士护眼大路灯值得买吗&#xff1f;随着生活水平的提高&#xff0c;更多的人开始关注养生和视力健康&#xff0c;而护眼大路灯因为有能够提高光线质量&#xff0c;也能用来盖上长期用眼造成眼部干涩、酸痛等症状也越来越受消费者青睐。而现在的护眼大路灯市场&#xff0c;品牌…

先导微型雕刻机XD1213创造更多可能性

打造一间现代化教育装备的校园木工创客室&#xff0c;使木工制作、创意设计、科技融合与教育实践于一体的多功能空间&#xff0c;对校园木工创客室配备一些列的木工设备和工具&#xff0c;以满足不同层次的木工制作需求。 校园木工创客室的空间布局应合理、有序&#xff0c;便于…

【鸿蒙学习笔记】关系型数据库概述

目录标题 关系型数据库的运行机制样例代码共通方法 DBUtilsIndex 代码效果 关系型数据库的运行机制 1、 关系型数据库对应用提供通用的操作接口&#xff0c;底层使用SQLite作为持久化存储引擎&#xff0c;支持SQLite具有的数据库特性&#xff0c;包括但不限于事务、索引、视图…

精密制造,光纤激光打标机在电子通讯行业的深度实践

光纤激光打标机&#xff0c;作为激光加工领域的一项先进技术&#xff0c;近年来在电子通讯行业中得到了广泛应用。它利用掺稀土元素玻璃光纤作为增益介质的激光器&#xff0c;通过光纤放大器开发出高功率密度的激光束&#xff0c;对工件表面进行精细加工&#xff0c;形成永久性…

强制升级最新系统,微软全面淘汰Win10和部分11用户

说出来可能不信&#xff0c;距离 Windows 11 正式发布已过去整整三年时间&#xff0c;按理说现在怎么也得人均 Win 11 水平了吧&#xff1f; 然而事实却是&#xff0c;三年时间过去 Win 11 占有率仅仅突破到 29%&#xff0c;也就跳起来摸 Win 10 屁股的程度。 2024 年 6 月 Wi…

Visual Studio 2022 安装及使用

一、下载及安装 VS 官网&#xff1a;Visual Studio: IDE and Code Editor for Software Developers and Teams 下载免费的社区版 得到一个.exe文件 右键安装 选择C开发&#xff0c;并修改安装位置 等待安装 点击启动 二、VS的使用 1.创建项目 打开VS&#xff0c;点击创建新项…

Ubuntu22.04安装NIVIDIA显卡驱动总结

1.首先在安装驱动时需要判断系统有无GPU以及GPU的型号 可以参考这篇文章&#xff1a; https://blog.51cto.com/u_13171517/8814753#:~:textubuntu%20%E7%B3%BB%E7%BB%9F%20%E6%80%8E%E4%B9%88%E5%88%A4%E6%96%AD%E7%B3%BB%E7%BB%9F%E6%9C%89%E6%B2%A1%E6%9C%89GPU%201%20%E6%…

批量提取PDF中表格内容

1 背景 从PDF文件获取表格中的数据&#xff0c;也是日常办公容易涉及到的一项工作。比如我们想获取某公司年报里面的表格数据&#xff0c;PDF动辄上百页的数据。 2 传统方法 一个一个从PDF表格中复制&#xff0c;然后粘贴到Excel表格中&#xff0c;效率太低了。 3 办公自动…

【OC】巧用UIStackView简化布局

UIStackView的运用 文章目录 UIStackView的运用引入UIStackView的作用UIStackView的属性compression resistance 和 huggingaxisalignmentDistributionspacing UIStackView的方法UIStackView的示例 引入 在仿写ZARA的过程之中&#xff0c;我看到软件之中是有大量的按钮排列在一…

一文带你掌握SpringMVC扩展点RequestBodyAdvice和ResponseBodyAdvice如何使用及实现原理

1.概述 Spring MVC是当今web项目系统开发后端技术框架的不二之选&#xff0c;也是Spring全家桶中非常重要的一个成员&#xff0c;它的设计思想和实现套路都是很值得我们学习的&#xff0c;所以今天我们就再来看看Spring MVC框架中预留的两个钩子也就是扩展点&#xff1a;Reque…

minio在redhat7.9上面的单节点单驱动离线安装(docker)

问题 最近需要在红帽上面离线安装minio&#xff0c;并且还是要离线安装到服务器中的Docker里面去。 检查服务器磁盘 # lsblk -f NAME FSTYPE LABEL UUID MOUNTPOINT sda ├─sda1 xfs xxxxsx-xxx-xxx…

如何基于大模型开发应用接口

一、前言 针对自然语言处理方向&#xff0c;以前要开发一个情感分析、命名实体识别之列的应用是非常麻烦的&#xff0c;我们需要完成收集数据、清理数据、构建模型、训练模型、调优模型等一系列操作&#xff0c;每一步都非常耗时。如今大语言模型&#xff08;LLM&#xff09;的…

【BUG】已解决:JsonMappingException

已解决&#xff1a;JsonMappingException 欢迎来到英杰社区https://bbs.csdn.net/topics/617804998 概述&#xff1a; 没有getter方法的实体的序列化&#xff0c;并解决Jackson引发的JsonMappingException异常。 默认情况下&#xff0c;Jackson 2只会处理公有字段或具有公有get…

RK3588部署YOLOV8-seg的问题

在使用YOLOV8-seg训练出来的pt模型转为onnx的时候&#xff0c;利用以下仓库地址转。 git clone https://github.com/airockchip/ultralytics_yolov8.git 在修改ultralytics/cfg/default.yaml中的task&#xff0c;mode为model为自己需要的内容后&#xff0c; 执行以下语句 cd …

2006-2021年 291个地级市资源错配指数、劳动和资本相对扭曲指数do文件和结果

资源错配指数&#xff1a;衡量生产要素配置效率的关键指标 资源错配指数&#xff08;Misallocation Index&#xff09;是一个衡量资源配置效率的指标&#xff0c;它反映了生产要素是否得到了合理配置&#xff0c;以及是否达到了生产效率的最优状态。一个较高的资源错配指数意味…

视图库对接系列(GA-T 1400)十六、视图库对接系列(本级)通知(订阅回调)

说明 之前我们实现了订阅接口,其中有一个receiveAddr参数, 这个就是对应的回调的地址。一般情况下对应的是同一个服务。 我们推荐使用http://xxx:xxx/VIID/SubscribeNotifications接口文档 SubscribeNotificationList对象对象如下: 文档中是xml,但实际上目前使用的都是jso…