shapper wrote:
Hello,
I am using CSS to apply a background to a header:
<h1>Header</h1>
h1 {background-color: black;}
The problem is that the background color is applied not only to the
text but to the full lenght of the h1.
How to apply it only to the header text?
A it is supposed to do by default. Her are your options:
1) Set the with explicitly
h1 { color: white; background-color: black; width: 3.25em; }
PROS: Does what you wish across browsers, sets the width and will be
proportional to font if you use ems.
CONS: Must be adjusted depending on the length of the content
2) Use float, will auto adjust width:
h1 { color: white; background-color: black; float: left; }
PROS: Does what you wish with most browsers
CONS: Changes flow of document, will need to clear float after the H1.
May interfere with layout if there are other floated elements on page.
IE is notorious for being 'twitchy' with floats
3) Change display like a table cell...
h1 { color: white; background-color: black; display: table-cell; }
PROS: Does exactly what you wish, but leaves IE out of the picture
CONS: Even though this would be the perfect solution, with IE's market
share it will be a hard case to make (even though from a designer's
perspective "life would be great" if IE would just go away...)
--
Take care,
Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com