Forum Moderators: not2easy

Message Too Old, No Replies

background image not repeating full width of div

         

Purple Martin

10:48 pm on Mar 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My header's backgound image only goes part way across in Mozilla/Netscape/Opera, but it goes all the way across in IE6. So that means I've done something wrong because Mozilla/Netscape/Opera get it right, right?

I must have missed something easy - it is repeating part of the way therefore there is a repeat happening in all browsers, but for some reason it doesn't repeat all the way to the right-hand side. There's even some content in the div going past the right of where the repeating stops.

#header {
width: 100%;
background-image: url(images/top.gif);
background-repeat: repeat-x;
}

rogerdp

11:35 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



Do you have a page where this is happening?

Purple Martin

12:03 am on Mar 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Corporate intranet - sorry but you can't see it.

rogerdp

5:49 am on Mar 11, 2004 (gmt 0)

10+ Year Member



So create a simple mock page that recreates the problem. That's usually much better anyway as it narrows down the core issue.

Purple Martin

2:28 am on Mar 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's the mock page. Just paste it in a blank file and edit "background-image: url(images/top.gif);" to point to a graphic of yours.

You will see that in IE the background image goes all the way to the right, in Mozilla/Netscape/Oper it stops part way. So IE is fooling me!

<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test page</title>
<style type="text/css">
body {
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
}
img {
border: none;
}
#headerWrap {
position: absolute;
top: 0px;
left: 0px;
height: 117px;
padding-left: 180px;
}
#header {
width: 100%;
height: 100%;
background-image: url(images/top.gif);
background-repeat: repeat-x;
}
#heading {
position: relative;
left: 70px;
}
</style>
</head>
<body>
<div id="headerWrap">
<div id="header">
<div id="heading"><img src="images/heading.gif" alt="My Logo"></div>
<p>Other header content here.</p>
</div>
</div>
</body>
</html>

rogerdp

5:39 am on Mar 12, 2004 (gmt 0)

10+ Year Member



#headerWrap needs width: 100%

Why are you using position: relative on #heading? Why not { padding: 0 0 0 70px; }? It's my experience that less use of relative and fixed positioning is easier to maintain.

I only tested in Opera.

Purple Martin

12:51 am on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



#headerWrap needs width: 100%

If I add width: 100%; to #headerWrap then I get a horizontal scrollbar because the page is now 180 pixels wider than the browser. This happens in Mozilla and IE and Opera.

Why are you using position: relative on #heading? Why not { padding: 0 0 0 70px; }? It's my experience that less use of relative and fixed positioning is easier to maintain.

If I remove the position: relative; from #heading and use padding: 0 0 0 70px; instead (or padding: 0px 0px 0px 70px;) the heading image doesn't get indented by 70 pixels, it is left-aligned instead. This happens in Mozilla and IE and Opera.

Thank you for trying but those ideas didn't work. I'd still appreciate it if anyone wants to have another look.

tedster

1:33 am on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the issue is using position:absolute for the #headerWrap div. When I remove that rule I get the expected tiling of the background image.

I don't think you need absolute positioning here, at least from what I see on your test page. The div would automatically begin at 0,0 in the natural flow. And when position:absolute is included, the div no longer extends for the full width of the screen but stops at the width where its content is contained.

Purple Martin

2:10 am on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you tedster, that fixed it.

---------------------------------

I understand why "when position:absolute is included, the div no longer extends for the full width of the screen but stops at the width where its content is contained".

But I don't understand why specifying 100% width while it is absolutely positioned causes a horizontal scrollbar. As far as I can work out, a div with the following CSS should be 100% wide and 117px high, be at the top of the window, and should have an internal gap of 180px to the left of the content.

#headerWrap {
position: absolute;
width: 100%;
top: 0px;
left: 0px;
height: 117px;
padding-left: 180px;
}

It gets the gap, but also extends past the right-hand edge of the window by 180px. I always thought that adding padding didn't affect the width - have I misunderstood something basic?

tedster

2:49 am on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I know is that browsers are not consistent in whether padding adds to the declared width of the div. If what you want is a gap, and you're not using a visible border, then using margin instead of padding will give you more consistent results.

I am not sure which browsers are correct, but in the practical world where we need results, it matters not. I have always assumed that padding "should" be added to the width of the element.

Perhaps one of our serious CSS scholars can clear this up.

rogerdp

8:37 am on Mar 16, 2004 (gmt 0)

10+ Year Member



[w3.org...]
"The box width is given by the sum of the left and right margins, border, and padding, and the content width. The height is given by the sum of the top and bottom margins, border, and padding, and the content height."

Anyone that hasn't read enough of the spec to quickly find these kind of answers shouldn't be using CSS for any more than dabbling.

grahamstewart

9:09 am on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I know is that browsers are not consistent in whether padding adds to the declared width of the div.

Width is the area for the content, so 100px image (with no padding etc) should fit in a div with width:100px regardless of what the padding, border or margin on the div is set to.

All modern browsers (including IE6) get this part of the box model correct provided you use a full doctype declaration in the HTML.

Which means your HTML should always start with something like this..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Older browsers (such as IE5.5) will require some corrective surgery - so I usually browser sniff and send them an additional CSS file.