Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css h1 - only as wide as the text

Tags:

css

I have an H1 style for my site:

.centercol h1 {     color: #006bb6;     font-weight: normal;     font-size: 18px;     padding:3px 3px 3px 6px;     border-left:3px solid #c6c1b8;     background:#f2efe9;     display:block; } 

The background color spans the entire width of the centercol, 500px...

How do I make this H1 only span the width of the text of the H1?

like image 360
tony noriega Avatar asked Dec 30 '10 20:12

tony noriega


People also ask

How can I make h1 text wider?

Specify a width on the <h1> and use margin: 0 auto; if you want it centered. Or, alternatively, you could float the <h1> , which would make it only exactly as wide as the text.

Can h1 have padding?

There is no padding. There is just space allocated by the default line-height and no characters with descenders (like p, q, and g) in the text. Show activity on this post. Every font has their own vertical space which is called line-height.


2 Answers

You can use the inline-block value for display, however in this case you will loose the block feature of h1 i.e. the siblings will be displayed inline with h1 if they are inline elements(in which case you can use a line-break
).

display:inline-block;  
like image 184
Chandu Avatar answered Sep 17 '22 12:09

Chandu


.h1 {   width: -moz-fit-content;   width: fit-content;    // workaround for IE11   display: table; } 

All modern browsers support width: fit-content for that.

For IE11 we could emulate this behavior with display: table which doesn't break margin collapse like display: inline-block or float: left.

like image 45
Ihor Zenich Avatar answered Sep 19 '22 12:09

Ihor Zenich