如何防止标题(< h1>)占用超出其所需宽度的宽度?

问题描述:

如果我定义一个标题,例如<h1>,则默认情况下其宽度设置为100%.

If I define a heading like <h1>, its width is set to 100% by default.

是否有办法确保其宽度设置为可能的最小值?

Is there a way to make sure its width is set to the smallest value possible?

您可以给它display: inline.这将使其表现得像任何文本元素一样-<h1>的默认值为display: block.

You can give it display: inline. This will make it behave like any text element - the default for <h1> is display: block.

还有其他方法:例如float: left,但我发现这是最简单的方法,副作用最少.

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