I have a simple image rotation at the top of the main page of my website and I want to put a shadow around it without making the site wider. My website is 960px but the image that would have the shadow around the rotation is 982px…
I dont want horizontal bars to appear at the bottom of my page because of the 982px image.
As an example of what I’m trying to do, if you take a look at the Macbook Pro page on apple.com the edges of the outer macbook pros are outside of the width of the page but if you make the browser small enough horizontally you see the edges dont affect the width of the page itself… the horizontal bars appear when you make your browser window smaller than the site container.
So they’ve pulled the image container left and right (wider that its own container) with those negative margins. Quite clever, really.
Mind you, another approach could be to have a larger container and create the appearance that it was narrower by use of background images—although this might limit your options a bit.
A margin—for example, a left margin of 20px—will push an element 20px to the right. If you put a minus sign in front—that is, -20px— then it pulls the element the other way, 20px to the left, outside of its container. You can do this in both directions, allowing the element to be wider than its container. Weird, but interesting too!
You don’t actually need the negative margins as the page will not show the scrollbar until 960px is reached. However without the negative margin the black border in my example would sit at the viewports edge and not go beyond it.
As it is now it allows the page to shrink and eventually the borders go beyond the viewport but leaves all content visible without a scrollbar.