CSS positioning tutorial: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
mNo edit summary
Line 1: Line 1:
{{stub}}
{{stub}}
{{Web technology tutorial|Intermediate}}
{{Web technology tutorial|Intermediate}}
<pageby nominor="false" comments="false"/>
{{incomplete}}
{{Web technology tutorial|Beginners}}
<pageby nominor="false" comments="false"/>
<pageby nominor="false" comments="false"/>



Revision as of 15:27, 16 November 2011

Draft

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

Learning goals
  • Understand CSS positioning features
  • Be able to create simple CSS-based layouts
Prerequisite
Concurrent
Moving on
Level and target population
  • Beginners
Teaching materials
Remarks
  • This tutorial is intended for students in a field that is technology intensive (e.g. computer applications or educational technology). For people who need less, there exist many easy CSS tutorials on the web. This text is intended for students who also must learn principles and who are willing to learn CSS by doing a project, looking at CSS code and online reference manuals.
  • Ideally, a teacher also should assign a text formatting task, during or before assigning this tutorial for reading).
  • There are some inline examples. However, they had to be created in a away that makes them fit into a wiki page. I.e. we use inline style (something we usually don't advocate) and we also had to add extra styling properties.

Introduction

Positioning and layout 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))
  • See also CSS float tutorial (create simple layouts with float:left)

Basic positioning

Positioning other than using floats is done with two sets properties:

  1. Four positioning properties: left, right, top and bottom.
  2. The position property.

Each HTML element can be positioned with the left, right, top and bottom properties. Each of these properties defines a distance between this element and either the next element or the parent's border depending on the the position property settings.

Example of positioning 1cm to the right and 1cm down:

 top: 1cm; 
 left: 1cm;

Example of positioning 1cm the left and 1cm up:

 right: 1cm;
 bottom: 1cm;

In order to tell HTML how to position an element with respect to what, we need another property: position. The position property defines wether an element is positioned with respect to the parent element (absolute positioning) or with respect to its "normal" position (relative positioning).

Let's illustrate this with a few CSS examples:

  • Set the left edge of the element with respect to the right edge of the containing element:
div.picture {
     left:1cm;
     position:absolute;
     }
  • Move the left edge (and the whole element) X units to the left with respect to its normal position.
div.picture2 {
     left:1cm;
     position:relative;
     }
  • Do not position, i.e. ignore the left (that's the default)
div.picture3 {
    left:1cm;
    position:static;
     }

Absolute positioning will remove an element from the normal text flow, i.e. it will be inserted wherever you tell it to go. In other words, it can overlap with other elements.

Have a look at this example:

We are not done yet. The position:relative CSS declaration will have another effect. It will define a local coordinate system, i.e. all child element's absolute positionings will refer to this box and not the page as a whole.

Let's now look at two very simples examples that pull together these elements:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  <title>CSS background demo - First principles of instruction</title>
  <style type="text/css" media="all">
#container {
	     top:2cm;
             }
#left_col  {
             border:solid thin; position:absolute; 
             top:0.5cm; left:0pt;  width:49%;
             }
#right_col {
             border:solid thin; position:absolute; 
             top:0.5cm; right:0pt; width:49%;
             }
  </style>
</head>

<body>
<div id="container">
  <div id="left_col">
  Contents of left column ....
  Contents of left column ....
  </div>

  <div id="right_col">
  Contents of right column ....
  </div>
</div>

</body>
</html>

Life example:

This example may not do what we want. Each of the two left_col and right_col boxes are positioned with repsect to the body element. In order to position them with respect to the container element, we must use the following kind of code:

The "container" element includes a position:relative; declaration. This is needed in order to create a relative positioning system for the child elements.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  <title>CSS background demo - First principles of instruction</title>
  <style type="text/css" media="all">
    /* <![CDATA[ */

#container {
             top:2cm;
             position:relative;
             border: dotted thin;
             color: green;
             }
#top_right { 
             border:solid thin; position:absolute;
             right: 0;
             }
#left_col  {
             border:solid thin; position:absolute; 
             top:0.5cm; left:0pt;  width:49%;
             color: black;
             }
#right_col {
             border:solid thin; position:absolute; 
             top:0.5cm; right:0pt; width:49%;
             color: black;
             }

    /* ]]> */
  </style>
</head>

<body>
<div id="top_right">HOME</div>

<div id="container">
  container content that should be removed ...
  <div id="left_col">
  Contents of left column ....
  </div>

  <div id="right_col">
  Contents of right column ....
  </div>
</div>
</body>
</html>

Life example:

Some discussion:

  • As you can see, the left and right column boxes will move over the contents of the container box, i.e. absolute positioning removes a box from the normal "flow".
  • Also these two columns now position with respect to the container box, because the CSS of the container box includes the position:relative declaration. (We can't understand why the CSS folks didn't invite another keyword for that feature).
  • Finally, we added a little box on top right that is positioned with respect to the html page itself. As you can see, there is not default margin to its top and right.

Other examples / To do

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

Example layouts that you could look at:

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

Links

CSS Positioning tutorials