position

  • Revision slug: CSS/position
  • Revision title: position
  • Revision id: 25413
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment 7 words added, 3 words removed

Revision Content

{{ CSSRef() }}

Summary

The position CSS property chooses alternative rules for positioning elements, designed to be useful for scripted animation effects.

  • {{ Xref_cssinitial() }}: static
  • Applies to: all elements
  • {{ Xref_cssinherited() }}: no
  • Media: {{ Xref_cssvisual() }}
  • {{ Xref_csscomputed() }}: as specified

A positioned element is an element whose computed position property is relative, absolute, or fixed.

An absolutely positioned element is an element whose computed position property is absolute or fixed.

The {{ Cssxref("top") }}, {{ Cssxref("right") }}, {{ Cssxref("bottom") }}, and {{ Cssxref("left") }} properties specify the position of positioned elements.

Syntax

position:  static | relative | absolute | fixed | {{ cssxref("inherit") }}

Values

static
Normal behavior.  The top, right, bottom, and left properties do not apply.
relative
Lay out all elements as though the element were not positioned, and then adjust the element's position, without changing layout (and thus leaving a gap for the element where it would have been had it not been positioned).  The effect of position:relative on table-*-group, table-row, table-column, table-cell, and table-caption elements is undefined.
absolute
Do not leave space for the element.  Instead, position it at a specified position relative to its closest positioned ancestor or to the containing block. Absolutely positioned boxes can have margins, they do not collapse with any other margins.
fixed
Do not leave space for the element.  Instead, position it at a specified position relative to the screen's viewport and doesn't move when scrolled. When printing, position it at that fixed position on every page.

Examples

Relative positioning

To position an element relatively 20px from the top and left of its normal position, the following CSS is used.

#two { position: relative; top: 20px; left: 20px; }

 Note how the other elements are displayed as if "Two" were in it's normal position and taking up space.

Absolute positioning

Elements that are positioned relatively are still considered to be in the normal flow of elements in the document.  In contrast, an element that is positioned absolutely is taken out of the flow and thus takes up no space when placing other elements.  The absolutely positioned element is positioned relative to nearest positioned ancestor.  If a positioned ancestor doesn't exist, the initial container is used.

In the example below, the blue ancestor div is positioned relative (so it becomes the nearest positioned ancestor) and box Two is positioned absolutely:

#ancestor { position: relative; background: #ddf; width: 500px; }
#two { position: absolute; top: 20px; left: 20px; }

If #ancestor had not been positioned relative, box Two would have appeared relative to the upper left corner of the page.

Fixed positioning

Fixed positioning is similar to absolute positioning, with the exception that the element's containing block is the viewport.  This is often used to create a floating element that stays in the same position even after scrolling the page.  In the example below the "One" box is fixed 80px from the top of the page and 20px from the left:

#one { position: fixed; top: 80px; left: 20px }

When viewing the top of the page, the position box appears in the upper left, and after scrolling, it remains in the same place relative to the viewport:

fixed-1.png

fixed-2.png

 

 

 

 

  

Notes

For relatively positioned elements, the {{ Cssxref("top") }} or {{ Cssxref("bottom") }} property specifies the vertical offset from the normal position and the {{ Cssxref("left") }} or {{ Cssxref("right") }} property specifies the horizontal offset.

For absolutely positioned elements, the {{ Cssxref("top") }}, {{ Cssxref("right") }}, {{ Cssxref("bottom") }}, and {{ Cssxref("left") }} properties specify offsets from the edge of the element's containing block (what the element is positioned relative to). The margin of the element is then positioned inside these offsets.

Most of the time, absolutely positioned elements have auto values of {{ Cssxref("height") }} and {{ Cssxref("width") }} computed to fit the contents of the element. However, non-replaced absolutely positioned elements can be made to fill the available space by specifying (as other than auto) both {{ Cssxref("top") }} and {{ Cssxref("bottom") }} and leaving {{ Cssxref("height") }} unspecified (that is, auto). Likewise for {{ Cssxref("left") }}, {{ Cssxref("right") }}, and {{ Cssxref("width") }}.

Except for the case just described of absolutely positioned elements filling the available space:

  • If both {{ Cssxref("top") }} and {{ Cssxref("bottom") }} are specified (technically, not auto), {{ Cssxref("top") }} wins.
  • If both {{ Cssxref("left") }} and {{ Cssxref("right") }} are specified, {{ Cssxref("left") }} wins when {{ Cssxref("direction") }} is ltr (English, horizontal Japanese, etc.) and {{ Cssxref("right") }} wins when {{ Cssxref("direction") }} is rtl (Arabic, Hebrew, etc.).

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 1.0 (1.0) ({{ anch("Gecko notes", "See notes") }}) 4.0 4.0 1.0 (85)
fixed value 1.0 1.0 (1.0) 7.0 4.0 1.0 (85)
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} 1.0 (1.0) ({{ anch("Gecko notes", "See notes") }}) {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

Gecko notes

In Gecko 10.0 {{ geckoRelease("10.0") }}, handling of absolutely positioned elements inside tables was fixed. Previously, if you had a positioned {{ HTMLElement("table") }} element inside a positioned block element, such as a {{ HTMLElement("div") }}, a position: absolute; styled element inside the table would be positioned relative to the outer {{ HTMLElement("div") }} rather than the table, which is the nearest positioned ancestor. This was a bug, and has been fixed. See this blog post for a more verbose explanation of the details.

Specifications

See also

{{ Cssxref("display") }}, {{ Cssxref("float") }}, {{ Cssxref("top") }}, {{ Cssxref("right") }}, {{ Cssxref("bottom") }}, {{ Cssxref("left") }}

{{ languages( { "de": "de/CSS/position", "es": "es/CSS/position", "fr": "fr/CSS/position", "zh-cn": "cn/CSS/position" } ) }}

Revision Source

<p>{{ CSSRef() }}</p>
<h3>Summary</h3>
<p>The<code> position </code>CSS property chooses alternative rules for positioning elements, designed to be useful for scripted animation effects.</p>
<ul> <li>{{ Xref_cssinitial() }}:<code> static</code></li> <li>Applies to: all elements</li> <li>{{ Xref_cssinherited() }}: no</li> <li>Media: {{ Xref_cssvisual() }}</li> <li>{{ Xref_csscomputed() }}: as specified</li>
</ul>
<p>A <strong>positioned element</strong> is an element whose <a href="/en/CSS/computed_value" title="en/CSS/computed_value">computed</a> position property is <code>relative</code>, <code>absolute</code>, or <code>fixed</code>.</p>
<p>An <strong>absolutely positioned element</strong> is an element whose <a href="/en/CSS/computed_value" title="en/CSS/computed_value">computed</a> position property is <code>absolute</code> or <code>fixed</code>.</p>
<p>The {{ Cssxref("top") }}, {{ Cssxref("right") }}, {{ Cssxref("bottom") }}, and {{ Cssxref("left") }} properties specify the position of positioned elements.</p>
<h3>Syntax</h3>
<pre class="eval">position:  static | relative | absolute | fixed | {{ cssxref("inherit") }}</pre>
<h3>Values</h3>
<dl> <dt>static</dt> <dd>Normal behavior.  The<code> top</code>,<code> right</code>,<code> bottom</code>, and<code> left </code>properties do not apply.</dd> <dt>relative</dt> <dd>Lay out all elements as though the element were not positioned, and then adjust the element's position, without changing layout (and thus leaving a gap for the element where it would have been had it not been positioned).  The effect of<code> position:relative </code>on<code> table-*-group</code>,<code> table-row</code>,<code> table-column</code>,<code> table-cell</code>, and<code> table-caption </code>elements is undefined.</dd> <dt>absolute</dt> <dd>Do not leave space for the element.  Instead, position it at a specified position relative to its closest positioned ancestor or to the containing block. Absolutely positioned boxes can have margins, they do not collapse with any other margins.</dd> <dt>fixed</dt> <dd>Do not leave space for the element.  Instead, position it at a specified position relative to the screen's viewport and doesn't move when scrolled. When printing, position it at that fixed position on <em>every page</em>.</dd>
</dl>
<h3>Examples</h3>
<h4>Relative positioning</h4>
<p>To position an element relatively 20px from the top and left of its normal position, the following CSS is used.</p>
<pre>#two { position: relative; top: 20px; left: 20px; }
</pre>
<p> Note how the other elements are displayed as if "Two" were in it's normal position and taking up space.</p>
<p><img alt="" class="internal" src="/@api/deki/files/4922/=relative-positioning.png" style="width: 519px; height: 136px;"></p>
<h4>Absolute positioning</h4>
<p>Elements that are positioned relatively are still considered to be in the normal flow of elements in the document.  In contrast, an element that is positioned absolutely is taken out of the flow and thus takes up no space when placing other elements.  The absolutely positioned element is positioned relative to <em>nearest positioned ancestor</em>.  If a positioned ancestor doesn't exist, the initial container is used.</p>
<p>In the example below, the blue ancestor div is positioned relative (so it becomes the nearest positioned ancestor) and box Two is positioned absolutely:</p>
<pre>#ancestor { position: relative; background: #ddf; width: 500px; }
#two { position: absolute; top: 20px; left: 20px; }
</pre>
<p><img alt="" class="internal" src="/@api/deki/files/4923/=absolute-positioning.png" style="width: 518px; height: 134px;"></p>
<p>If #ancestor had not been positioned relative, box Two would have appeared relative to the upper left corner of the page.</p>
<h4>Fixed positioning</h4>
<p>Fixed positioning is similar to absolute positioning, with the exception that the element's containing block is the viewport.  This is often used to create a floating element that stays in the same position even after scrolling the page.  In the example below the "One" box is fixed 80px from the top of the page and 20px from the left:</p>
<pre>#one { position: fixed; top: 80px; left: 20px }
</pre>
<p>When viewing the top of the page, the position box appears in the upper left, and after scrolling, it remains in the same place relative to the viewport:</p>
<p><img align="left" alt="fixed-1.png" class="internal lwrap" src="/@api/deki/files/4924/=fixed-1.png?size=thumb" style="width: 160px; height: 125px;"></p>
<p><img align="left" alt="fixed-2.png" class="internal lwrap" src="/@api/deki/files/4925/=fixed-2.png?size=thumb" style="width: 160px; height: 100px;"></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>  </p>
<h3>Notes</h3>
<p>For relatively positioned elements, the {{ Cssxref("top") }} or {{ Cssxref("bottom") }} property specifies the vertical offset from the normal position and the {{ Cssxref("left") }} or {{ Cssxref("right") }} property specifies the horizontal offset.</p>
<p>For absolutely positioned elements, the {{ Cssxref("top") }}, {{ Cssxref("right") }}, {{ Cssxref("bottom") }}, and {{ Cssxref("left") }} properties specify offsets from the edge of the element's containing block (what the element is positioned relative to). The margin of the element is then positioned inside these offsets.</p>
<p>Most of the time, absolutely positioned elements have <code>auto</code> values of {{ Cssxref("height") }} and {{ Cssxref("width") }} computed to fit the contents of the element. However, non-replaced absolutely positioned elements can be made to fill the available space by specifying (as other than <code>auto</code>) both {{ Cssxref("top") }} and {{ Cssxref("bottom") }} and leaving {{ Cssxref("height") }} unspecified (that is, <code>auto</code>). Likewise for {{ Cssxref("left") }}, {{ Cssxref("right") }}, and {{ Cssxref("width") }}.</p>
<p>Except for the case just described of absolutely positioned elements filling the available space:</p>
<ul> <li>If both {{ Cssxref("top") }} and {{ Cssxref("bottom") }} are specified (technically, not <code>auto</code>), {{ Cssxref("top") }} wins.</li> <li>If both {{ Cssxref("left") }} and {{ Cssxref("right") }} are specified, {{ Cssxref("left") }} wins when {{ Cssxref("direction") }} is <code>ltr</code> (English, horizontal Japanese, etc.) and {{ Cssxref("right") }} wins when {{ Cssxref("direction") }} is <code>rtl</code> (Arabic, Hebrew, etc.).</li>
</ul>
<h3 name="Browser_Compatibility">Browser compatibility</h3>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari (WebKit)</th> </tr> <tr> <td>Basic support</td> <td>1.0</td> <td>1.0 (1.0) ({{ anch("Gecko notes", "See notes") }})</td> <td>4.0</td> <td>4.0</td> <td>1.0 (85)</td> </tr> <tr> <td><code>fixed </code>value</td> <td>1.0</td> <td>1.0 (1.0)</td> <td>7.0</td> <td>4.0</td> <td>1.0 (85)</td> </tr> </tbody> </table>
</div>
<div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Phone</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatUnknown() }}</td> <td>1.0 (1.0) ({{ anch("Gecko notes", "See notes") }})</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> </tbody> </table>
</div>
<h4>Gecko notes</h4>
<p>In Gecko 10.0 {{ geckoRelease("10.0") }}, handling of absolutely positioned elements inside tables was fixed. Previously, if you had a positioned {{ HTMLElement("table") }} element inside a positioned block element, such as a {{ HTMLElement("div") }}, a <code>position: absolute;</code> styled element inside the table would be positioned relative to the outer {{ HTMLElement("div") }} rather than the table, which is the nearest positioned ancestor. This was a bug, and has been fixed. See <a class="external" href="http://ehsanakhgari.org/blog/2011-10-04/upcoming-changes-absolute-positioning-tables-and-table-margin-collapsing-firefox-10" title="http://ehsanakhgari.org/blog/2011-10-04/upcoming-changes-absolute-positioning-tables-and-table-margin-collapsing-firefox-10">this blog post</a> for a more verbose explanation of the details.</p><h3>Specifications</h3>
<ul> <li><a class="external" href="http://www.w3.org/TR/CSS21/visuren.html#choose-position" title="http://www.w3.org/TR/CSS21/visuren.html#choose-position">CSS 2.1 Visual formatting #position</a></li>
</ul>
<h3>See also</h3>
<p>{{ Cssxref("display") }}, {{ Cssxref("float") }}, {{ Cssxref("top") }}, {{ Cssxref("right") }}, {{ Cssxref("bottom") }}, {{ Cssxref("left") }}</p>
<p>{{ languages( { "de": "de/CSS/position", "es": "es/CSS/position", "fr": "fr/CSS/position", "zh-cn": "cn/CSS/position" } ) }}</p>
Revert to this revision