CSS positioning tutorial: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
Line 11: Line 11:
== Simple fluid layouts using the float property ==
== Simple fluid layouts using the float property ==


Using simple the float:left property/value combo you can create simple column layouts. We shall discuss an example that shows how to create a page with a heading box on top, a main body with three columns and a footing box.
Maybe you don't need positioning ! Using simply the '''float:left''' CSS property/value combo, you can create simple fluid column layouts. We shall discuss an example that shows how to create a page with a heading box on top, a main body with three columns and a footing box.


Originally, the float property wasn't mean for creating layouts. It was meant to create boxes around which other text flows, e.g. something like the "web technology box" on top right of this page. However, since there wasn't any proper layout design language, designers now use the float property (and often also the position property) for creating layouts.
Originally, the float property wasn't mean for creating layouts. It was meant to create boxes around which other text can flow, e.g. something like the "web technology box" on top right of this page. However, since CSS doesn't include any proper layout design language (like in [http://www.w3.org/TR/xsl11/ XSL]), some designers now use the float property for creating column layouts.


Creating a simple fluid three column layout is quite simple:
Creating a simple fluid three column layout is quite simple:
* Use a hierarchical structure tat defines all the big boxes: top, main (with left, center and right) and bottom.
* Use a hierarchical structure that defines all the big boxes: (1) top, (2) main and (3) bottom. The "main" box includes (2a) left, (2b) center and (2c) right.
* The left, center, right columns should use percentages and can't have any margins or padding.
* Define the ''width'' of these (2a) left, (2b) center, (2c) right columns with '''percentages''' and '''do not use''' any margins borders, or padding. If you do need margins etc., insert yet another div inside these divs.
* Use ''div id="xxx"'' for these boxes, and ''div class="yyy"'' for content boxes that go inside (this way you can fill the columns with several boxes).
* Use ''div id="xxx"'' for all major layout boxes, and ''div class="yyy"'' for content boxes that go inside (this way you can fill the columns with several boxes).
* After using a ''float:left'', insert ''clear:both'' in the next div.
* After using a ''float:left'', insert ''clear:both'' in the next div if you don't want the next div to float around. E.g. we use one for the bottom div.


Example:
Example:

Revision as of 13:53, 14 November 2011

Draft

<pageby nominor="false" comments="false"/>

Introduction

Positioning an layouting with CSS is one of the most difficult chapters

  • The whole model is very complex with respect to how various properties interact
  • You will have to fight browser implementation issues (e.g. IE bugs, though IE 9 seems to do quite well now - Daniel K. Schneider 23:04, 13 November 2011 (CET))

Simple fluid layouts using the float property

Maybe you don't need positioning ! Using simply the float:left CSS property/value combo, you can create simple fluid column layouts. We shall discuss an example that shows how to create a page with a heading box on top, a main body with three columns and a footing box.

Originally, the float property wasn't mean for creating layouts. It was meant to create boxes around which other text can flow, e.g. something like the "web technology box" on top right of this page. However, since CSS doesn't include any proper layout design language (like in XSL), some designers now use the float property for creating column layouts.

Creating a simple fluid three column layout is quite simple:

  • Use a hierarchical structure that defines all the big boxes: (1) top, (2) main and (3) bottom. The "main" box includes (2a) left, (2b) center and (2c) right.
  • Define the width of these (2a) left, (2b) center, (2c) right columns with percentages and do not use any margins borders, or padding. If you do need margins etc., insert yet another div inside these divs.
  • Use div id="xxx" for all major layout boxes, and div class="yyy" for content boxes that go inside (this way you can fill the columns with several boxes).
  • After using a float:left, insert clear:both in the next div if you don't want the next div to float around. E.g. we use one for the bottom div.

Example:

This page has the following layout:

Div layout structure

The CSS is included in the example HTML page. Look at its source. Its most important elements are:

#top {
      border: 1px solid;
 }

#main {
    float: left;
    margin-top: 1cm;
    margin-bottom: 1cm;
    width: 100%;
    border: 1px dotted;
    }

#left_col {
    float: left;
    width:17%;
    min-width: 112px;
    /* When using borders, you have to adjust percentages
       You can uncomment these just for debuggin boxes
       */
    /* border: 1px dotted; */
}

.left {
    border: 1px solid;
    margin-bottom: 2px;
}

#text_col {
    float:left;
    width: 66%;
    min-width: 4cm;
}

.text { 
    background-image: url(tree_silhouette.png);
    background-repeat: no-repeat;
    background-position: center center;
    background-size: contain;
    margin-left:1%;
    margin-right:1%;
    border: 1px solid;
    padding: 1em;
 }
 
#right_col {
    float: left;
    width:17%;
    min-width:2cm;
}

.right {
    border: 1px solid;
    border: 1px solid;
    margin-bottom: 2px;
    }

#bottom_padding {
    clear: both;
    border: 1px transparent;
}

#bottom {
    margin-top: 1cm;
    border: 1px solid;
}

Template:

To do

I probably will translate, repair and expand this old example (in french): http://tecfa.unige.ch/guides/css/ex/boxing0.html

In the meantime, please have a look at other tutorials, some of which are listed in the links section.

Links

CSS Positioning tutorials

- Daniel K. Schneider 19:22, 8 September 2009 (UTC)