如何让div自动设置自己的宽度?
如何让 DIV 的宽度表现得像设置了 float:left
时一样,但不设置浮动? width:auto
似乎没有做到这一点(在 Chrome 中)。
我正在尝试让类似以下的东西起作用......
.center
{
margin-left: auto;
margin-right: auto;
width: auto;
}
How can I have a DIV's width behave like it does when float:left
is set, but without setting a float? width:auto
doesn't seem to do it (in chrome).
I am trying to get something like the following to work...
.center
{
margin-left: auto;
margin-right: auto;
width: auto;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是块元素。他们采用容器的整个宽度。时期。就像
或
您上面的规则本质上是这样做的:
div { 宽度:100%;保证金:0 自动; }
因为 100% 是它的自动值。inline-block
可以工作,但它不会像真正的inline-block
元素一样工作。基本上...如果不设置宽度、定位它就无法做到这一点。
如果您选择内联块路线,请阅读本文...您最终可能会与您的 CSS 期望进行一些意想不到的战斗。
http://www.brunildo.org/test/InlineBlockLayout.html(链接位于附近以下 msdn 页面底部)
http://msdn.microsoft.com/en -us/library/ms530751%28v=vs.85%29.aspx
<div>
s are block elements. They take the full width of their containers. Period. Just like<body>
or<html>
Your rule above is essentially doing this:
div { width:100%; margin:0 auto; }
as 100% is its auto value.inline-block
would work, but it won't work exactly like a trueinline-block
element.Basically...you can't do it without setting a width, positioning it.
If you go the inline-block route, give this a read...you may end up fighting some unexpected battles with your CSS expectations.
http://www.brunildo.org/test/InlineBlockLayout.html (link found near the bottom of the following msdn page)
http://msdn.microsoft.com/en-us/library/ms530751%28v=vs.85%29.aspx
对于那些仍然遇到此问题的人,现在您可以使用以下方法处理此问题:
这是一个基于已接受答案的新小提琴: http://jsfiddle.net/j33qL8pa/1/
For those of you who are still coming across this issue, nowadays you can handle this with:
Here is a new fiddle based on the accepted answer: http://jsfiddle.net/j33qL8pa/1/
使用 inline-block 的解决方案
您可以尝试
display: inline-block
并查看它是否适合您的情况。但是,您将无法使用margin-left: auto
& 将其居中。margin-right: auto
因为该技术需要宽度值。如果可能,请使用
display: inline-block
并在其容器上设置text-align: center
。使用 Flexbox + 容器 div 的解决方案
上面的原始答案是在 2011 年编写的,当时 Flexbox 尚未实现。您可以实现类似的效果
没有容器 div 的解决方案
还有另一种方法可以在没有父元素的情况下实现此目的。
inline-block
以使 div 宽度适合其内容。position:relative
和left: 50%
将其左边缘定位到其包含元素的 50%。transform:translateX(-50%)
会将元素向左“移动”其自身宽度的一半。Solution with inline-block
You could try
display: inline-block
and see if that works in your situation. However, you won't be able to center it usingmargin-left: auto
&margin-right: auto
because that technique requires a width value.If possible, use
display: inline-block
and settext-align: center
on it's container.Solution using Flexbox + container div
The original answer above was written in 2011, before flexbox was implemented. You can achieve a similar effect
Solution without container div
There is another way to do this without a parent element.
inline-block
to make the div width fit to its content.position: relative
andleft: 50%
to position its left edge to 50% of its containing element.transform: translateX(-50%)
which will "shift" the element to the left by half its own width.我认为这在当今有效:
I think this works nowadays: