How to prevent headings (

) from taking up more

2020-02-26 03:32发布

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?

标签: html css width
4条回答
【Aperson】
2楼-- · 2020-02-26 03:44

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.

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

查看更多
相关推荐>>
3楼-- · 2020-02-26 03:46

Instead of display: inline, give it display: inline-block. This will retain the block-like qualities of the h1 tag, except for the 100% width.

查看更多
爷的心禁止访问
4楼-- · 2020-02-26 03:48

It's a block level element, so it follows the rules of display:block. You can set display:inline, which may do what you need or not, depending on the context.

查看更多
家丑人穷心不美
5楼-- · 2020-02-26 03:48

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

I think that width: fit-content; is better.

https://jsfiddle.net/fqk1a2v6/

查看更多
登录 后发表回答