我的问题是 :after
元素位于元素中的文本上。这是一个例子:
我希望 :after
位于文本下方。
我该如何解决这个问题?
body {
text-align: center;
}
a {
display: inline-block;
text-decoration: none;
padding: 10px;
background: #222;
color: #f3f3f3;
font-size: 2em;
margin: 20px auto;
position: relative;
}
a:after {
content: "";
width: 100%;
height: 50%;
background: #00A3A3;
position: absolute;
left: 0;
top: 0;
}
<a href="#">Home page</a>
最佳答案
只需将 after
中的 top:0
更改为 top:50px;
即可。通过保持 top:0
top 属性将元素的上边缘设置为其最近定位祖先的上边缘上方/下方一个单位。
您通过将元素保持为 0 使该元素保持在顶部
body {
text-align: center;
}
a {
display: inline-block;
text-decoration: none;
padding: 10px;
background: #222;
color: #f3f3f3;
font-size: 2em;
margin: 20px auto;
position: relative;
}
a:after {
content: "";
width: 100%;
height: 50%;
background: #00A3A3;
position: absolute;
left: 0;
top: 50px;
}
<a href="#">Home page</a>
关于html - css3 after 是在元素中的文本上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47239168/