gpt4 book ai didi

html - 在 3 div 下居中一个按钮

转载 作者:行者123 更新时间:2023-11-27 23:24:40 27 4
gpt4 key购买 nike

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

如何让按钮在 div 下居中?

在我当前的代码中,按钮出现在与 #squares 相同的行中,并且它向左浮动。感谢您的回答。

#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 添加到这个新容器中。
  • 现在您可以轻松地在垂直和水平方向上对齐正方形和按钮。

#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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com