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.

an absolute gotcha<br />

One more thing you should know about<br />

absolute positioning<br />

So far, when you’ve used the left, right, top, and bottom properties to specify<br />

the position, these numbers have always been <strong>with</strong> respect to the edge of the page,<br />

right? Well, we need to refine that just a bit.<br />

When you position an element, you’re specifying the position relative to the closest<br />

ancestor element that is also positioned. So, you’re probably saying, “What? I haven’t<br />

positioned anything except for the sidebar. How could there be an ancestor in my<br />

X<strong>HTML</strong> that is already positioned?” Believe it or not there is – the element,<br />

which the browser positions for you when it creates the page.<br />

But, let’s take this one step further. Say you wanted to absolutely position another<br />

inside the sidebar.<br />

<br />

532 Chapter 12<br />

Here’s a new nested<br />

inside the sidebar.<br />

<br />

<br />

<br />

<br />

... more X<strong>HTML</strong> here ...<br />

<br />

...<br />

<br />

If we absolutely position the “tv” , its closest<br />

positioned ancestor is the sidebar . And so,<br />

the positioning will be relative to the edges of the<br />

sidebar, not the page.<br />

#tv {<br />

position: absolute;<br />

top: 100px;<br />

left: 100px;<br />

width: 100px;<br />

}<br />

Another thing to know... if you get caught in a<br />

conversation about “closest ancestors that are<br />

positioned” at your next cocktail party, just say<br />

the “nearest containing block that is positioned.”<br />

That’s the terminology the experts use.<br />

div id=“header”<br />

div id=“main”<br />

div id=“footer”<br />

div<br />

id=“sidebar”<br />

100<br />

pixels<br />

The “tv” is<br />

positioned relative<br />

to the sidebar ,<br />

not to the page.<br />

100<br />

pixels<br />

div<br />

id=“tv”<br />

If you’re positioning <strong>with</strong> respect to the <br />

element, then the bottom property may not<br />

do what you’d expect. You’d think the “bottom”<br />

would be the very bottom of the Web page<br />

itself, but the element actually defines<br />

this as the bottom of the browser window. So,<br />

if you want to absolutely position an element<br />

from the bottom of the page, rather than the browser<br />

window, you need to place your element inside an element<br />

that extends to the bottom of your page, and is positioned.<br />

One way to do this is to put your element into a relatively<br />

positioned element at the bottom of the page. We’ll look at<br />

relative positioning later in the chapter.

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

Saved successfully!

Ooh no, something went wrong!