【问题标题】:FlexBox: align icons by left corner and keep same gap after themFlexBox:左角对齐图标并在它们之后保持相同的间隙
【发布时间】:2018-06-14 02:09:18
【问题描述】:

我有包含项目的 flexbox 容器。它们都按中心水平和垂直对齐。这通常呈现在实时网站上。但是我需要以这种方式将字体真棒图标与文本对齐: 1)。所有图标左角对齐
2)。图标之后和文本之前的间隙必须具有相同的长度。

这是CodePen 的草稿

所以显示图标没有正确对齐。

代码:

.contact-container {   
  display: -ms-flexbox;
  -ms-flex-wrap: wrap;
  -ms-flex-direction: column; 
  -webkit-box-orient: horizontal; 
  -webkit-box-direction: normal; 
      -ms-flex-flow: row wrap; 
          flex-flow: row wrap; 
  display: -webkit-box;
  display: flex;  
}
.contact-container .copy {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  justify-content: center;  
  -webkit-box-align:center;
      -ms-flex-align:center;
          align-items:center;
}

.contact-container .fa {
  font-size: 40px;    
  -ms-flex-negative: 0;    
      flex-shrink: 0;  
  padding-left:20px;
}
.contact-container .item > *{color: #9f6a4a;}
.contact-container .item { 
   -webkit-box-flex: 0; 
    flex: 0 1 100%;
  -ms-flex: 0 1 100%;
  flex: 0 1 47%; 
  background:#fff;
  color:#fff;      
}
.contact-container .item{
    /*height: calc(720px/4);*/
    -ms-flex-item-align: center;
        align-self: center;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
        -ms-flex-direction: column;
            flex-direction: column;
    text-align: center;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
        flex-wrap: wrap;
    -webkit-box-pack: center;
        -ms-flex-pack: center;
            justify-content: center;
    /*min-height: 100%;*/
    min-width: 50%;
}
.contact-container h2{font-size:1.1em;font-weight:500;}
.contact-container h3 {padding-left: 10px;}

【问题讨论】:

    标签: html css flexbox font-awesome


    【解决方案1】:

    根据定义,您不能左对齐居中的对象。您必须将父容器设置为左对齐图标/文本,然后提供左边距/宽度以允许它们匹配 - 因为每个单独的容器又在另一个容器中。

    编辑:一个粗略的例子,在这里:

    .contact-container {   
      display: flex;
      flex-flow: row wrap;
      align-items: center;
    }
    
    .item {
      width: 50%;
    }
    
    .item > * {
      color: #9f6a4a;
    }
    
    
    .copy {
      display: flex;
      flex-direction: row;
      align-items: center;
      width: 70%;
      margin: auto;
    }
    
    h3 {
      margin: 0 .5em;
    }
    
    i.fa {
      font-size: 40px;
      padding-left:20px;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    /* IGNORE THIS - for illustrative purpose only */
    .item:nth-child(1), .item:nth-child(4) {
      background-color: #DFD;
    }
    
    .item:nth-child(2), .item:nth-child(3) {
      background-color: #DDF;
    }
    <script src="https://use.fontawesome.com/97dc0a8baa.js"></script>
    <div class="contact-container">
        <div class="item">
            <div class="box">
                <div class="inner">
                    <div class="copy"> <i class="fa fa-mobile-phone"></i>
                        <h3 class="sppb-addon-title sppb-feature-box-title sppb-media-heading"><a href="tel:+37068713383">+1 123 45 678 99</a></h3></div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="box">
                <div class="inner">
                    <div class="copy"> <i class="fa fa-envelope-o"></i>
                        <h3 class="sppb-addon-title sppb-feature-box-title sppb-media-heading"> <span>info@info.com</span></h3></div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="box">
                <div class="inner">
                    <div class="copy"> <i class="fa fa-map-marker"></i>
                        <h3 class="sppb-addon-title sppb-feature-box-title sppb-media-heading">City, Street 22</h3></div>
                </div>
            </div>
        </div>
        <div class="item">
            <div class="box">
                <div class="inner">
                    <div class="copy"> <i class="fa fa-calendar"></i>
                        <h3 class="sppb-addon-title sppb-feature-box-title sppb-media-heading">I-V 09:00-17:00<br>&nbsp;VI 09:00-14:00<br>VII closed</h3></div>
                </div>
            </div>
        </div>
    </div>

    【讨论】:

    • 所以,我可以将justify-content: flex-start; 添加到.copy div 以便将它们左角对齐,然后将它们推到margin-left:xx% 的中心?
    • @Kuzma 对。我在答案中添加了一个快速示例,并在实践中展示了它。不幸的是,至少在没有 JS 的情况下,在保持图标对齐的同时给出“真正的”中心对齐是不可能的。
    猜你喜欢
    • 2017-07-11
    • 2017-06-29
    • 2015-01-17
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多