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.

Another example of absolute positioning<br />

Let’s look at another example. Say we have another <br />

<strong>with</strong> the id “annoyingad”. We could position it like this:<br />

#annoyingad {<br />

position: absolute;<br />

top: 150px;<br />

left: 100px;<br />

width: 400px;<br />

}<br />

The annoying ad is being positioned 100<br />

pixels from the left, and 150 pixels<br />

from the top. It’s also a bit wider than<br />

the sidebar, at 400 pixels.<br />

Just like <strong>with</strong> the sidebar, we’ve placed the annoying<br />

ad at a precise position on the page. Any<br />

elements underneath that are in the normal flow<br />

of the page don’t have a clue about the absolutely<br />

positioned elements layered overhead. This is a little<br />

different from floating an element, because elements<br />

that were in the flow adjusted their inline content<br />

to respect the boundaries of the floated element.<br />

But absolutely positioned elements have no effect<br />

whatsoever on the other elements.<br />

Who’s on top?<br />

Another interesting thing about absolutely<br />

positioned elements is that you can layer them<br />

on top of each other. But if you’ve got a few<br />

absolutely positioned elements at the same<br />

position in a page, how do you know the<br />

layering? In other words, who’s on top?<br />

Each positioned element has a property called a<br />

z-index that specifies its placement. You’ll see<br />

how to specify a z-index in just a few pages.<br />

The header, main, and<br />

footer s are all<br />

in the flow, and flat<br />

on the page.<br />

div id=“header”<br />

div id=“sidebar”<br />

div id=”main”<br />

div id=“annoyingad”<br />

div id=“header”<br />

div id=“footer”<br />

div id=“annoyingad”<br />

div id=”main”<br />

layout and positioning<br />

Now we have a second , positioned<br />

absolutely, about 100 pixels from the<br />

left and 150 pixels from the top.<br />

div id=“sidebar”<br />

div id=“footer”<br />

Notice the annoyingad is<br />

on top of the sidebar .<br />

The sidebar and annoyingad s<br />

are layered on the page, <strong>with</strong> the<br />

annoyingad having a greater z-index<br />

than the sidebar, so it’s on top.<br />

you are here 521

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

Saved successfully!

Ooh no, something went wrong!