如何防止< H1>占用比他们需要更多的宽度

问题描述:

如果我制作< h1> 标题,默认情况下,其宽度设置为100%。有没有办法确保将其宽度设置为尽可能最小的值?我需要这个来做特殊的CSS边框。

If I make an <h1> title, its width is set to 100% by default. Is there a way to make sure its width is set to the smallest value possible? I need this to do special CSS stuff with borders.

你可以给它 display:inline ,这会使它表现得像任何文本元素一样( h1 的缺省值是 display:block >)。还有其他一些方法 - 例如 float:left - ,但我觉得这是最简单的方法,也是副作用最少的方法。

You can give it display: inline, this will make it behave like any text element (The default for h1 is display: block). There are other ways - float: left for example - , but I find this the simplest and the one with the fewest side effects.

请注意,您可能需要添加< br> 以确保在h1之后换行。

Note that you will then probably have to add a <br> to ensure a line break after the h1.