Positioning elements using CSS is more accurate than using graphical HTML objects or tables, and makes the display much faster.
By using CSS, you can position a HTML element in different ways: fixed, absolute or relative, in relation to other elements.
The positioning of an element is based on the parameters: top, right, bottom, left. Typically, top and left will be used for positioning elements, since the start point is the top-left corner.
- There are four different positioning methods (or values): static, relative, absolute and fixed; these are applied to position property.


static

static is the default value where an element is rendered in the normal flow and not uniquely positioned.
Static positioned elements are not affected by the top, bottom, left, and right properties.
- Syntax:
selector { position: static; }
- Example:
<style>
#p_left {
border: 2px solid blue;
position: relative;
left: 90px;
}
#p_top {
border: 2px solid red;
position: static;
top: -28px;
}
</style>

<p>Example position: static</p>
<h3 id='p_left'>Position relative, left: 90px.</h3>
<h3 id='p_top'>Position static, top: -28px.</h3>

relative

A relative positioned element is positioned relative to its normal position, does not take the element out of the normal flow, and it will create a new point of origin for any of its child elements that may be positioned absolutely.
The content of relatively positioned elements can be moved and overlap other elements, by using negative values for top, right, bottom, left.

- Syntax:
selector { position: relative; }
- Example:
<style>
#p_left {
border: 2px solid blue;
position: relative;
left: 90px;
}
#p_top {
border: 2px solid red;
position: relative;
top: -25px;
}
</style>

<p>Example position: relative</p>
<h3 id='p_left'>Position relative, left: 90px.</h3>
<h3 id='p_top'>Position relative, top: -25px.</h3>

absolute

An absolute position element is positioned relative to the first parent element that has a position other than static, or <html> if no such element exists, and its display is not affected by other elements.
Absolute positioning takes the element out of the normal document flow, and can overlap other elements.
  - Syntax:
selector { position: absolute; }
- Example:
<style>
#p_left {
border: 2px solid blue;
position: absolute;
top: 15px;
left: 80px;
}
#p_right {
border: 2px solid red;
position: absolute;
right: 25px;
}
</style>

<h3 id='p_left'>Absolute left: 80</h3>
<h3 id='p_right'>Absolute right: 25</h3>
• When an absolutely positioned element is included inside an element with relative position (called 'parent'), the absolutely positioned element uses the top-left corner origin of the parent.
- Example:
<style>
.relativ { border: 2px solid blue; position:relative; top:30px; left:50px; }
.absolut { position:absolute; top:15px; left:0px; }
</style>

<div class='relativ'>
Relative...
<div class='absolut'>Absolute, coursesweb.net </div>
...
 </div>
• When a relatively positioned element is placed inside an absolute positioned element, it will move with the element positioned absolute (its parent).
- Example:
<style>
.absolut { border: 2px solid blue; position:absolute; top:20px; left:25px; }
.relativ { position:relative; top:10px; left:5px; }
</style>

<div class='absolut'>
Absolute...
 <div class='relativ'>Relative, GamV.eu </div>
...
 </div>

fixed

Fixed positioning is based on the viewport window. The origin for fixed-positioned elements is the root node (<html>). The top-left position of 0, 0 would be the top-left corner of the viewport (screen), and similarly, 'bottom:0px; right:0px;' is the bottom-right corner.
Fixed positioned elements can overlap other elements and it will not move even if the window is scrolled.
  - Syntax:
selector { position: fixed; }
- Example (element with id='p_fix' is positioned fixed):
<style>
body { height:1400px; }
#p_fix {
background:#cecefe;
position: fixed;
top: 25px;
left: 30%;
width:300px;
height:85px;
}
</style>

<h4>Example position: fixed</h4>
<h3 id='p_fix'>If you scroll the page, this element remains fixed on screen.</h3>

z-index

The z-index property specifies the stack order of an element (which element should be placed behind, or, in front of others).
The index placement of the elements is made automatically, starting with 0 and continuing through the increment by one, in order of their appearance in the HTML document and relative to their parent.
An element with greater stack order is always in front of an element with a lower stack order.
- z-index can change the display order, but this property only works on positioned elements (position:absolute, position:relative, or position:fixed)
- Values - Syntax:
selector { z-index: value; }
- Example, the content of the first DIV is displayed over the second DIV. You can see the difference if you test this code and remove z-index: 99; from #dv1.
<style>
#dv1 {
position: absolute;
top: 15px;
left: 50px;
background-color: #fee0e0;
z-index: 99;
}
#dv2 {
position: absolute;
top: 9px
left: 5px;
background-color: #d0f0d0;
color: #0000dd;
}
</style>

<div id='dv1'>The First DIV</div>
<div id='dv2'>Content of the second DIV</div>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Positioning

Last accessed pages

  1. AJAX Course, free Lessons (7411)
  2. AJAX and JSON (1019)
  3. Wake Up! (3449)
  4. Read Excel file data in PHP - PhpExcelReader (63706)
  5. JavaScript Course - Free lessons (15710)

Popular pages this month

  1. Contact page - CoursesWeb (1955)
  2. Tabs effect with CSS (1942)
  3. Insert, Select and Update NULL value in MySQL (1036)
  4. Read Excel file data in PHP - PhpExcelReader (832)
  5. PHP getElementById and getElementsByTagName (513)