[英]HTML/CSS align multiple elements within div
I wonder how to align three elements in one div. 我想知道如何在一个div中对齐三个元素。 Basically, it should look like this
基本上应该是这样的
Solved. 解决了。
For future readers, the solution for my problem looks like this 对于未来的读者,我的问题的解决方案看起来像这样
<div style="text-align: center;"> <span style="float: left;">LEFT</span> <span style="margin: auto;">CENTER</span> <span style="float: right;">RIGHT</span> </div>
Try using float:left
and float:right
for left and right see this fiddle: 尝试对左和右使用
float:left
和float:right
right看看这个小提琴:
https://jsfiddle.net/u924gptz/ https://jsfiddle.net/u924gptz/
.container{ display: flex; // working with all latest browsers display: -webkit-flex; // for old version of safari display: -ms-flex; // for old versions of IE justify-content: space-between; } .container span{ flex: 1; text-align: center; }
<div class="container"> <span>LEFT</span> <span>CENTER</span> <span>RIGHT</span> </div>
The best thing is if you are adding more data inside container
. 最好的事情是,如果要在
container
中添加更多数据。 it will give equal spacing. 它将给出相等的间距。 And inline styling is not a better option
内联样式不是更好的选择
Please do follow certain things like not have any styles inline 请注意某些事情,例如没有任何内联样式
.wrapper {
text-align: center;
}
.left {
float:left;
}
.center {
margin: auto;
}
.right {
float: right;
}
<div class="wrapper">
<span class="left">LEFT</span>
<span class="center">CENTER</span>
<span class="right">RIGHT</span>
</div>
All 3 spans should have separate stylings. 所有3个跨度应具有单独的样式。 Here:
这里:
<div style="text-align: center;">
<span style="float: left;">LEFT</span>
<span style="margin: auto;">CENTER</span>
<span style="float: right;">RIGHT</span>
</div>
This should do it. 这应该做。
well there are a number of ways to do this. 嗯,有很多方法可以做到这一点。 but you could use something like this
但是你可以用这样的东西
div { display:flex; align-items: center;} span:first-child {float:left;} span:nth-child(2) {margin:0 auto} span:last-child {float:right}
<div> <span>LEFT</span> <span>CENTER</span> <span>RIGHT</span> </div>
Use DIVs instead of spans and this simple CSS for the container element: 使用DIV代替spans和以下简单CSS作为容器元素:
.x {
display: flex;
justify-content: space-between;
}
display: flex
does the equal distirbution, justify-content: space-between;
display: flex
做相等的分配, justify-content: space-between;
makes the outmost elements align at the borders. 使最外面的元素在边界对齐。
.x { display: flex; justify-content: space-between; }
<div class="x"> <div>LEFT</div> <div>CENTER</div> <div>RIGHT</div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.