How to make a div grow with content?

后端 未结 5 721
再見小時候
再見小時候 2021-02-05 01:40

Pertaining to html, how do I make a div container grow with the content instead of manually defining a width and height.

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 02:01

    The default behaviour for divs is to fill the entire available width. A few ways to override this:

    • set display: inline-block (not IE-friendly)
    • float it (with the side effect of, well, floating it)
    • set display: inline (but that's almost never what you want)
    • set position: absolute
    • hard-code a width (no dynamic width though)

    As a last resort, consider javascript.

提交回复
热议问题