gpt4 book ai didi

css - :before gets placed above (instead of to the left) when element contains block elements

转载 作者:太空宇宙 更新时间:2023-11-04 09:49:07 24 4
gpt4 key购买 nike

我有一个伪元素,我想将其放在相关元素 (.alpha) 的左侧。

但是当该元素包含 block 元素 (.beta) 时,伪元素将置于所有元素之上。

是否有解决此问题的方法,或者是否不可能在具有伪元素的元素中包含 block 元素?

这是一个 live example .

<div class='alpha'>
<div class='beta'>paragraph text</div>
</div>

.alpha:before {
content: "this is ";
font-weight: bold;
font-style: italic;
}

最佳答案

方法#01:

在您的代码中为 .beta 类使用 inline 元素,例如 spanem 等。

.alpha:before {
content: "this is ";
font-weight: bold;
font-style: italic;
}
<div class='alpha'>
<span class='beta'>paragraph text</span>
</div>

方法#02:

在你的伪元素上添加 float: left 和一些 margin-right 并且如果你有 inline 的混合设置它的布局>block 元素在父元素中。

.alpha {
overflow: hidden;
}

.alpha:before {
content: "this is ";
font-weight: bold;
font-style: italic;
margin-right: 4px;
float: left;
}
<div class='alpha'>
<div class='beta'>
paragraph text
</div>
</div>

关于css - :before gets placed above (instead of to the left) when element contains block elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39244435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com