WD-positioning-19970819 Positioning HTML Elements with Cascading Style Sheets W3C Working Draft 19-Aug-1997 Latest version: /TR/WD-positioning This version: http://www.w3.org/TR/WD-positioning-19970819 Previous version: http://www.w3.org/TR/WD-positioning-970131 Editor: Robert Stevahn, Hewlett Packard Co. Authors: Scott Furman, et.al., Netscape Communications Corp. Scott Isaacs, et.al., Microsoft Corp. ------------------------------------------------------------------------ Status of this document This document is an intermediate draft produced by the W3C CSS Working Group as part of the Stylesheets Activity; it is stable enough to be released for public comment (to www-style@w3.org) but may change before approval as (part of) a recommendation. Hence it should not be implemented as part of a production system, but may be implemented on an experimental basis. This is a W3C Working Draft for review by W3C members and other interested parties. It is a draft document and may be updated, replaced or obsoleted by other documents at any time. It may be updated, replaced or obsoleted by other documents at any time. If citing W3C Working Drafts, please refer to them as "works in progress." A list of current W3C technical reports can be found at http://www.w3.org/TR. ------------------------------------------------------------------------ Abstract The HyperText Markup Language (HTML) is a simple markup language used to create hypertext documents that are portable from one platform to another. HTML documents are SGML documents with generic semantics that are appropriate for representing information from a wide range of applications. CSS (Cascading Style Sheets) is a style sheets language that can be applied to HTML to control the style of a document: which fonts and colors to use, how much white space to insert, etc. The following specification extends CSS to support the positioning and visibility of HTML elements in three-dimensional space. Familiarity with both CSS1 and HTML 3.2 are assumed. ------------------------------------------------------------------------ Contents Abstract Contents 1 Introduction 2 Positioning Elements by example 2.1 'Absolute' Positioning 2.2 'Relative' Positioning 2.3 Placing 'absolute' positioned elements relative to the flow 2.3.1 Implementing change bars 2.4 'Static' Positioning 2.5 Floating elements 2.6 Visibility 2.7 Z-order 3 CSS Positioning Properties 3.1 'position' 3.1.2 The element 3.2 'left', 'top' 3.3 'width', 'height' 3.4 'clip' 3.5 'overflow' 3.6 'z-index' 3.7 'visibility' 4 Glossary 5 Questions and Answers ------------------------------------------------------------------------ 1 Introduction Designers want to explicitly control the position of HTML elements to produce rich, static HTML documents. They also want powerful layout control to enable dynamic, animated HTML-based content. This document describes extensions to CSS that allow authors to exercise greater accuracy in page description and layout for both purposes. This document introduces two methods of positioning HTML elements using the new 'position' property: 1) 'relative' positioning allows elements to be offset relative to their natural position in the document's flow, and 2) 'absolute' positioning allows elements to be removed from the document's flow and positioned arbitrarily. The examples below demonstrate these features. Dynamic aspects of managing positioned elements, such as hiding, displaying and movement can only be performed using an external scripting language. The W3C Document Object Model Working Group (DOM-WG) is working on the dynamic aspects of HTML. ------------------------------------------------------------------------ 2 Positioning Elements by example Elements can be positioned through use of the new 'position:' property. Let's examine each of this new property's possible values. Most of the examples below utilize the following HTML snippet:
Beginning of body contents. Start of outer contents. Inner contents. End of outer contents. End of body contents.
Each example will declare different CSS styles for outer and inner. 2.1 'Absolute' Positioning Elements with 'absolute' positioning are formatted as rectangular overlays, outside the normal flow of the document, into which their contents flow. An 'absolute' positioned element is laid out independently of both its parent and child elements, without regard for their dimensions or position. For that matter, the layout of each 'absolute' positioned element is independent of any others. The contents of an element with a higher z-order may obscure the contents of an element with a lower z-order. Text in one positioned element cannot be flowed around an image in another positioned element. By default, a positioned element will be placed just above (in z-space) its parent. Example 1 Consider the following CSS declarations for outer and inner: #outer {position:absolute; top: 200px; left: 200px; width: 200px; color: red;} #inner {color: blue;} This results in something like the following: (0,0) +----------document window----------+(400,0) |Beginning of body contents. End of | |body contents. | | | | | | | | | | +-----------------+| | |Start of outer || | |contents. Inner || | |contents. End of || | |outer contents. || | +-----------------+| | | | | +-----------------------------------+(400,400) (0, 400) 'Absolute' positioned elements have the following characteristics: * They define a new rectangular context into which their contents are flowed, just as the HTML inside the element flows into the document window. * They are positioned within the current positioning context by means of the 'top' and 'left' properties. * Their height and width may be specified using the 'height' and 'width' properties. If the width is not specified, the element will extend to immediately inside the right inner edge of the parent element. The height, if not specified, will be just large enough for the contents of the element. Like floating elements, absolutely positioned elements are considered to be block-level in nature. * They define a box positioning context for child elements. Note: This draft does not specify the behavior of dynamic elements in scripting environments. For example, what happens when an element having 'width: auto' is repositioned? Do the contents reflow, or do they maintain their original formatting? The answer is outside the scope of this draft, and such behavior is likely to differ in initial implementations of CSS Positioning. The DOM-WG is addressing issues such as this. 2.2 'Relative' Positioning Elements with 'relative' positioning flow into place just like any other HTML, and can be positioned relative to their natural position within the document flow. When these elements are positioned, they keep their naturally rendered shape, including line breaks, and the space originally reserved for them remains. Any following elements do not reflow. Dynamic movement of 'relative' elements can provide animation effects in scripting environments. Relative positioning could also be used as a general form of super- and subscripting, although the line height will not be automatically adjusted to take the positioning into consideration. The following example, admittedly contrived, demonstrates relative positioning without utilizing a script: Example 2 BODY {line-height: 200%} #outer {position: relative; top: -12px; color: red;} #inner {position: relative; top: 12px; color: blue;} This results in the outer text being superscripted, while the inner text is subscripted relative to the outer text (but even with the normal body text). In the illustration below, the numbers to the left of the illustration indicate the normal position of the double-spaced lines. +----------document window----------+ | Start | 1|Beginning of body contents. | |of outer contents. | 2| Inner contents. | |End of outer contents. | 3| End of body | | | 4|contents. | | | 5| | | | 6| | | | 7| | | | | | +-----------------------------------+ 'Relative' positioned elements have the following characteristics: * Their contents flow naturally into position, just like "normal" HTML elements. * They can be positioned relative to their natural position in the document. This is most useful in scripting environments, where animation effects can be created through relative positioning. * They define a Cartesian positioning context for absolutely positioned child elements, with the origin located in the position where the element is initially rendered. 2.3 'Absolute' positioning relative to the flow Example 1 demonstrated absolute positioning in a box positioning context. That is, it demonstrated what happens when absolutely positioned elements are nested. The following example demonstrates an absolutely positioned element that is a child of a relatively positioned element. Instead of being positioned inside of a box, the element is positioned on a two-dimensional plane having its origin at the "logical beginning" of the relatively positioned element. In this example, the outer span is never intended to be explicitly positioned. Instead, relative positioning is used only to provide a local context for positioning a child element. Example 3 #outer {position: relative; color: red;} #inner {position: absolute; top: 200px; left: -100px; height: 130px; width: 130px; color: blue;} This results in something like the following: (0,0) +----------document window----------+(0,400) |Beginning of body contents. Start | |of outer contents. End of outer | |contents. End of body contents. | | | | | | | | +----------+ | | |Inner | | | |contents. | | | | | | | | | | | +----------+ | | | | | +-----------------------------------+(400,400) (400,0) Note that 'inner' is positioned relative to 'outer', rather than being positioned within the default positioning context. 2.3.1 Implementing change bars with relative and absolute positioning Using 'auto' for the value of the 'top' property, an 'absolute' positioned element may be used to display change bars, as demonstrated by the example below. 'Auto' results in the element being placed at the "current" location in the document window, just as if the element were being flowed into that space. Example 4I used two red hyphens to serve as a change bar. They will "float" to the left of the line containing THIS -- word.
This might result in something like: +----------document window----------+ | I used two red hyphens to serve | | as a change bar. They will | | "float" to the left of the line | |-- containing THIS word. | | | | | | | | | +-----------------------------------+ 2.4 Static Positioning 'Static' is the default value for 'position'. Static elements cannot be positioned or repositioned, nor do they define a positioning context for child elements. Example 5 Compare the following to Example 3. The only difference in the CSS is that in this case the outer element is static, rather than relative. #outer {/* position: static; */ color: red;} #inner {position: absolute; top: 200px; left: -100px; height: 130px; width: 130px; color: blue;} Since the static element does not establish a new positioning context, the inner element is positioned within the default positioning context: (0,0) +----------document window----------+(0,400) |Beginning of body contents. Start | |of outer contents. End of outer | |contents. End of body contents. | | | | | | | |------+ | |r | | |ents. | | | | | | | | |------+ | | | | | +-----------------------------------+(400,400) (400,0) 2.5 Floating elements Floating elements can already be created using CSS1, as demonstrated by the following example. We anticipate future extensions to the 'float:' property to allow more flexibility in flowing text around elements. Example 6 #outer {color: red;} #inner {float: right; width: 130px; color: blue;} (0,0) +----------document window----------+(0,400) |Beginning of body contents. Start | |of outer contents. End+-----------+| |of outer contents. |Inner || |End of body contents. |contents. || | +-----------+| +-----------------------------------+ 2.6 Visibility The 'visibility' property determines whether or not an element is initially displayed. In scripting environments, this property can be dynamically modified to hide or show an element. Unlike 'display: none', elements that are not visible still take up space--they are simply rendered transparently. Example: Example 7Choose a suspect:
Name: Al Capone
Residence: Chicago
Name: Lucky Luciano
Residence: New York
Choose a suspect:
Name: Al Capone
Residence: Chicago
Name: Lucky Luciano
Residence: New York