【问题标题】:Bootstrap 4 - align an element to the bottom of its containerBootstrap 4 - 将元素与其容器底部对齐
【发布时间】:2019-02-08 16:13:28
【问题描述】:

我试图找到答案,但没有运气,我自己也尝试了一些事情。 我一行中有两列,我想将第二列(三个按钮组)的底部div垂直对齐到行容器的底部,即#control-panel。

目前第二列是居中对齐(垂直),这就是为什么三个按钮没有附加到容器底部的原因。

我还附上了一张图片,以便于查看

 <div id="control-panel">
    <div class="container">
      <div class="row">

        <div class="col-lg-3">
          <div class="display-4 p-3"><h2>Step 3:</h2></div>
          <p>Add a contact form, if you like, so people can contact you!</p>
          <a href="" class="btn btn-primary">Show contact form</a>
        </div>
        <div class="col-lg-3 d-flex flex-column">
          <div class=" p-3"><h2>Step 4:</h2></div>
            <p>Pick a style for your website</p>
            <div class="btn-group align-self-bottom">
              <button class="btn btn-primary" type="button">Style 1</button>
              <button class="btn btn-secondary" type="button">Style 2</button>
              <button class="btn btn-primary" type="button">Style 3</button>
          </div>
        </div>
      </div>
    </div>
  </div>

容器的 CSS 只是:

#control-panel {
height: 200px
}

我尝试将 'align-self-bottom' 或 'align-self-end' 用于我想要对齐到底部的 'btn-group' 类,但它水平移动而不是垂直移动我想要.

非常感谢您的任何建议

【问题讨论】:

  • #control-panel 是容器吗?您发布的 html 代码中没有包含 id 为 control-panel 的元素
  • 我尝试了您的代码,按钮与底部对齐。你能更详细地解释你的问题吗?也许在 StackBlitz 中发布您的代码示例。
  • 大家好,我很抱歉没有很好地解释自己,我编辑了我的帖子以便于理解问题。再次提前感谢

标签: twitter-bootstrap alignment


【解决方案1】:

查看此 StackBlitz:FlexBox example

HTML 文件:

<div class="main-container">
  <div class="first-column">

  </div>

  <div class="second-column">
    <div class="buttons">

      <button>Button1</button>
      <button>Button2</button>
    </div>
  </div>
</div>

CSS 文件:

.main-container {
  border-color: red;;
  border-style: solid;
  height: 100px;

  display: flex;
  flex-direction: row;
  justify-content: space-around;
}

.first-column {
  border-color: blue;;
  border-style: solid;
  height: 100px;
  width: 100px;
}

.second-column {
  border-color: green;;
  border-style: solid;
  height: 100px;
  width: 100px;
}

.buttons {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;

  height: 100px;
}

我在这里使用 flexbox 创建一个主容器(红色)和内部的两列(蓝色和绿色),然后在第二列中创建两个按钮并将它们与容器的底部对齐。

【讨论】:

  • 哇非常感谢胡安的详细回答!很有帮助
猜你喜欢
  • 2018-05-23
  • 1970-01-01
  • 2014-12-03
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 2021-07-12
  • 2019-07-03
相关资源
最近更新 更多