【问题标题】:How to center an i tag inside a round div?如何在圆形 div 内将 i 标签居中?
【发布时间】:2019-03-14 12:44:47
【问题描述】:

我想创建一个圆形的 div,它的字体真棒 i 箭头标签居中 like this:

我试图将 i 标签包装在一个 div 中,并以 margin 0 auto 将其居中,但它并没有完美居中,我该怎么做?

这是我的html:

<div class="parent">
    <div class="child">
      <i class="fas fa-arrow-up"></i>
    </div>
</div>

还有我的 CSS:

.parent {
    width: 300px;
    background: #f00;
    height: 300px;
    margin: 0 auto;
    border-radius: 50%;
}

.child {
    width: 150px;
    margin: auto;
    height: 100px;
   }

和我的小提琴的链接: http://jsfiddle.net/philipkovachev9/8kgxL59f/4/

【问题讨论】:

标签: html css centering


【解决方案1】:

我宁愿只给&lt;i&gt; 一个圆圈而不需要添加任何div。 它缩短了您的代码,您将获得一个完美的循环。

HTML:

<div class="social>
  <i class="fas fa-arrow-up"></i>
</div>

CSS:

.social .fas {
  margin-right: 1rem;
  border: 2px #fff solid;
  border-radius: 50%;
  height: 20px;
  width: 20px;
  line-height: 20px;
  text-align: center;
  padding: 0.5rem;
}

【讨论】:

    【解决方案2】:
    .parent {
       width: 300px;
       background: #f00;
       height: 300px;
       margin: 0 auto;
       border-radius: 50%;
     }
    
    .child {
      font-size: 100px;
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      top: 25%;
      left: 50%;
      transform: translate(-50%, -50%);
     }
    

    【讨论】:

      【解决方案3】:

      您可以stack fontawesome icons,所以只需使用圆圈和箭头:

      <link href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" rel="stylesheet"/>
      <span class="fa-stack fa-2x">
        <i class="far fa-circle fa-stack-2x"></i>
        <i class="fas fa-arrow-up fa-stack-1x"></i>
      </span>

      【讨论】:

      • 很好,但是这样我就不能用不同的颜色来设置圆的边框
      • @Rupert 当然可以。 jsfiddle.net/j08691/n592t8rw。如果这是您的要求,您应该在问题中提及。
      【解决方案4】:

      你可以这样居中:

      .parent {
        width: 300px;
        background: #f00;
        height: 300px;
        margin: 0 auto;
        border-radius: 50%;
      }
      
      .child {
        width: 150px;
        margin: auto;
        height: 100px;
        position: relative;
      }
      
      .child i {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
      }
      

      【讨论】:

        猜你喜欢
        • 2018-04-19
        • 2011-11-20
        • 2011-06-28
        • 2020-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-14
        • 1970-01-01
        相关资源
        最近更新 更多