如何水平居中可变宽度的浮动元素? [英] How to horizontally center a floating element of a variable width?
本文介绍了如何水平居中可变宽度的浮动元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何水平居中一个可变宽度的浮动元素?
How to horizontally center a floating element of a variable width?
编辑:我已经使用一个包含 div
用于浮动元素,并为容器指定 width
(然后使用 margin:0 auto;
容器)。我只想知道是否可以不使用一个包含元素,或者至少不必为包含元素指定 width
。
I already have this working using a containing div
for the floating element and specifying a width
for the container (then use margin: 0 auto;
for the container). I just wanted to know whether it can be done without using a containing element or at least without having to specify a width
for the containing element.
推荐答案
假设浮动的元素将以 div
> id =content
...
Assuming the element which is floated and will be centered is a div
with an id="content"
...
<body>
<div id="wrap">
<div id="content">
This will be centered
</div>
</div>
</body>
并应用以下 CSS:
#wrap {
float: left;
position: relative;
left: 50%;
}
#content {
float: left;
position: relative;
left: -50%;
}
这是一个很好的参考。
这篇关于如何水平居中可变宽度的浮动元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文