[英]How can I center 2 button inside a div horizontally?
I've a div (1140 x 180) and I need to set two buttons inside, next to each other. 我有一个div(1140 x 180),我需要在内部设置两个按钮,彼此相邻。 Ideally, one button occupies the entire left side, and the other all the right side. 理想情况下,一个按钮占据整个左侧,另一个按钮占据整个右侧。 All this with "Flex", because I must respect the responsive design. 所有这些都使用“ Flex”,因为我必须尊重响应式设计。
You can do this very simply by using the flex
property like so: 您可以通过使用flex
属性非常简单地执行此操作,如下所示:
.container {
display:flex;
width: 1140px;
height: 180px;
}
.button {
flex:1;
height:100%;
}
<div class=“container”>
<div class=“button”>first button</div>
<div class=“button”>second button</div>
</div>
Here is code snippet to understand that buttons are horizontally and vertically center. 这是一些代码片段,以了解按钮在水平和垂直方向上居中。
Here is a document on how to align items with display: flex
这是有关如何使display: flex
与项目对齐的文档 display: flex
Snippet: 片段:
.container{ display: flex; align-items: center; justify-content: center; border: 1px solid red; width: 1140px; height: 180px; } .button{ display:inline-block; height: 50px; width: 100px; }
<div class="container"> <button class="button">Button1</button> <button class="button">Button2</button> </div>
Based on the Claire answer. 基于克莱尔的答案。 Here is a solution: 这是一个解决方案:
CSS: CSS:
.container {
display:flex;
width: 1140px;
height: 180px;
}
.button {
width: 50%;
display: inline-flex;
justify-content: center;
}
HTML(same): HTML(相同):
<div class=“container”>
<div class=“button”>first button</div>
<div class=“button”>second button</div>
</div>
您可以将两个按钮都放在div中,并在外部div中将该div居中。
Well, If the div has display:flex;
好吧,如果div有display:flex;
, then you can justify-content:center
. ,那么您就可以justify-content:center
。 It will align all items to the center. 它将所有项目与中心对齐。
You can study more about justify-content from https://www.w3schools.com/cssref/css3_pr_justify-content.asp 您可以从https://www.w3schools.com/cssref/css3_pr_justify-content.asp了解更多有关证明内容的信息
If the div is display:block
which is by default div styles. 如果div为display:block
,则默认为div样式。 you can use text-align:center
on parent div. 您可以在父div上使用text-align:center
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.