【问题标题】:Why does my ::before element only display if I use position absolute? [duplicate]为什么我的 ::before 元素仅在我使用绝对位置时才显示? [复制]
【发布时间】:2020-01-24 16:58:52
【问题描述】:

我有一个简单的问题,我似乎找不到答案,可能是我没有在 Google 中输入正确的搜索字词,而是在试图获得更好的理解时挠了挠头对此,我仍然空手而归。

我有一个span 元素,它有一个::before 伪元素,没什么特别的,只是一个简单的圆圈。但是,我注意到我必须绝对定位伪元素才能使其可见。我发现这很奇怪,并且不确定我是否只是不完全理解这种性质的伪元素,或者我是否错误地实现了这个想法。

不管怎样,这里有一个有和没有绝对定位的例子。

html,
body {
  margin: 0;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

span {
  margin-bottom: 15px;
}

.no-absolute::before,
.absolute::before {
  content: '';
  width: 10px;
  height: 10px;
  margin-right: 5px;
  border-radius: 50%;
  background-color: #f33;
  cursor: pointer;
}

.absolute {
  position: relative;
}

.absolute::before {
  position: absolute;
  top: 4px;
  left: -15px;
}
<span class="no-absolute">No Absolute</span>
<span class="absolute">Absolute</span>

为什么我的::before 伪元素必须绝对定位才能可见?

【问题讨论】:

  • @HarunYilmaz ::before 伪类已经有content: '';
  • 因为你不能将高度设置为内联元素和绝对位置强制显示阻塞

标签: html css visibility pseudo-element


【解决方案1】:

答案是:您需要display 来给出伪元素的大小/形状(blockinline-block)。 inline 元素接受宽度/高度。

您也可以使用padding 来赋予它大小/形状,但displaywidth / height 最有意义。

span {
  margin-bottom: 15px;
  position: relative;
}

.no-absolute::before,
.absolute::before {
display: block;
  content: '';
  width: 10px;
  height: 10px;
  margin-right: 5px;
  border-radius: 50%;
  background-color: #f33;
  cursor: pointer;
}
&lt;span class="absolute"&gt;Absolute&lt;/span&gt;

【讨论】:

    【解决方案2】:

    您的pseudo 元素需要一个显示属性。我添加了display: block

    html,
    body {
      margin: 0;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
    }
    
    span {
      margin-bottom: 15px;
      position: relative;
    }
    
    .no-absolute::before,
    .absolute::before {
      content: '';
      width: 10px;
      height: 10px;
      margin-right: 5px;
      border-radius: 50%;
      background-color: #f33;
      cursor: pointer;
      display: block;
    }
    
    .absolute {
      position: relative;
    }
    
    .absolute::before {
      position: absolute;
      top: 4px;
      left: -15px;
    }
    <span class="no-absolute">No Absolute</span>
    <span class="absolute">Absolute</span>

    【讨论】:

      【解决方案3】:

      您不需要绝对定位。您只需要设置 ::before 伪元素的显示属性

      html,
      body {
        margin: 0;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
      }
      
      span {
        margin-bottom: 15px;
      }
      
      .no-absolute::before{
        content: '';
        display: inline-block;
        width: 10px;
        height: 10px;
        margin-right: 5px;
        border-radius: 50%;
        background-color: #f33;
        cursor: pointer;
      }
      &lt;span class="no-absolute"&gt;No Absolute&lt;/span

      【讨论】:

      • 太快了!你能为未来的读者解释一下为什么会这样吗?我认为这是因为 DOM 不知道如何在没有明确告知的情况下显示元素,但这只是一个有根据的猜测。感谢您指出了这一点!此外,不能再接受 6 分钟。 +1
      • 伪元素默认被赋予“初始”显示属性,即“内联”。在您的情况下,您需要将 display 属性更改为“inline-block”或“block”,以便能够在块级别进行更改
      猜你喜欢
      • 1970-01-01
      • 2018-10-17
      • 2021-04-28
      • 2021-01-17
      • 1970-01-01
      • 2019-02-17
      相关资源
      最近更新 更多