html - 将按钮置于 3 个 div 下方

标签 html css flexbox

我有 3 个 div(彩色方 block )和一个按钮。

如何将按钮置于 div 下方?

在我当前的代码中,该按钮与#squares 出现在同一行,并且 float 到左侧。感谢您的回答。

#div1 {
  background-color: red;
  width: 100px;
  height: 100px;
  margin-right: 10px;
}
#div2 {
  background-color: green;
  width: 100px;
  height: 100px;
  margin-right: 10px;
}
#div3 {
  background-color: blue;
  width: 100px;
  height: 100px;
}
#squares {
  display: flex;
  position: absolute;
  margin-left: 35%;
}
#button {
  width: 100px;
  height: 50px;
  clear: both;
}
<div id="squares">
  <div id="div1"></div>
  <div id="div2"></div>
  <div id="div3"></div>
</div>
<div id="button">
  <button>Click me!</button>
</div>

最佳答案

由于您已经在使用 Flexbox,因此有多种解决方案,而且都相对简单。

这是一个:

  • 将两个容器包装在一个父容器中。
  • display: flexflex-direction: column 添加到这个新容器。
  • 现在您可以轻松地垂直和水平对齐方 block 和按钮。

#container {
  display: flex;
  flex-direction: column;
}
#div1 {
  background-color: red;
  width: 100px;
  height: 100px;
}
#div2 {
  background-color: green;
  width: 100px;
  height: 100px;
  margin: 0 10px;
}
#div3 {
  background-color: blue;
  width: 100px;
  height: 100px;
}
#squares {
  display: flex;
  align-self: center;
  margin-bottom: 10px;
}
#button {
  align-self: center;
  text-align: center;
  width: 100px;
  height: 50px;
}
<div id="container">
  <div id="squares">
    <div id="div1"></div>
    <div id="div2"></div>
    <div id="div3"></div>
  </div>
  <div id="button">
    <button>Click me!</button>
  </div>
</div>

关于html - 将按钮置于 3 个 div 下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39185684/

相关文章:

html - 使用 flexbox 和 100% 高度时 iPhone 6 和 7 上的 div 高度错误

javascript - 如何将 HTML 字符串插入到 div 容器中,并在 "flex-wrap"行中显示这些容器?

javascript - Bootstrap 在粘性导航上方插入 div

html - 无法使子元素与父元素IE高度相同

添加自定义类后 HTML 输入类型提交奇怪的行为

html - 图片排不下

html - 如何在父 flex 行 div 的末尾显示子 div,在父 div 的开头显示另外两个 div?

javascript - Html 地理定位在我的移动网络应用程序上不起作用 | Spring MVC

javascript - 单选按钮的 OnChange 事件处理程序(输入类型 ="radio")不能作为一个值工作

html - 在 Firefox 和 border-collapse : collapse; 中不存在单元格的表上隐藏边框