【问题标题】:Pseudo elements that don't affect centered HTML elements不影响居中 HTML 元素的伪元素
【发布时间】:2017-09-16 17:06:57
【问题描述】:

我试图在一个与元素中心完全对齐的圆圈内显示一个百分比。酷,这行得通。

但是,我希望数值居中对齐,而“%”浮动在旁边并且不影响放置。

见codepen:https://codepen.io/brycesnyder/pen/MEwddv

div {
  font-family: sans-serif;
  height: 200px;
  width: 200px;
  background: #ae63e4;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
  line-height: 1;
  color: white;
  text-align: center;
}

div.percent:after {
  content: '%';
  font-size: 30px;
  margin-bottom: 15px;
}

div.a-percent {
  position: relative;
  background: #0ebeff;
}

div.a-percent:after {
  content: '%';
  position: absolute;
  font-size: 30px;
  margin-top: 5px;
}
<div class="percent">0</div>
<div class="percent">10</div>
<div class="percent">100</div>

<br>
<br>

<div class="a-percent">0</div>
<div class="a-percent">10</div>
<div class="a-percent">100</div>

【问题讨论】:

    标签: html css flexbox centering


    【解决方案1】:

    为什么不在数字的左侧添加一个相同的百分号?

    然后用visibility: hidden隐藏这个重复。

    这在容器中创建了相等的平衡,并且数字可以完全居中。

    div {
      font-family: sans-serif;
      height: 200px;
      width: 200px;
      background: #ae63e4;
      border-radius: 50%;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      font-size: 60px;
      line-height: 1;
      color: white;
      text-align: center;
    }
    div::after {
      content: '%';
      font-size: 30px;
      margin-bottom: 15px;
    }
    div::before {
      content: '%';
      font-size: 30px;
      margin-bottom: 15px;
      visibility: hidden;
    }
    div.a-percent {
      background: #0ebeff;
    }
    <div class="percent">0</div>
    <div class="percent">10</div>
    <div class="percent">100</div>
    
    <br>
    <br>
    
    <div class="a-percent">0</div>
    <div class="a-percent">10</div>
    <div class="a-percent">100</div>

    更多细节和其他选项在这里:Center and right align flexbox elements

    【讨论】:

      【解决方案2】:

      看来我已经设法通过简单地将绝对位置添加到伪元素来弄清楚如何保持节奏。以前,我曾认为这会导致元素的位置为 0,0。

      div {
        height: 200px; width: 200px;
        background: maroon;
        border-radius: 50%;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
        color: white;
        &.percent {
          &:after {
            content: '%';
            position: absolute;
        }
      }
      

      【讨论】:

        【解决方案3】:

        试试这个:

        div {
            font-family: sans-serif;
            height: 200px; width: 200px;
            background: #ae63e4;
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-size: 60px;
            line-height: 1;
            color: white;
            text-align: center;
            &.percent {
                &:after {
                    content: '%';
                    font-size: 30px;
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-25
          • 2021-01-15
          • 1970-01-01
          • 2019-03-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多