Div:在出现在街区前面之前

时间:2019-11-13 作者:Jonny

我试图在我的分数框中添加一条背景线,但出于某种原因div::before 在这种情况下不会躲在后面。You can view it live here

    .game-center-result .score h1 {
    font-size: 56px;
    color: #fff;
    font-weight: 400;
    margin: 0;
    border: 2px solid #45464f;
    border-radius: 50%;
    width: 127px;
    height: 127px;
    margin: 0 auto;
    line-height: 131px;
    background: #30313b;
}
.game-center-result .score h1:before {
    content: "";
    position: absolute;
    display: block;
    width: 100%;
    height: 2px;
    background: #44454d;
    left: 0;
    right: 0;
    bottom: 60px;
    margin: 0 auto;
}
[enter image description here]

1 个回复
SO网友:Michelle

尽管有名字,:before 实际上并没有设置元素的分层位置,而是通过z-index. 要将:before元素放置在H1文本后面,需要添加

z-index: -1;
到:before元素,如下所示:

.game-center-result .score h1:before {
    content: "";
    position: absolute;
    display: block;
    width: 100%;
    height: 2px;
    background: #44454d;
    left: 0;
    right: 0;
    bottom: 60px;
    margin: 0 auto;
    z-index:-1;
}