Jump to content


Photo

Tutorial: Grasping CSS layouts


  • Please log in to reply
11 replies to this topic

#1 Jeeves

Jeeves

    I write the interwebz

  • Members
  • 4,156 posts
  •  Friendly neighborhood standards Nazi

Posted 28 January 2008 - 10:43 AM

Grasping CSS Layouts
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.

Posted Image

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 Phil

Phil

    Force Majeure

  • Network Leaders
  • 7,976 posts
  • Location:Switzerland
  • Projects:Revora, C&C:Online
  •  Thought Police
  • Division:Revora
  • Job:Network Leader
  • Donated
  • Association

Posted 28 January 2008 - 04:39 PM

I guess especially when talking about the box model, an image is a must. Here's a pretty good one (IMO):
Posted Image

Very nice article, by the way. I'm looking forward to Chapter 2: floats :)

revorapresident.jpg
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 Jeeves

Jeeves

    I write the interwebz

  • Members
  • 4,156 posts
  •  Friendly neighborhood standards Nazi

Posted 29 January 2008 - 12:06 PM

I was going to include them but didn't have a spare week :sad2:
Thanks for adding the pic, was going to find one but had to dash at the time :thumbsupsmiley:

World Domination Status: 2.7%


#4 Bart

Bart

  • Network Admins
  • 8,524 posts
  • Location:The Netherlands
  • Division:Revora
  • Job:Network Leader

Posted 13 March 2008 - 02:48 PM

If its uncontaned, this will be the root element, <html>

you mean body?

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;
}

bartvh | Join me, make your signature small!
Einstein: "We can’t solve problems by using the same kind of thinking we used when we created them."

#5 Phil

Phil

    Force Majeure

  • Network Leaders
  • 7,976 posts
  • Location:Switzerland
  • Projects:Revora, C&C:Online
  •  Thought Police
  • Division:Revora
  • Job:Network Leader
  • Donated
  • Association

Posted 13 March 2008 - 04:28 PM

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?

Yes!

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.

revorapresident.jpg
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 Kravvitz

Kravvitz

    [X]HTML, CSS, & JS/DOM Adept

  • Members
  • 443 posts
  • Location:USA

Posted 14 March 2008 - 02:44 AM

IE8 will support -ms-box-sizing in its Standards mode.

As that QuirksMode.org page shows, IE5-7/Win don't support it.

If its uncontaned, this will be the root element, <html>

you mean body?

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>.

#7 Jeeves

Jeeves

    I write the interwebz

  • Members
  • 4,156 posts
  •  Friendly neighborhood standards Nazi

Posted 14 March 2008 - 06:23 AM

2pg, 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.

World Domination Status: 2.7%


#8 CodeCat

CodeCat

    Half fox, half cat, and all insanity!

  • Members
  • 3,768 posts
  •  Fighting for equality of all species

Posted 16 March 2008 - 12:39 PM

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?
CodeCat

Posted Image
Posted Image

#9 Kravvitz

Kravvitz

    [X]HTML, CSS, & JS/DOM Adept

  • Members
  • 443 posts
  • Location:USA

Posted 16 March 2008 - 06:22 PM

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?

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.

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 Jeeves

Jeeves

    I write the interwebz

  • Members
  • 4,156 posts
  •  Friendly neighborhood standards Nazi

Posted 17 March 2008 - 01:30 PM

Make sure the parent is positioned, too. Positioning is based off context, and if you position something relative to something else, you have to make sure the something else is positioned too, or it will look for the next thing up the hiarchy

World Domination Status: 2.7%


#11 Bart

Bart

  • Network Admins
  • 8,524 posts
  • Location:The Netherlands
  • Division:Revora
  • Job:Network Leader

Posted 17 March 2008 - 02:15 PM

2pg, 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.

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 all
bartvh | Join me, make your signature small!
Einstein: "We can’t solve problems by using the same kind of thinking we used when we created them."

#12 Jeeves

Jeeves

    I write the interwebz

  • Members
  • 4,156 posts
  •  Friendly neighborhood standards Nazi

Posted 19 March 2008 - 11:09 AM

Well, by default they should be pretty much the same, with no markup between the two, but html is the root, and you can do some funky things applying CSS to both elements. At the end of the day, its not really an issue if you treat body as if it was the root, but play around with it ;)

World Domination Status: 2.7%





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users