css - 伪元素问题:before,:after
本文介绍了css - 伪元素问题:before,:after的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
为什么这样子不显示伪类设置的样式,我设置了定位才能显示
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
h1:before{ width:100px; height:5px; background:red; content:"";}
h1:after{ width:100px; height:5px; background:#000; content:"";}
</style>
</head>
<body>
<h1>和哈哈哈哈哈哈哈哈哈</h1>
</body>
</html>
解决方案
因为 :before
和 :after
的 display
属性默认为 inline
. 行内非替换元素不适用 width
或 height
属性。改成 display: block
即可。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
h1:before{ width:100px; height:5px; background:red; content:""; display: block;}
h1:after{ width:100px; height:5px; background:#000; content:""; display: block;}
</style>
</head>
<body>
<h1>和哈哈哈哈哈哈哈哈哈</h1>
</body>
</html>
这篇关于css - 伪元素问题:before,:after的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文