html - CSS :after is being pushed down by following elements

标签 html css less

我正在使用 after 元素在 h2 元素下显示边框,但由于某种原因 after 元素被下推通过以下 divs/elements/text。

图片说明: enter image description here

为什么会这样?我的代码有什么问题?

&:after {
    position: absolute;
    height: 4px;
    display: block;
    bottom:-20px;
    width: 200px;
    background: @yellow;
    content: '';

} 

提前致谢!

最佳答案

问题是在这种情况下绝对位置不是相对于它的父“H2”为了解决这个问题

h2{
   position:relative;
}

h2:after {
    position: absolute;
    height: 4px;
    display: block;
    bottom:-20px;
    width: 200px;
    background: yellow;
    content: '';
} 
h2{
position:relative;
}
<h2>title</h2>

关于html - CSS :after is being pushed down by following elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43207678/

相关文章:

html - 如何使用 CSS 向我的表格添加文本阴影

javascript - 悬停时显示 div 的图标

html - DIV 的最高边距不起作用?

html - 我如何使用 less/css 只影响没有其他内容的段落中的 imgs?

javascript - 使用选择器后 Jquery 使用选择器

javascript - 为什么标题转换在 chrome 和 firefox 中有效,但在 edge 中无效?

html/css 在 ios 设备上没有响应

javascript - 如何访问 <caption> 标签中的文本?

css - 文件观察器不会从 PhpStorm 中的 Less 文件生成 CSS

javascript - Symfony、grunt、requires.js 和 Assetic