Forum Moderators: not2easy
td.columnHead {
background-image: url(grafix/content/content_guide_top.gif);
height: 19px;
width: 229px;
}
Now I've defined h2 this way:
h2 {
font-family: Impact, Verdana, Arial, Helvetica, sans-serif;
font-size: 16px;
color: #6A283E;
font-weight: normal;
background-color: #EBEBE0;
padding-right: 8px;
}
Now here's the HTML:
<td class="columnHead"><h2>REQUIRED READING</h2></td>
My intention is to have the table cell show the text against a plain background (as specified in the h2 style). Then, starting 8 pixels to the right of where this text ends, I want to see the background image.
What happens is that the background image is completetly obscured on the h2 line. Then a new line in the <td> is created, and there I see my background image. This is in spite of the height of this <td>, which I've set at 19px.
<div class="reqreading"><h2>REQUIRED READING</h2></div>
.reqreading {
position: absolute;
top: ...;
left: ...;
}
Or put Div inside the TD and position relatively.
What happens is that the background image is completetly obscured on the h2 line.
<hx> tags are block level elements, which means they fill their containers horizontally. Therefor, the background on the <h2>, which stretches the full width of the table cell, is covering up the background image on the <td>.
To avoid this, you need to somehow shrink the <h2> so that it does not fill the full width of the cell. I can think of three options. The third one is best.
(a) Put the background image on the <h2> and position it to the left of the header text (difficult since it's impossible to reliably know the width of the text).
(b) Place a right margin on the <h2> to push it's right edge away from the right edge of the table cell (also difficult, since , again, you can't know how far to push it over to get 8px between header text and image.
(c) Leave the image as background for the cell, float the <h2> to the left and give it a right margin of 8px.
I recommend option (c). Here's the code...
td.columnhead h2{
float:left;
margin-right:8px;
}
It would be much better if you could give the <h2> a width, as well. Non-widthed, floated elements tend to trigger display bugs in IE, but in this case you really can't. If adding this causes some crazy things to happen on your page (like if mousing over links causes whole portions of the page to vanish), you may need to add a hack to bring IE into line.
Also note that anything which follows the float will have to be given a clear:left.
A final note: if you want the 8px between the header text and the image to have the header's background color, use padding-right instead of margin-right.
cEM
Anyhow, here's my solution:
========================
CSS
.columnHead {
font-family: Impact, Verdana, Arial, Helvetica, sans-serif;
font-size: 16px;
color: #6A283E;
font-weight: normal;
background-color: #EBEBE0;
padding-right: 8px;
padding: 0px;
}
td.columnHead {
background-image: url(grafix/content/content_guide_top.gif);
height: 19px;
width: 229px;
}
========================
HTML
<td class="columnHead"><span class="columnHead">REQUIRED READING<img src="../grafix/misc/spacer.gif" width="8"></span></td>
By applying "columnHead" to the <td>, I get the background image. By using <span> for REQUIRED READING, I get the background color to cover over the background image for the extent of the text. Then, my inelegant way of creating 8px of space at the end of the text before the background image appears, I stuck in a spacer.gif.
If anyone can suggest a more elegant approach, I'm all ears. Also, I'd be interested to hear if anyone else has had the same problems as me with the "h" styles.
If I understand the first post correctly, all you should need to do is switch the 'padding-right:8px;' in the <h2> to 'margin-right:8px;'.
Remember that, according to the w3c box model [google.ca], the order of features of a containing element is (from the inside out):
...and also that background images do not extend past borders - in other words, margins don't get background properties...
-B
[edit]
We posted at the same time :)
In general, I've always found that the "h" styles (h1, h2, etc) are pretty difficult to work with. They seem to include extra vertical space where a normal style would not. Maybe that's due to my own lack of understanding, but I can't understand why anybody would use these styles.
Forgive me for saying so, but it's probably 'cause you don't understand their properties fully. The 'hx' elements are styled by default with a big bottom margin. If the margin causes problems for you, just alter or delete it.
Try a test stylesheet for the page you're working on (or some other), and put this in the stylesheet:
* {
padding:0;
margin:0;
}
This sets all elements' padding and margins to zero. If you do this, you will have to explicitly declare padding and margin values on elements that need them (typically a set of something like hx, ol, ul, p, form, th, td, address etc...), but you will also find that you have a lot more control over the way the elements display.
[/edit]
They seem to include extra vertical space where a normal style would not.
Using a <span> tag will work, but a <span> tag is meaningless. It holds no information about what it contains and gives no indication of the purpose served by the text inside of it.
I can't understand why anybody would use these styles.
all you should need to do is switch the 'padding-right:8px;' in the <h2> to 'margin-right:8px;'.
cEM