有关css伪元素before after

本文深入探讨了CSS伪元素:before与:after的使用技巧,通过实例展示了如何在不同类型的HTML元素前后添加文本或图片,揭示了它们在块级元素与文本元素中的表现差异。
<html>

<head>
    <style type="text/css">
        div:before {
            content: url(open_live.png);
            background-color: #ff0000;
            width: 100px;
            height: 100px;
        }
        
        div::after {
            content: url(open_live.png);
            background-color: #ff0000;
        }
        
        p:before {
            content: "before-";
        }
        
        p::after {
            content: "-after";
        }
        
        button:before {
            content: "before-";
        }
        
        button::after {
            content: "-after";
        }
    </style>
</head>

<body>
    <p style=" background-color: #ff0000;">111111111111111</p>
    <div class="container">
        <p>222222222222222</p>
        <button>ssss</button>
    </div>

</html>

效果:

通过效果我们可以看出,如果是文本内,添加文字的话是直接在标签的前面或后面添加文字

如果是块,则在该块的开始或者结束位置添加子元素。

另外注意如果是在p标签前面添加img这个是没有效果的

但如果是在div标签前面添加文本 :

<html>

<head>
    <style type="text/css">
        div:before {
            content: "before";
            background-color: #ff0000;
            width: 100px;
            height: 100px;
        }
        
        div::after {
            content: "after-";
            background-color: #ff0000;
        }
    </style>
</head>

<body>

    <div class="container" style=" background-color: #00ff00;">
        <p>222222222222222</p>
        <button>ssss</button>
    </div>

</html>

效果如下

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值