中心图标垂直和水平,文本正好在div边框上方

时间:2017-01-28 11:07:02

标签: html css css3 flexbox vertical-alignment

我希望 sometext 位于div边框之上。

.reg_form_fields_icon_box {
  margin: 0 auto;
  display: flex;
  width: 80px;
  height: 80px;
  border: thin #dee5e7 solid;
}
.reg_form_fields_icon_box .reg_form_fields_icon {
  margin: auto;
}
.reg_form_fields_icon_box .reg_form_fields_icon i {
  font-size: 20px;
}
.reg_form_fields_icon_box .reg_form_fields_icon .text_name {
  vertical-align: bottom;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div class="reg_form_fields_icon_box text-center">
  <div class="reg_form_fields_icon">
    <i class="glyphicon glyphicon-font"></i>
    <div class="m-t-md text_name">
      <span>sometext</span>
    </div>
  </div>
</div>

just like in the image

任何帮助都会很棒。

谢谢。

3 个答案:

答案 0 :(得分:1)

您可以使用flexbox尝试以下解决方案:

.reg_form_fields_icon_box {
  margin: 0 auto;
  width: 80px;
  height: 80px;
  border: thin #dee5e7 solid; 
}
.reg_form_fields_icon_box .reg_form_fields_icon {
  display:flex;
  flex-direction:column;
  width:100%;
  height:100%;
  text-align:center;
  justify-content:space-between;
}
.reg_form_fields_icon_box .reg_form_fields_icon i {
  font-size: 20px;
  top:calc(50% - 20px);
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div class="reg_form_fields_icon_box text-center">
  <div class="reg_form_fields_icon">
    <i class="glyphicon glyphicon-font"></i>
    <div class="m-t-md text_name">
      <span>sometext</span>
    </div>
  </div>
</div>

答案 1 :(得分:0)

添加以下css:

div.reg_form_fields_icon_box {
    position: relative;
}

div.reg_form_fields_icon_box div.reg_form_fields_icon div.text_name{
    position: absolute;
    bottom: 0;
}

答案 2 :(得分:0)

你可以自己flexbox这样做,你不需要calc来设置位置 - 位置绝对 - 所以这就是我所做的:< / p>

  1. 您可以使reg_form_fields_icon_box成为flexbox 垂直水平对齐。

  2. 重置margin: auto并将其设置为100%heightwidth

    .reg_form_fields_icon_box .reg_form_fields_icon {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100%;
      width: 100%;
      position: relative;
      margin: 0;
    }
    
  3. 现在定位reg_form_fields_icon 绝对并使用以下内容将其与 bottom 对齐:

    .reg_form_fields_icon_box .reg_form_fields_icon .text_name {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
    }
    
  4. 见下面的演示:

    &#13;
    &#13;
    .reg_form_fields_icon_box {
      margin: 0 auto;
      display: flex;
      width: 80px;
      height: 80px;
      border: thin #dee5e7 solid;
    }
    .reg_form_fields_icon_box .reg_form_fields_icon {
      margin: auto;
    }
    .reg_form_fields_icon_box .reg_form_fields_icon i {
      font-size: 20px;
    }
    .reg_form_fields_icon_box .reg_form_fields_icon .text_name {
      vertical-align: bottom;
    }
    /*ADDED THESE*/
    
    .reg_form_fields_icon_box .reg_form_fields_icon {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100%;
      width: 100%;
      position: relative;
      margin: 0;
    }
    .reg_form_fields_icon_box .reg_form_fields_icon .text_name {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
    }
    &#13;
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
    <div class="reg_form_fields_icon_box text-center">
      <div class="reg_form_fields_icon">
        <i class="glyphicon glyphicon-font"></i>
        <div class="m-t-md text_name">
          <span>sometext</span>
        </div>
      </div>
    </div>
    &#13;
    &#13;
    &#13;