04.06.2013 Views

Head First HTML with CSS

Head First HTML with CSS

Head First HTML with CSS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

playing <strong>with</strong> relative positioning<br />

The test drive<br />

After reloading the Starbuzz page, you should see the<br />

coffee bag over to the right part of the sidebar. What<br />

is interesting is that part of the image is actually<br />

extending beyond the sidebar into the margin and off<br />

the edge of the page. How does that work? Well, as<br />

you’ve seen, the browser first flows a relative element<br />

onto the page, and only then does it offset where it<br />

is displayed. So the element still takes up the same<br />

spot on the page, it’s just drawn in a different location.<br />

Relative is a little like static positioning, but <strong>with</strong> a<br />

dash of absolute thrown in. But, unlike absolute,<br />

relative positioning is specified just as an offset from<br />

the element’s real location, not in absolute coordinates<br />

from the nearest containing block.<br />

So, does this improve the page? We’re not sure, but<br />

it was fun. (You might want to remove the relative<br />

positioning before you show it to the CEO.)<br />

540 Chapter 12<br />

Couldn’t we have done this<br />

<strong>with</strong> padding or margins<br />

somehow?<br />

Not really.<br />

No matter how you tweaked the padding<br />

and margins you still can’t get an image<br />

to be positioned outside of the box it’s<br />

in. And why try to do it the hard way?<br />

We achieved a better effect <strong>with</strong> two lines<br />

of <strong>CSS</strong>. You can use relative positioning<br />

to display an element well beyond the<br />

element’s box in the flow, which you just<br />

can’t do <strong>with</strong> padding or margins.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!