Centering a Body Background Image that Behaves
So we had ourselves a little bit of a CSS problem here on Script & Style. I wanted to have two “halfs” of the side. The left half with the gray background and the right half with the red background. Easy enough usually, I handle that kind of thing with a background image (sort of like “faux columns”).
On Script & Style though, I wanted these colors to extended to the browsers edge, not be cut off by another wrapper or anything else. Again, easy enough, I’ll just make a background image for the body that is ~2000px wide and center it on the body. That’s exactly what I did: see background image.
So what’s the problem?
The problem is that when the browser window shrinks, the body element shrinks as well. And then because that image is centered on the body, we have an “intrusion” problem:

How we fixed it
We knew about the problem all along, and honestly, I didn’t think there was a good solution for it. I had done a tutorial about half and half backgrounds in the past, but they made use of absolute positioning which would almost certainly cause some z-index problems with our centered site.
The most intuitive solution would be applying a min-width to the body element. After all, this would prevent the body from shrinking any smaller than you set it to and prevent any intruding. I definitely tried that, but it didn’t seem to work.
The nice folks over at Markup Service (one of our PSD to HTML conversion sponsors) noticed the problem too and they emailed me a solution:
html { background:#000; }
body { min-width:958px; }
Huh? How’s that supposed to work? I have tried the min-width thing on the body before to no avail. How is adding a background color to the html element going to change anything. Well, I’m here to tell you folks, it does matter. I don’t know why, I’m not sure I care why at this point, it does the trick!.
With these things now added to our main CSS file, the centered body background image does it’s job. It stays centered, but when the browser window goes to small, it stays in place and doesn’t intrude over to the left.
Wow cool hack, didn’t know that one.