为什么这样子不显示伪类设置的样式,我设置了定位才能显示
<!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
即可。