Thursday, February 4, 2010

Absolutely Positioning a Container Div

I've created a few simple CSS websites in the past, so I'm not a complete newbie to CSS (I even use external stylesheets, woo!). However, these websites tended to be hacked together based on trial and error without true understanding of the forces at work behind them. What I'll aim to do from time to time in this blog is explain and demonstrate some of these fundamental CSS concepts. This is mostly for solidifying my own understanding, but I also hope that it may be useful for someone else in my position.

And speaking of my position, today I'm writing about positioning! The position selector is something I've used in the past, and it's very useful for placing page elements exactly where you want them. Even if it takes a few tries before you get it there. Of course, this sort of number hacking can be avoided if you know the size of all your elements ahead of time. I usually forget.

To introduce my short example, I'll also need to introduce something called a <div>. <div> is an html tag used to separate - or divide - the document into logical sections. A simple website may only have a couple of <div>s. For example, there may be one called "header" which contains the page title and graphic, one called "mainContent" for the site's content (text, images, links), and one called "footer" for displaying copyright information.

Most of the time, web designers like to wrap ALL of the site's content inside a container <div>. That's because it allows them more control to position all elements easily. Think of it as kind of like Russian nesting dolls. If all the dolls are contained within the largest doll, you can move them all easily. If there was no container, you'd have to move each of them individually. In most cases, this <div> is called "wrapper" or "container". I like to use "wrapper" just because I can type it faster.

Here is what the wrapper div would look like in an html document.


<html>
<head><title>The Title of the Page</title>
</head>
<body>
<div id="wrapper">
<p>
Here is some content in a wrapper div.
</p>
</div>
</body>
</html>




So, I just used "id" and set the name of the div to "wrapper." Which is important because that's the name we're going to need to use in our stylesheet to tell this div what to do.

And here is what my stylesheet looks like:


body {
background: #000;
}
#wrapper {
background: #ddd;
}



All I did was set some background colors so we can see where the outside of our box is. And here is what we end up with.

As you can see, our body and our #wrapper are not aligned. We can see the black color of the body on both the left and right, as well as the top of #wrapper.

This was a problem for me because I wanted the header and navigation to be able to bleed off to the edges of the browser window. If I set body {margin: 0;}, that takes care of the right and left sides, but there's still a gap at the top. I still don't quite understand why that is. If anyone knows, please explain in the comments section. The way I've found around it for now is to use position on our #wrapper like so:


body {
background: #000;
}
#wrapper {
position: absolute;
top: 0;
left: 0;

background: #dddddd;
}



View.
Now our box is positioned 0 from the top and 0 from the left, leaving us without the gap at the top. In this case, setting the position to fixed would have the same effect. But using the position property had an unexpected side-effect. Instead of our #wrapper extending all the way to the right like it did before, now our box is shrink-wrapped to the width of its content. That's because when something is absolutely positioned, it is removed from the "flow" of the document. This means other elements (in this case body) behave as though it's not there. I fixed this by adding the width property to my CSS like this:


#wrapper {
position: absolute;
top: 0;
left: 0;
width: 100%;
background: #dddddd;
}



Now our box goes all the way to the edge of the browser window, no matter how it's resized. Great! Exactly what I wanted!

For now... :)

Starting Out

In the early days of the internet - a period I shall affectionately refer to as 'the mid-to-late 90s' - web pages, and good web pages in particular, were few and far between. Web design as a profession hadn't really developed, and most pages were written in plain HTML. Mine was no different.

Back then I had what I believe was a relatively popular web page on Geocities. The homepage displayed a cheerful pastel tiled rainbow background with a few supporting images and links. I had a page dedicated to Jess Harnell and Rob Paulsen, the voices of Wakko and Yakko from "Animaniacs," my favorite cartoon show. Another page was my "Rants" section, a kind of bloggy type page where I would occasionally write articles about things that irritated me.

My site's claim to fame, however, was the Ultimate Cartoon List, on which I endeavored to include the title of every cartoon ever made. The list was alphabetical with anchor links leading to each letter at the top of the page. I got quite a few emails from people who said it helped them figure out if such-and-such was a real cartoon, and so much support from people submitting additional shows for the list that I hadn't thought of. It was a great time on the web for me, and thanks to this experience, I realize that creating and maintaining a website was something I truly enjoyed.

As a 7th grader in middle school I feverishly wrote down my lengthy URL (which, thanks to Geocities naming conventions, probably included at least ten forward slashes and 23 numbers) on strips of paper and handed them out to everyone in school, including teachers. I was really quite good at marketing myself back then, in retrospect.

But it was a small internet back then. Nowadays I'd be lucky if someone even found my little site, and being emailed by an admirer would be as likely as being struck by lightning. But it's that memory that makes me want to try this web thing again. I already know HTML pretty well thanks to that experience in middle school (which continued into early high school). So now I'm learning CSS, JavaScript and Flash. I want to make fun and pretty web pages again, by today's updated standards.

So on with my Journey Into The Web!