Tuesday 10 September 2013

Detailed Positioning - Part2

Position Property

Occasionally you need more control over the position of an element, more than a float can provide, in which case theposition property comes into play. The position property accepts five different values, each of which provide different ways to uniquely position an element.

Position Static

Elements by default have the position value of static, meaning they don’t have, nor will they accept, any specific box offset properties. Furthermore, elements will be positioned as intended, with their default behaviors.
In the demonstration below, all the boxes are stacked one on top of the other as they are block level elements and are not floated in any specific direction.
HTML
  1. <div class="box-set">
  2. <div class="box box-1">Box 1</div>
  3. <div class="box box-2">Box 2</div>
  4. <div class="box box-3">Box 3</div>
  5. <div class="box box-4">Box 4</div>
  6. </div>
CSS
  1. .box-set {
  2. background: #e8eae9;
  3. }
  4. .box {
  5. background: #8ec63f;
  6. height: 80px;
  7. width: 80px;
  8. }

Position Static Demo

BOX 1
BOX 2
BOX 3
BOX 4

Position Relative

The relative value for the position property is very similar to that of the static value. The primary difference is that the relative value accepts the box offset properties toprightbottom, and left. These box offset properties allow the element to be precisely positioned, shifting the element from its default position in any direction.

How Box Offset Properties Work

The box offset properties, toprightbottom, and left, specify how elements may be positioned, and in which direction. These offset properties only work on elements with a relativeabsolute, or fixedpositioning value.
For relatively positioned elements, these properties specify how an element should be moved from its default position. For example, using a top value of 20px on a relatively positioned element will push the element 20 pixels down from where it was originally placed. Switching the top value to -20px will instead pull the element 20 pixels up from where it was originally placed.
For elements using absolute or fixed positioning these properties specify the distance between the element and the edges of its parent element. For example, using a top value of 20px on an absolutely positioned element will push the element 20 pixels down from the top of its relatively positioned parent. Switching the top value to-20px will then pull the element 20 pixels up from the top of its relatively positioned parent.
While the relative position does accept box offset properties the element still remains in the normal, or static, flow of the page. In this case other elements will not impede on where the relatively positioned element was originally placed. Additionally, the relatively positioned element may overlap, or underlap, other elements without moving them from their default position.
In the demonstration below you will notice that the elements are still stacked on top of one another, however they are shifted from their default positions according to their individual box offset property values. These values cause the boxes to overlap one another, yet do not push each other in different directions. When an element is positioned relatively the surrounding elements will observe the relatively positioned elements default position.
HTML
  1. <div class="box-set">
  2. <div class="box box-1">Box 1</div>
  3. <div class="box box-2">Box 2</div>
  4. <div class="box box-3">Box 3</div>
  5. <div class="box box-4">Box 4</div>
  6. </div>
CSS
  1. .box-set {
  2. background: #e8eae9;
  3. }
  4. .box {
  5. background: #8ec63f;
  6. height: 80px;
  7. position: relative;
  8. width: 80px;
  9. }
  10. .box-1 {
  11. top: 20px;
  12. }
  13. .box-2 {
  14. left: 40px;
  15. }
  16. .box-3 {
  17. bottom: -10px;
  18. right: 20px;
  19. }

Position Relative Demo

BOX 1
BOX 2
BOX 3
BOX 4
In the event that the top and bottom box offset properties are both declared on a relatively positioned element, thetop properties will take priority. Additionally, if both the left and right box offset properties are declared on a relatively positioned element, priority is given in the direction in which the language of the page is written. For example, English pages the left offset property is given priority, and for Arabic pages the right offset property is given priority.

Position Absolute

Absolutely positioned elements accept box offset properties, however they are removed from the normal flow of the document. Upon removing the element from the normal flow, elements are positioned directly in relation to their containing parent whom is relatively or absolutely positioned. Should a relatively or absolutely positioned parent not be present, the absolutely positioned element will be positioned in relation to the body of the page.
Using absolutely positioned elements and specifying both vertical and horizontal offset properties will move the element with those property values in relation to its relatively positioned parent. For example, an element with a top value of50px and a right value of 100px will position the element 50 pixels down from the top of its relatively positioned parent and 100 pixels in from the right of its relatively positioned parent.
Furthermore, using an absolutely positioned element and not specifying any box offset property will position the element in the top left of its closest relatively positioned parent. Setting one box offset property, such as top, will absolutely position the element vertically but will leave the horizontal positioning to the default value of flush left.
In the demonstration below you can see how each box is absolutely positioned in relation to the parent division, of which is relatively positioned. Each individual box is moved in from a specific side with a positive value, or pulled out from a specific side with a negative value.
HTML
  1. <div class="box-set">
  2. <div class="box box-1">Box 1</div>
  3. <div class="box box-2">Box 2</div>
  4. <div class="box box-3">Box 3</div>
  5. <div class="box box-4">Box 4</div>
  6. </div>
CSS
  1. .box-set {
  2. background: #e8eae9;
  3. height: 200px;
  4. position: relative;
  5. }
  6. .box {
  7. background: #8ec63f;
  8. height: 80px;
  9. position: absolute;
  10. width: 80px;
  11. }
  12. .box-1 {
  13. top: 6%;
  14. left: 2%;
  15. }
  16. .box-2 {
  17. top: 0;
  18. right: -40px;
  19. }
  20. .box-3 {
  21. bottom: -10px;
  22. right: 20px;
  23. }
  24. .box-4 {
  25. bottom: 0;
  26. }

Position Absolute Demo

BOX 1
BOX 2
BOX 3
BOX 4
When an element has a fixed height and width and is absolutely positioned, the top property takes priority should both the top and bottom offset properties be declared. As with the relatively positioned elements, should an element with a fixed width have both the left and right box offset properties, priority is given to the direction of which the language of the page is written.
If an element doesn’t have a specific height or width and is absolutely positioned, using a combination of the topand bottom box offset properties displays an element with a height spanning the entire specified size. Same goes for using both the left and right box offset properties, resulting in an element with a full width based on both of theleft and right box offset properties. Using all four box offset properties will display an element with a full specified height and width.

Position Fixed

Using the positioning value of fixed works just like that of absolute, however the positioning is relative to the browser viewport, and it does not scroll with the page. That said, elements will always be present no matter where a user stands on a page. The only caveat with fixed positioning is that it doesn’t work with Internet Explorer 6. Should you want to force fixed positioning within Internet Explorer 6 there are suitable hacks.
Using multiple box offset properties with fixed positioning will produce the same behaviors as absolutely positioned element.
Keeping the same box offset properties from the previous demonstration, watch how the boxes are positioned in relation to the browser’s viewport and not the containing, relatively positioned parent. Clicking the link below will toggle between absolute and fixed positioning.
HTML
  1. <div class="box-set">
  2. <div class="box box-1">Box 1</div>
  3. <div class="box box-2">Box 2</div>
  4. <div class="box box-3">Box 3</div>
  5. <div class="box box-4">Box 4</div>
  6. </div>
CSS
  1. .box {
  2. background: #8ec63f;
  3. height: 80px;
  4. position: fixed;
  5. width: 80px;
  6. }
  7. .box-1 {
  8. top: 6%;
  9. left: 2%;
  10. }
  11. .box-2 {
  12. top: 0;
  13. right: -40px;
  14. }
  15. .box-3 {
  16. bottom: -10px;
  17. right: 20px;
  18. }
  19. .box-4 {
  20. bottom: 0;
  21. }

Position Fixed Demo

Turn on fixed positioning.
BOX 1
BOX 2
BOX 3
BOX 4

Fixed Header or Footer

One of the most common uses of fixed positioning is to build a fixed header, or footer, anchored to one side of a page. As a user scrolls the element stays prevalent, always within the viewport for users to interact with.
The code and demonstration below outline how this may be achieved. Notice how both left and right box offset properties are declared. This allows the footer to span the entire width of the bottom of the page, and it does so without disrupting the box model, allowing margins, borders, and padding to be applied freely.
HTML
  1. <footer>Fixed Footer</footer>
CSS
  1. footer {
  2. bottom: 0;
  3. left: 0;
  4. position: fixed;
  5. right: 0;
  6. }

Z-Index Property

By nature web pages are often considered to be two dimensional, displaying elements upon a x and y axis. However when you begin to position elements they are occasionally placed on top of one another. To change the order of how these elements are stacked, also known as the z-axis, the z-index property is to be used.
Generally, elements are positioned upon the z-axis as they appear within the DOM. Elements coming at the top of the DOM are positioned behind elements coming after them. Changing this stacking using the z-index property is pretty straight forward. The element with the highest z-index value will appear on the top regardless of its placement in the DOM.
In order to apply the z-index property to an element, you must first apply a position value of relative,absolute, or fixed. Same as if you were to apply and box off set properties.
In the example below, without the z-index property each box will be positioned precisely, starting with box two sitting on top of box one, then box three sitting on top of box two, and so forth. Reordering the stacking with the z-indexproperty now positions box two on top of every other box, followed by box three underneath it, and box four underneath box three.
Click the link below to toggle the z-index properties. Notice how the elements change their stacking order.
HTML
  1. <div class="box-set">
  2. <div class="box box-1">Box 1</div>
  3. <div class="box box-2">Box 2</div>
  4. <div class="box box-3">Box 3</div>
  5. <div class="box box-4">Box 4</div>
  6. </div>
CSS
  1. .box-set {
  2. background: #e8eae9;
  3. height: 160px;
  4. position: relative;
  5. }
  6. .box {
  7. background: #8ec63f;
  8. border: 3px solid #f7941d;
  9. position: absolute;
  10. }
  11. .box-1 {
  12. left: 10px;
  13. top: 10px;
  14. }
  15. .box-2 {
  16. bottom: 10px;
  17. left: 70px;
  18. z-index: 3;
  19. }
  20. .box-3 {
  21. left: 130px;
  22. top: 10px;
  23. z-index: 2;
  24. }
  25. .box-4 {
  26. bottom: 10px;
  27. left: 190px;
  28. z-index: 1;
  29. }

Z-Index Demo

Turn on the z-index.
BOX 1
BOX 2
BOX 3
BOX 4