The Wayback Machine - https://web.archive.org/web/20080512040142/http://www.webreference.com/html/tutorial18/1.html


spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / html / tutorials / 18 / 1

index1234

Tutorial 18: CSS Positioning, Part I

Biz Resources
Technology Asset Management Software
Information Technology Services
ecommerce solutions
Developer News
MicrosoftÂ’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads For The JON 2.0

A positioning primer

When a document is formatted using Cascading Style Sheets, it contains a number of boxes, as we have seen previously. Normally, these boxes are laid out one after the other, observing the rules we discussed in Tutorials 9 and 10 (or, as is the case with most browsers on the market today, not observing any rules at all). However, with CSS, you also have the option of taking boxes out of the normal flow and using different ways of positioning them.

One way to do this is to use floating boxes. We will discuss these in a future tutorial. Another way is to use absolute positioning.

Absolute Positioning

In its simplest use, absolute positioning means that an element's box is removed from the normal flow and placed somewhere in the page, as specified by a handful of properties. An absolutely positioned box says goodbye to all the other boxes and just goes and sits somewhere on its own, not bothering them at all. For example, look at the following style directives:

DIV#abs {
 position: absolute;
 top: 1em;
 left: 20%;
 width: 60%;
}

With the ruleset above, the position property for the DIV element with ID abs is set to absolute. Its default value is static, which means that the element's box is part of the normal flow. When set to absolute, in the simplest case, it means that the element is placed in an arbitrary position somewhere in the viewport. This position is set by the properties top, bottom, left and right, which tell the user agent to put the box at a certain distance from one of the viewport's edges.

In our example, the box is placed 1em down from the top edge of the viewport, and 20% (of the width of the viewport) to the right of the left edge. The width property is also used to set the width of the box in our example.

An absolutely positioned box, 1em from the top, 20% from the left, and 60% wide
An absolutely positioned box, 1em from the top, 20% from the left, and 60% wide

There is a catch, however; if this box was a child of another box that was not positioned statically, then instead of being positioned relative to the viewport, it will be positioned relative to that parent's box. Confused? You should be. Here's a nifty little diagram to make it all make sense.

#one {
 position: absolute;
 top: 1em;
 left: 1em;
}
#two {
 position: absolute;
 top: 3em;
 left: 2em;
}
#three {
 position: absolute;
 top: 0.5em;
 left: 0.5em;
}
<P ID="one">This is box one.</P>
<DIV ID="two">
 <P>This is box two.</P>
 <P ID="three">This is box three, which is also inside box two.</P>
</DIV>

Let's assume that everything else in this document is statically positioned. Then, the three boxes would be laid out as shown in the following image.

Box one is absolutely positioned relative to the viewport. Box two is also absolutely positioned relative to the viewport. Box three, however, is a child of box two, so it is absolutely positioned relative to box two.
Box one is absolutely positioned relative to the viewport. Box two is also absolutely positioned relative to the viewport. Box three, however, is a child of box two, so it is absolutely positioned relative to box two.

index1234

http://www.internet.com/



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Copyright 2008 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Perl Pragma Primer · Implement Drag and Drop in Your Web Apps: Part 2 · How to Create an Ajax Autocomplete Text Field: Part 5
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
SQL Server 2005 Express Edition - Part 22 - Upgrading from Microsoft SQL Server Desktop Engine (MSDE) · Vyatta: Downgrades that Pay Off · NetMotion Brings Cross-Network Support to Wireless VoIP

Legal Notices.

URL: http://www.webreference.com/html/tutorial18/1.html

Produced by Stephanos Piperoglou
Created: February 15, 2000
Revised: February 16, 2000

Morty Proxy This is a proxified and sanitized view of the page, visit original site.