Div Size Automatically size of content

前端 未结 7 698
萌比男神i
萌比男神i 2021-02-03 17:07

I am trying to make a h2 header for sidebar widgets but I want the width of the div class to be whatever width the content becomes. It seems I can\'t just set a width because th

7条回答
  •  孤城傲影
    2021-02-03 17:39

    If you are coming here, there is high chance width: min-content or width: max-content can fix your problem. This can force an element to use the smallest or largest space the browser could choose…

    This is the modern solution. Here is a small tutorial for that.

    There is also fit-content, which often works like min-content, but is more flexible. (But also has worse browser support.)

    This is a quite new feature and some browsers do not support it yet, but browser support is growing. See the current browser status here.

提交回复
热议问题