Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Width of Headers (H1, H2 etc)

Tags:

css

width

header

I want my headers (h1 through h6) to have a background colour (different to that of the page background), I also want the width of this background to be the same as the width of the text in the header, plus padding (Not the width of the container that the header is in, which is what is happening now).

There isn't much point in me showing you any code as this is fairly simple (or it should be anyway!). The container is a fixed width. Right now I have just some margins, padding, background colour and font styling set for the h1, h2, h3 etc tags.

EDIT: I guess code would help! https://web.archive.org/web/20090724165158/http://adriantrimble.com/headers (this has Gerald's solution applied, although obviously this would still not work in IE6/7 and still has problems in newer browsers). Using display:inline causes more problems than it solves, using float: left and clear: left as mentioned has problems because of the 2 column layout. Thanks for everyones help so far.

like image 813
Adrian Trimble Avatar asked Jul 21 '09 18:07

Adrian Trimble


People also ask

What is the width of a H1?

Hummer H1 Length, Width, Height & Wheelbase Overview The Hummer H1 is 15.4 feet (4.69 meters) long. The Hummer H1 is 7.2 feet (2.2 meters) wide. The Hummer H1 has a height ranging from 6.2 feet (1.91 meters) to 6.6 feet (2.01 meters), depending on the trim and model year.

Is H1 is larger size than H2?

H2 heading tag Like an H1 tag, an H2 tag also appears larger than the rest of your main body text. However, H2 tags are always slightly smaller than your H1 tag, so that it does not compete with your main heading.

What is H1 H2 H3?

The structure of H1, H2, H3 tags For an article or webpage, remember that the H1 title is the most important section. H2 and H3 are used to organize sub-sections, while H4, H5 and H6 are intended to provide additional information, with more details.

Is H1 or H3 bigger?

The H1 heading should be the largest heading on the page. The H2 heading is a little smaller than the H1 , and in turn the H3 heading is smaller than then H2 heading and so on.


2 Answers

h1-h6 are block level elements and even if they are not in a container (or just body) they'll stretch over the width of the window. One solution is to change their display to be inline-block instead:

<h1 style="background-color:pink; display:inline-block; padding:1em;">Testheader</h1>

Note that you'd have to make a manual break after the header if the next element isn't a block-level element like p.

like image 60
Gerald Senarclens de Grancy Avatar answered Oct 16 '22 08:10

Gerald Senarclens de Grancy


I would use

#rightcol h1, #rightcol h2, #rightcol h3, #rightcol h4, #rightcol h6, #rightcol h6 {
   float:left;
   clear:left;
}
#rightcol p {clear:left;} 

edit after comment

If the containing div is floated, the clear won't clear the left col. So float rightcol left and remove the margin

#rightcol {
   float:left;
   padding:70px 20px 20px 0px;
   width:585px;
}
like image 17
Emily Avatar answered Oct 16 '22 08:10

Emily