【问题标题】:Bootstrap vertically center heading div responsivelyBootstrap 响应地垂直居中标题 div
【发布时间】:2018-10-17 00:15:42
【问题描述】:

我正在尝试将 h5 在 div 中垂直居中,但我想响应式地进行,所以那些将父级的高度设置为一定数量像素的解决方案对我来说不是解决方案。

id 为“channelName”的 h5 是我试图在父 div 中垂直居中的。

<div class="col-10 text-left channelTab"><h5 id="channelName">Name</h5></div>

#greyBox {
  background-color: grey;
}

#title {
  color: white;
}

.logo {
  max-height: 50px;
  max-width: 50px;
  border-radius: 50%;
  border: 3px solid white;
}

#channelName {
  vertical-align: middle;
  display: inline-block;
}

#channelRow {
  background-color: red;
}
<div class="container-fluid">
  <div class="row">
    <div class="col-4"></div>
    <div id="greyBox" class="col-4 text-center">
      <div class="row">
        <div class="col-12 text-center">
          <h1 id="title">TITLE</h1>
          <div class="row" id="channelRow">
            <div class="col-2">
              <img src="image.png" class="logo"></div>
            <div class="col-10 text-left channelTab">
              <h5 id="channelName">Name</h5>
            </div>
          </div>

          <div class="col-6">

          </div>
        </div>
      </div>
    </div>
  </div>
</div>

如果可以通过对我的代码进行更多更改来更轻松地完成此操作,那么任何此类提示都将受到欢迎,因为我对此非常陌生并且仍在学习。

【问题讨论】:

    标签: html css bootstrap-4 centering


    【解决方案1】:

    将此类添加到 h5 d-flex align-items-center 的父级并删除 h5 的底部边距

    此类 (d-flex) 将使 h5 的父级显示 flex 并且align-items-center 将其垂直居中。

    #greyBox {
      background-color: grey;
    }
    
    #title {
      color: white;
    }
    
    .logo {
      max-height: 50px;
      max-width: 50px;
      border-radius: 50%;
      border: 3px solid white;
    }
    
    #channelName {
     margin:0;
    }
    
    #channelRow {
      background-color: red;
    }
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="container-fluid">
      <div class="row">
        <div class="col-4"></div>
        <div id="greyBox" class="col-4 text-center">
          <div class="row">
            <div class="col-12 text-center">
              <h1 id="title">TITLE</h1>
              <div class="row" id="channelRow">
                <div class="col-2">
                  <img src="image.png" class="logo"></div>
                <div class="col-10 text-left d-flex align-items-center channelTab">
                  <h5 id="channelName">Name</h5>
                </div>
              </div>
    
              <div class="col-6">
    
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

    • 它有效。只是一个额外的问题: "margin: 0" 背后的逻辑是什么?我可以看到如果没有这个,标题就不会完全位于中心,但我不明白为什么。那是因为 HTML/Bootstrap 中的所有标题都有默认边距吗?
    • @merkur0 boostrap 中的所有标题都有底部边距,我只是将其删除以正确垂直对齐
    猜你喜欢
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 2014-11-21
    • 2017-12-25
    • 2022-07-19
    • 2015-09-17
    • 2015-02-01
    • 2018-10-11
    相关资源
    最近更新 更多