Forum Moderators: not2easy

Message Too Old, No Replies

CSS Positioning - A simplified look

Floats and Positions..etc.

         

mipapage

1:24 pm on Jun 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Disclaimer: Okay, I've designed a few full CSS websites, but only until recently have I felt that my understanding of CSS Positioning has solidified for me. So, having read the New Charter [webmasterworld.com] post, I thought I'd throw in my try at a simplified look at CSS - Positioning... here we go!

Objective

This is intended as an overview of the types of positioning with CSS. It is very simple, and while it covers all of the methods of positioning with CSS, it doesn’t delve into the finer details of descendant blocks, and the different ways that the positioning of inline elements and block level elements in the descendant blocks are affected. These could be the subject matter for a second look at CSS - Positioning.

The Box Model

It is crucial that users of CSS understand the CSS Box Model [w3.org]. If you know not what it is, check that link and then get back here!

Understanding Normal Flow

Although fairly simple, understanding normal flow is critical to understanding the positioning options that exist with CSS. For our convenience, the normal flow of a document could be defined as the position where you would expect an element to be found if no CSS positioning was applied to that element.

The default value for the position attribute of an element (<div>, <span> etc.) in CSS is static, which in CSS terms means that the element's box (wrt CSS Box Model) is part of the normal flow of the document. Any element that is not ‘position:fixed’, ‘position:absolute’, will find itself a part of and positioned with respect to the normal flow.

As we will see below, you have to keep in mind the normal flow of the document when using either ‘float’ or ‘position:relative’. Only with ‘position: fixed’ or ‘position: absolute’ do you remove an element from the normal flow of the document and have the freedom to put something in it's place!

Float

Using the ‘float’ attribute allows users to position the box of an element horizontally either to the right - ‘float:right’ - or to the left - ‘float:left’.

The vertical position of the element is as it would be in the normal flow of the document - you could consider the place in the code where you insert your floated element as the vertical anchor of that element, which is then displaced in the horizontal direction that you set it to.

Any inline content (inline boxes) surrounding the floated box flows around the floated element - so the normal flow of the document is affected/displaced by the floated element..

So, floated elements affect other elements in the normal flow - that is, they will displace other inline boxes in the normal flow. As we will see below, this is different than with absolutely positioned elements, as absolutely positioned elements, which by definition are not part of normal flow, will not displace elements of the normal flow, but rather overlap them.

Relative Positioning

An element that has ‘position:relative’ is positioned relative to its own unpositioned location in the normal flow - that is, it will be moved the distance that you specify using either ‘top’ ‘right’ ‘bottom’ or ‘left’ from it’s unpositioned location in the normal flow.
The object still affects the normal flow, but not at its offset position: the area it would have occupied if it hadn't been positioned is what is affected.

As a simple example, imagine two divs, one right after the other in your code so that they appear in your document as one ‘box’ on top of the other:

<div>Some text here</div> 
<div>Some more text here</div>
<div>Even more text here</div>
<div>Yet even more text here</div>

With respect to ‘normal flow’, the text from these divs should appear as four lines vertically stacked one after the other in your document. But, if you set the second div to ‘position: relative; top: -20px;’, the second div will then shift twenty pixels towards the top of the page, relative to where it would have appeared in the normal flow, and a space would remain where it would have been had we not moved it 20 pixels upwards.

Any excellent example of the ‘power’ of ‘position: relative’ can be found here [webmasterworld.com].

Absolute Positioning

An element that has ‘position: absolute’ or ‘position: fixed’ is removed from the normal flow and does not affect normal flow.

Absolutely positioned elements are positioned according to their container block (the block level element that contains them). What is important to note here is that while the containing block of a fixed-position element is the viewport, the containing block of an absolute-positioned element is defined as its closest positioned ancestor - meaning the closest element, outside of itself, that has ‘position: absolute, relative, or fixed’. So, in the simplest case, if there are no other ‘positioned’ elements, the ‘closest positioned ancestor’ then becomes the viewport.

Elements that are set as ‘position: fixed’ are positioned relative to the viewport, and they do not scroll with the webpage.

Bugs

We all know that browsers are buggy, and there are certainly CSS-P bugs out there. While we gather them all up, here’s a good place to start learning about buggy behavior:

<snip>


I hope this helps! It certainly cleared a couple of things up for me just writing it... <edited by mipapage for accuracy>

[edited by: Nick_W at 3:19 pm (utc) on June 1, 2003]

[edited by: mipapage at 4:20 pm (utc) on June 1, 2003]

joshuakaufman

3:24 pm on Jun 1, 2003 (gmt 0)

10+ Year Member



Only with position: fixed or position: absolute do you remove an element from the normal flow of the document...

I'm don't think this is correct. The CSS2 spec [w3.org] specifically says that float elements are not in the normal flow of the document. This is important to know because it affects how CSS selectors will be applied to the float.

Other than that, this is a great introduction to CSS positioning. Nice work, mipapage!

mipapage

3:53 pm on Jun 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the css2 spec:
Since a float is not in the flow, non-positioned block boxes created before and after the float box flow vertically as if the float didn't exist. However, line boxes created next to the float are shortened to make room for the floated box. Any content in the current line before a floated box is reflowed in the first available line on the other side of the float.

The behavior had me fooled. Thanks for pointing that out!

So: Floated elements are not in the normal flow, but they affect line (inline) boxes that they are next to.


reading the specs after writing that, they make more sense to me!... and Thank you!

papabaer

8:22 pm on Jun 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Floats most certainly ARE in the page flow, though the float behavior dictates the "how and if." Understanding the fine points is crucial for effective use of floats.

Normal page flow? Let's call it "normal page flow with exceptions."

Page flow itself is a misleading term in that "flow" can occur in virtually any HTML container, whether the <body></body> element or within a child <div>, a positioned <div> or <blockquote>. There are lots of possibilities

mipapage

9:53 pm on Jun 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Page flow itself is a misleading term in that "flow" can occur in virtually any HTML container,

Would that then be child container flow? :-]

Those are the intricacies that I didn't want to get into with this one. I suppose that I could have, and that we still can, it's just the bugs you find here and there are intimidating (make that - a pain in the butt to explain) as can be all of the explaining that needs to be done as we don't have images here.

I think that little cut and paste code snippets might be a cool way to go about it - for example the 'position:relative' up there could'a been done as a snippet...

[edited by: mipapage at 10:49 pm (utc) on June 1, 2003]

joshuakaufman

10:31 pm on Jun 1, 2003 (gmt 0)

10+ Year Member



Understanding the fine points is crucial for effective use of floats.

And those fine points would be...?

papabaer

10:55 pm on Jun 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Margins, padding, stacked floats, container floats, element floats, opposing floats, clear properties, floats contained in other floats, empty floats (sandbag divs), inline (horizontal) multiple floats and conditional floats.

Even with IE's limitations, there remains quite a list of practical float applications. Be creative. Look to the possibilities.

John_Caius

2:33 pm on Jun 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've mastered absolute positioning relative to the top left corner of the screen, but I'd be interested to know whether you can do absolute positioning relative to the top left corner of a div. I've tried and failed in the past - didn't know whether it was because of my poor code or because it's impossible.

joshuakaufman

3:48 pm on Jun 2, 2003 (gmt 0)

10+ Year Member



John_Caius,

Here's an example of absolute positioning relative to the top left corner of a div:

div#container {
width: 100%;
height: 100px;
border: 1px solid red;
}

p#p1 {
position: relative;
top: 50px;
left: 50px;
}

<p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>

<div id="container">
<p id="p1">Positioned element</p>
</div>

madcat

5:46 pm on Jun 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well the container div needs to be set to absolute, relative or fixed. Then any subsequent elements can be positioned within that div, for starters...

madcat

9:28 pm on Jun 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Papabaer, man I know all of those circumstances in Message #7 fail to appear in all my CSS books ;)

mongoloid001

10:09 pm on Jun 2, 2003 (gmt 0)

10+ Year Member



Where can you get a good css book anyway? I bought Eric Meyer's "Mastering the language of web design" for $50 cdn, and it's all junks in it. (Mind you, cdn is not so cheap as it used to be nowadays).

I really don't want to spend any more money on useless books. Nobody does...

madcat

10:48 pm on Jun 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cascading Style Sheets: Separating Content from Presentation is excellent (Owen Briggs). Usually Eric Meyer's is well written material, esp. on CSS...not sure about the one you mentioned.