如何让div自动设置自己的宽度?

发布于 2024-11-01 21:42:06 字数 241 浏览 1 评论 0原文

如何让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

策马西风 2024-11-08 21:42:07

是块元素。他们采用容器的整个宽度。时期。就像

您上面的规则本质上是这样做的:
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 true inline-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

海之角 2024-11-08 21:42:07

对于那些仍然遇到此问题的人,现在您可以使用以下方法处理此问题:

<div>
  <div style="display: table; margin: auto">
    This will expand only as far as it needs to
  </div>
</div>

这是一个基于已接受答案的新小提琴: http://jsfiddle.net/j33qL8pa/1/

For those of you who are still coming across this issue, nowadays you can handle this with:

<div>
  <div style="display: table; margin: auto">
    This will expand only as far as it needs to
  </div>
</div>

Here is a new fiddle based on the accepted answer: http://jsfiddle.net/j33qL8pa/1/

_畞蕅 2024-11-08 21:42:06

使用 inline-block 的解决方案

您可以尝试 display: inline-block 并查看它是否适合您的情况。但是,您将无法使用 margin-left: auto & 将其居中。 margin-right: auto 因为该技术需要宽度值。

如果可能,请使用 display: inline-block 并在其容器上设置 text-align: center

<div style="text-align: center">
  <div style="display: inline-block;">
     This will expand only as far as it needs to
  </div>
</div>

使用 Flexbox + 容器 div 的解决方案

上面的原始答案是在 2011 年编写的,当时 Flexbox 尚未实现。您可以实现类似的效果

<div style="display: flex; justify-content: center;">
  <div>
     This will expand only as far as it needs to
  </div>
</div>

没有容器 div 的解决方案

还有另一种方法可以在没有父元素的情况下实现此目的。

  1. 将 display 设置为 inline-block 以使 div 宽度适合其内容。
  2. 使用 position:relativeleft: 50% 将其左边缘定位到其包含元素的 50%。
  3. 使用 transform:translateX(-50%) 会将元素向左“移动”其自身宽度的一半。
.center {
  display: inline-block;
  position: relative;
  left: 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 using margin-left: auto & margin-right: auto because that technique requires a width value.

If possible, use display: inline-block and set text-align: center on it's container.

<div style="text-align: center">
  <div style="display: inline-block;">
     This will expand only as far as it needs to
  </div>
</div>

Solution using Flexbox + container div

The original answer above was written in 2011, before flexbox was implemented. You can achieve a similar effect

<div style="display: flex; justify-content: center;">
  <div>
     This will expand only as far as it needs to
  </div>
</div>

Solution without container div

There is another way to do this without a parent element.

  1. Set display to inline-block to make the div width fit to its content.
  2. Use position: relative and left: 50% to position its left edge to 50% of its containing element.
  3. Use transform: translateX(-50%) which will "shift" the element to the left by half its own width.
.center {
  display: inline-block;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}
半城柳色半声笛 2024-11-08 21:42:06

我认为这在当今有效:

.center
{
    width: fit-content;
    margin-left: auto;
    margin-right: auto;
}

I think this works nowadays:

.center
{
    width: fit-content;
    margin-left: auto;
    margin-right: auto;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文