为什么:before和:after伪元素需要一个'content'属性?
P粉653045807
P粉653045807 2023-07-17 18:43:31
[HTML讨论组]

在以下情况下,为什么:after选择器需要一个content属性才能起作用?


.test {
    width: 20px;
    height: 20px;
    background: blue;
    position:relative;
}
			
.test:after {
    width: 20px;
    height: 20px;
    background: red;
    display: block;
    position: absolute;
    top: 0px;
    left: 20px;
}


注意,在没有指定content属性之前,你看不到伪元素。


.test {
    width: 20px;
    height: 20px;
    background: blue;
    position:relative;
}
			
.test:after {
    width: 20px;
    height: 20px;
    background: red;
    display: block;
    position: absolute;
    top: 0px;
    left: 20px;
    content:"hi";
}


为什么这是预期的功能?你可能会认为display:block会强制元素显示出来。奇怪的是,你实际上可以在Web调试工具中看到样式,但它们不会显示在页面上。

P粉653045807
P粉653045807

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号