简体   繁体   English

:伪元素未显示之后

[英]:after pseudo element is not showing up

I have simplified my question to this: 我简化了我的问题:

HTML HTML

<div class="dropbox">
</div>

CSS CSS

.dropbox {
    border:2px solid #ff0000;
    height:200px;
    width:50%;
    margin:auto;
}
.dropbox:after {
    content:'';
    width:10px;
    height:10px;
    background:#ff0000;
    top:100%;
    left:50%;
    margin-left:5px;
}

Why can't I get the :after pseudo element to show up? 为什么我不能得到:after伪元素出现?

I need it at the bottom middle of the div. 我需要它在div的底部中间。 To by clear, it should be under the div in the middle. 要清楚,它应该在中间的div下面。

It's worth noting that the pseudo element is inline by default - you therefore can't add margins or change the dimensions of it. 值得注意的是,伪元素默认是inline的 - 因此您无法添加边距或更改其尺寸。 If you were to change the display to block you will notice that it shows up as you would expect (example) . 如果您要将显示更改为block您会注意到它显示为您所期望的(示例)

In your case you need to absolutely position it relative to the parent element. 在您的情况下,您需要对于父元素绝对定位它。

Example Here 这里的例子

.dropbox {
    border:2px solid #ff0000;
    height:200px;
    width:50%;
    margin:auto;
    position:relative;
}
.dropbox:after {
    content:'';
    position:absolute;
    width:10px;
    height:10px;
    background:#ff0000;
    top:100%;
    left:50%;
    margin-left:5px;
}

I'm guessing that the default display for the pseudo :after element is inline. 我猜测伪:after元素的默认显示是内联的。 Because the :after element has no content it shrinks to nothing. 因为:after元素没有内容,所以它缩小为空。

Set display:block then your width & height apply and you can see your element. 设置display:block然后适用您的宽度和高度,您可以看到您的元素。

.dropbox:after {
    content:'';
    width:10px;
    height:10px;
    background:#ff0000;
    top:100%;
    left:50%;
    margin-left:5px;
    display: block
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM