【问题标题】:always keep pseudo element horizontally in the middle始终将伪元素水平保持在中间
【发布时间】:2020-01-21 20:18:32
【问题描述】:

如何始终将伪元素水平保持在中间?我将它绝对定位为一个百分比值,我理解它是相对于元素的宽度,但在不同的屏幕尺寸上,圆形“OR”在中间并不一致......

见下文:

.content {
    display: flex;
    flex-direction: row;
    justify-content: space-evenly;
    text-align: center;
    margin: 20px;
}

.cc-content {
    width: 50%;
    padding: 5px 20px;
    position: relative;
    background-color: red;
}

.cc-content-1 {
  margin-right: 3px;
}

.cc-content-2 {
  margin-left: 3px;
}

.cc-content-1:after {
    content: 'OR';
    display: inline-block;
    position: absolute;
    vertical-align: middle;
    line-height: 60px;
    top: 20px;
    left: 90%;
    z-index: 1;
    font-size: 21px;
    font-weight: bold;
    border-radius: 100%;
    width: 60px;
    height: 60px;
    background-color: #F2F2F2;
    color: #006AAD;
}
<div class="content">
      <div class="cc-content cc-content-1">
        <h3>Header Here</h3>
        <p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
      </div>
      <div class="cc-content cc-content-2">
        <h3>Header Here</h3>
        <p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
      </div>
</div>

如果您调整窗口大小,您会看到圆圈略微偏离中心,我如何始终将其保持在绝对位置的中心?不是百分比吗?我尝试使用 flex,因为它位于 flex 容器中,但这似乎没有任何作用。

如何将绝对位置:after 元素水平居中?

【问题讨论】:

    标签: html css pseudo-element


    【解决方案1】:

    与@mgrsskls 非常相似,但使用实际的:after 元素。使用absolute 是一个老技巧,然后添加left 和等于半角的负边距。但在您的情况下,还需要考虑额外的余量。

    .content {
        display: flex;
        flex-direction: row;
        justify-content: space-evenly;
        text-align: center;
        margin: 20px;
    }
    
    .cc-content {
        width: 50%;
        padding: 5px 20px;
        position: relative;
        background-color: red;
    }
    
    .cc-content-1 {
      margin-right: 3px;
    }
    
    .cc-content-2 {
      margin-left: 3px;
    }
    
    .cc-content-1:after {
        content: 'OR';
        display: inline-block;
        position: absolute;
        vertical-align: middle;
        line-height: 60px;
        top: 20px;
        left: 100%;
        margin-left: -27px;
        z-index: 1;
        font-size: 21px;
        font-weight: bold;
        border-radius: 100%;
        width: 60px;
        height: 60px;
        background-color: #F2F2F2;
        color: #006AAD;
    }
    <div class="content">
          <div class="cc-content cc-content-1">
            <h3>Header Here</h3>
            <p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
          </div>
          <div class="cc-content cc-content-2">
            <h3>Header Here</h3>
            <p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
          </div>
    </div>

    【讨论】:

      【解决方案2】:

      首先,我会在 HTML 中添加“或”,以便屏幕阅读器可以正确地大声朗读出来。然后您可以将该元素相对于整个容器定位,百分比是。您可以使用left: 50% 将其精确地移到中间,然后使用transform: translateX(-50%) 将其一半宽度移回左侧(这样您就不必知道“或”元素的宽度)。

      .content {
          display: flex;
          flex-direction: row;
          justify-content: space-evenly;
          text-align: center;
          margin: 20px;
          position: relative;
      }
      
      .cc-content {
          width: 50%;
          padding: 5px 20px;
          position: relative;
          background-color: red;
      }
      
      .cc-content-1 {
        margin-right: 3px;
      }
      
      .cc-content-2 {
        margin-left: 3px;
      }
      
      .or {
          display: inline-block;
          position: absolute;
          vertical-align: middle;
          line-height: 60px;
          top: 20px;
          left: 50%;
          transform: translateX(-50%);
          z-index: 1;
          font-size: 21px;
          font-weight: bold;
          border-radius: 100%;
          width: 60px;
          height: 60px;
          background-color: #F2F2F2;
          color: #006AAD;
          text-transform: uppercase;
      }
      <div class="content">
            <div class="cc-content cc-content-1">
              <h3>Header Here</h3>
              <p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
            </div>
            <div class="or">or</div>
            <div class="cc-content cc-content-2">
              <h3>Header Here</h3>
              <p>Lorem Ipsuom gshjgshj gshjgsajhusgs gsaiyusgisysgsyigs</p>
            </div>
      </div>

      【讨论】:

        【解决方案3】:

        您可以使用left: calc(100% - 30px) 代替left: 90%

        30px 是伪元素宽度的一半。

        【讨论】:

        • 我认为应该是 33px 来考虑每个 div 上 3px 的边距。
        猜你喜欢
        • 2020-05-21
        • 2022-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-11
        • 2021-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多