How can I let a div automatically set it own width?

后端 未结 4 1475
攒了一身酷
攒了一身酷 2021-02-04 10:35

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).

4条回答
  •  眼角桃花
    2021-02-04 10:41

    s are block elements. They take the full width of their containers. Period. Just like or

    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

提交回复
热议问题