tags:

views:

694

answers:

5

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.

Thanks in advance!

EDIT: I guess code would help! 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.

+6  A: 

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.

Gerald Senarclens de Grancy
Some browsers will let you use display:inline-block on any element, but IE6/7 only accept it on elements that are natively display:inline. As usual, IE spoils the party for everyone.
Gabriel Hurley
A: 

A header tag (h1-h6) is a block-level element and thus fills the width of the container it's in. Changing it to

display:inline

will bring the background in to just the width of the text. This will introduce other issues though (since inline elements don't behave like block elements in lots of other ways).

The alternative is to set a width for the header itself. If you wanted to be really tricky you could do that with javascript so that it calculates it for each header specifically.

You'll have to play around a bit and figure out what works in your situation.

Gabriel Hurley
A: 

I'm not sure but you could add the backround-color to your headers with css.

Time Machine
It would be nice if you add some css samples here
chermosillo
+1  A: 

as others mentioned display:inline-block; would be an option, but does not work in all broswers. what i could think of right now is giving the headers a float:left; and the element that follows a clear:left;

another way - not as clean - is to add spans around your heading's text, and give that spans a background color:

<h1><span>your text</span></h1>

h1 span { background-color:#369; }
knittl
+1  A: 

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;
}
Emily
Unfortunately this makes the content clear the left column as well as clearing each other. (see http://adriantrimble.com/headers/clear.html). Thanks though!
Adrian Trimble
Then float the right column left and the clear won't clear the left column. I added the code to my post.
Emily
Oh great, thank you so much!
Adrian Trimble