04.06.2013 Views

Head First HTML with CSS

Head First HTML with CSS

Head First HTML with CSS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using absolute positioning<br />

We’re now going to create a two column Starbuzz page using techniques similar to<br />

those we used <strong>with</strong> the float version of the page; however, this time we’ll use absolute<br />

positioning. Here’s what we’re going to do:<br />

1 <strong>First</strong> we’re going to make the sidebar absolutely<br />

positioned. In fact, we’re going to position it in exactly the same<br />

place that we floated it to before.<br />

2<br />

3<br />

Next, we’re going to give the main content another big margin so<br />

that the sidebar can sit on top of the margin space.<br />

Finally, we’re going to give this a good testing and see how it<br />

compares to the float version.<br />

Changing the Starbuzz <strong>CSS</strong><br />

Our X<strong>HTML</strong> is all ready to go, and the sidebar is right<br />

where we want it (below the important main content). All we need<br />

to do is make a few <strong>CSS</strong> changes and we’ll have a sidebar that is<br />

absolutely positioned. Open your “starbuzz.css” file and let’s make<br />

a few changes to the sidebar:<br />

#sidebar {<br />

}<br />

background: #efe5d0 url(images/background.gif) bottom right;<br />

font-size: 105%;<br />

padding: 15px;<br />

margin: 0px 10px 10px 10px;<br />

position: absolute;<br />

top: 128px;<br />

right: 0px;<br />

width: 280px;<br />

layout and positioning<br />

Okay, now we’re going to specify that the sidebar is absolutely<br />

positioned 128 pixels from the top, and 0 pixels from the right<br />

of the page. We also want the sidebar to have a width, so let’s<br />

make it the same as the float version: 280 pixels.<br />

You’ll see where the “128”<br />

came from in a sec...<br />

Remember, we are going back<br />

to the original versions of the<br />

files, which you can find in the<br />

“chapter12/absolute” folder.<br />

You can work out of the “absolute”<br />

folder, or copy the files “index.html”<br />

and “starbuzz.css” into the “starbuzz”<br />

folder and work from there, like we did.<br />

0 pixels from the right<br />

will make sure that the<br />

sidebar sticks to the right<br />

side of the browser.<br />

you are here 523

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

Saved successfully!

Ooh no, something went wrong!