同一行上的两个 Div 并居中对齐
我有两个这样的 div,
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
我希望它们显示在同一行,所以我使用了 float:left
。
我希望它们都位于页面的中心,所以我尝试用另一个这样的 div 来包裹它们,
<div style="width:100%; margin:0px auto;">
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
</div>
但它不起作用。如果我将代码更改为此
<div style="width:100%; margin-left:50%; margin-right:50%">
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
</div>
,那么它就会到中心,但是水平滚动条在那里,而且看起来它也没有真正居中。
您能建议我如何实现这一目标吗?谢谢。
编辑:我希望内部 div(Div 1 和 Div 2)也居中对齐。
I have two divs like this
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
I want them to display on the same row, so I used float:left
.
I want both of them to be at center of the page as well, so I tried to wrap them with another div like this
<div style="width:100%; margin:0px auto;">
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
</div>
But it doesn't work. If I change the code to this
<div style="width:100%; margin-left:50%; margin-right:50%">
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
</div>
then it's going to the center, but the horizontal scrollbar is there and it seems like it's not really centered as well.
Can you please kindly suggest to me how can I achieve this? Thanks.
Edit: I want the inner div (Div 1 and Div 2) to be center align as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你可以这样做
http://jsfiddle.net/jasongennaro/MZrym/
div
与text-align:center;
div
sadisplay:inline-block;
而不是 afloat
最好将该 css 放入样式表中。
You could do this
http://jsfiddle.net/jasongennaro/MZrym/
div
withtext-align:center;
div
s adisplay:inline-block;
instead of afloat
Best also to put that css in a stylesheet.
这对你有用吗?检查我的 JSFiddle
和代码:
HTML
CSS
Could this do for you? Check my JSFiddle
And the code:
HTML
CSS
两个浮动的 div 都需要有宽度!
将两者的宽度都设置为 50%,就可以了。
顺便说一句,外部 div 的
margin: 0 auto
只会将其自身居中,而不是内部的 div。both floated divs need to have a width!
set 50% of width to both and it works.
BTW, the outer div, with its
margin: 0 auto
will only center itself not the ones inside.使用
display: inline-block
和text-align: center
居中对齐。使用
display: flex
和justify-content: center
居中对齐使用
display: flex
、justify-content: center
和align-items:center
垂直和水平居中对齐。Align to the center, using
display: inline-block
andtext-align: center
.Align to the center using
display: flex
andjustify-content: center
Align to the center vertically and horizontally using
display: flex
,justify-content: center
andalign-items:center
.我会投票反对
display: inline-block
因为它不支持跨浏览器,IE <具体8.编辑:如果不需要单元格之间有间距,只需将
.left
和.right
更改为使用float: left;
代码>I would vote against
display: inline-block
since its not supported across browsers, IE < 8 specifically.EDIT: If no spacing between the cells is desired just change both
.left
and.right
to usefloat: left;
到目前为止更好的方法:
如果你给出 display:inline-block;到内部div,那么内部div的子元素也会获得这个属性并干扰内部div的对齐。
更好的方法是对具有宽度、边距和浮动的内部 div 使用两个不同的类。
到目前为止最好的方法:
使用flexbox。
http://css-tricks.com/snippets/css/a- Flexbox 指南/
Better way till now:
If you give display:inline-block; to inner divs then child elements of inner divs will also get this property and disturb alignment of inner divs.
Better way is to use two different classes for inner divs with width, margin and float.
Best way till now:
Use flexbox.
http://css-tricks.com/snippets/css/a-guide-to-flexbox/
请看一下 flex 它会帮助你把事情做好,
在主 div 设置 css
display :flex
里面的 div 设置 css:
flex:1 1 auto;
附加 jsfiddle 链接作为示例享受:)
https://jsfiddle.net/hodca/v1uLsxbg/
Please take a look on flex it will help you make things right,
on the main div set css
display :flex
the div's that inside set css:
flex:1 1 auto;
attached jsfiddle link as example enjoy :)
https://jsfiddle.net/hodca/v1uLsxbg/