Tutorial: Grasping CSS layouts
#1
Posted 28 January 2008 - 10:43 AM
Hello class, my names Jeeves and I like pie, tea, and long walks down the beach. I also like CSS, especially when used right. Now, hands up who actually understands CSS Box Model, and positioning? Unless you said no or hesistated, I don't believe you, and getting your head around these little details of technicality is the hardest part of CSS (besides specifity, but thats just math).
Box Model
Box Model is the structure of every element, it accounts for their logistics and layers, and packages it all together in a box. Why some people have trouble with this is probably because its not all inside the box, box model also allows for an area outside the traditional content area, but first, we'll worry about what is in the box.
At the center of your box, you have your content. Text, image, whatever, if its there to be seen its likely its your content. This is the top-most layer of your box. Behind this, you have a background-image (if applicable), and a background-color (likewise). Thats it, three layers, all of which self-explanatory, all of which in the order you'd imagine. This is where it gets hard.
Between the edges of the box and the content, you have padding. If you have a width set on your box, this is the padding, plus the content. Or rather, content size = height/width - padding. Looking at another example, find some used paper. The paper itself would be the background-color (and image?), and on top you have the writing(/images). The gap between the edge of the paper and the writing in the middle would be your padding. Easy.
Padding should be used if you want a gap between your content and the sides of the box, or a gap for the background to show through. If the padding is set to 0, the size of the box will be equal to the size of the content.
Outside the box, you have two more components; margin and border. Border is simply a border around something. It appears around the edge of your box, expanding outwards. So a 200px box with a 2px border (1px each side) would appear to be 202px, because the border sits outside this.
Last, theres the margin. This is perhaps the hardest to understand, as you cannot see it. Margin is a gap outside of the border and box, that doesn't appear, but affects the logistics and positioning of other elements. So you could, for example, use margin to keep a gap between elements. You can also use it to position elements. For example, unless otherwise specified, a block element will occupy 100% width, this means you can fluidly size the element using its margins; its size will be 100% - margins. You can also set the horisontal margins of an element to "auto" to center it. A very easy technique for creating a multi-column layout is by setting a margin on one or both sides of your central content that are big enough to fit the side contents, and position them absolutely in this gap. You can also use negative margins, which is often great for positioning.
Positioning
Positioning in CSS is the last powerful tool you need to understand in order to get layouts to work. Once you know box model you can make sure your elements are as big as you want, with the right spaces in the right places, but unless you want a single column of all elements, you may want to position some.
By default, all elements will be displayed according to their order in the source (position:static), one after the other.
To move an element outside of this sourced flow, there are three other settings you can use for position. Firstly, relative.
Relative is, as you may have guessed, relative to where it would normally be. For example, { position: relative; top: -1em; } would place the element 1em higher than it would normally be. The downside of relative positioning, is it leaves a gap where the element would normally be. In this case, there would be 1em below; its like moving an icon on your desktop; the rest won't move unless you tell them too as well.
Absolute positioning, however, removes an element from the documents source flow entirely, and positions it absolutely, relative to the first positioned parent. If its uncontaned, this will be the root element, <html>, but you can position something absolutely by placing it within a container, and setting a position to it (relative, with no offset works well). If an elements positioned absolutely, it will be exactly where you tell it to be. For example, { position: absolute; top: 2em; left: 93px; } would place the element 2em below, and 93px to the right of its parent/root. Because its taken out of the source flow, everything else ignores it. This makes it easy to have issues with overlapping elements, as they will ignore the box model of the absolute, but it does allow you place things exactly.
Last of all, Fixed is absolute positioning, but relative to the browsers viewport. Thus if you scrolled a page with a fixed element, the fixed will remain where it was positioned on screen, even after everything else has moved up or down.
Fin
Now, I didn't say this would be a good explanation (for that see "Relatively-Absolute" @ autisticcuckoo.net, and the specs box model & the visual formatting model), but I hope some of it makes sense, and people will stop using tabular layouts.
World Domination Status: ▾2.7%
#2
Posted 28 January 2008 - 04:39 PM
Very nice article, by the way. I'm looking forward to Chapter 2: floats
My Political Compass
Sieben Elefanten hatte Herr Dschin
Und da war dann noch der achte.
Sieben waren wild und der achte war zahm
Und der achte war's, der sie bewachte.
#3
Posted 29 January 2008 - 12:06 PM
Thanks for adding the pic, was going to find one but had to dash at the time
World Domination Status: ▾2.7%
#4
Posted 13 March 2008 - 02:48 PM
you mean body?If its uncontaned, this will be the root element, <html>
on the topic of the box model, do you agree that the size of a box should actually specify content + padding, or even better, both are possible?
for example, if i have a box which is 100 wide and with 10 pad, i need to make it 80. that's not so difficult, but then when i want a larger pad, i generally want to keep the total width. however, the way it's set up now, i need to edit 2 things, sometimes with a calculator in hand.
now, there could be a way to make both possible. either:
{ padding-style: outside; padding: 10px; }
or
{ padding-outside: 10px; }
Einstein: "We can’t solve problems by using the same kind of thinking we used when we created them."
#5
Posted 13 March 2008 - 04:28 PM
Yes!on the topic of the box model, do you agree that the size of a box should actually specify content + padding, or even better, both are possible?
But actually the W3C is already working on some kind of declaration for it, it's called box-sizing. I got some info from http://www.quirksmode.org/css/box.html.
My Political Compass
Sieben Elefanten hatte Herr Dschin
Und da war dann noch der achte.
Sieben waren wild und der achte war zahm
Und der achte war's, der sie bewachte.
#6
Posted 14 March 2008 - 02:44 AM
As that QuirksMode.org page shows, IE5-7/Win don't support it.
No, he means what he said. In IE5.x/Win (and IE6+ in quirks mode) <body> is the root element, but in other browsers it's <html>.you mean body?If its uncontaned, this will be the root element, <html>
#7
Posted 14 March 2008 - 06:23 AM
The root elements the first after the doctype, and the one that should have the xml namespace. Dispite the fact most your visable page will be in body, body is still a child of html.
World Domination Status: ▾2.7%
#9
Posted 16 March 2008 - 06:22 PM
You mean that it will overhang the parent's bottom edge? That's just the way they were designed. They weren't originally designed to be used so heavily for layout.Can anyone tell me why, if I put float: left; on an element, the element gets floated outside of its parent? How do I get it to float left but still stay within its parent?
To answer your question, I suggest you read up on clearing and containing floats. Other than floating the parent of the floated element(s) too, which often isn't a good option, the PIE/Aslett approach is best.
#10
Posted 17 March 2008 - 01:30 PM
World Domination Status: ▾2.7%
#11
Posted 17 March 2008 - 02:15 PM
naturally, but by uncontained i understood something like a <p> in <body>, so it would be positioned relatively to body. after all (even though some browsers do their best to fix it), any markup outside of <body> shouldn't be positioned at all2pg, welcome to the whacky world of win.
The root elements the first after the doctype, and the one that should have the xml namespace. Dispite the fact most your visable page will be in body, body is still a child of html.
Einstein: "We can’t solve problems by using the same kind of thinking we used when we created them."
#12
Posted 19 March 2008 - 11:09 AM
World Domination Status: ▾2.7%
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users