Tuesday, 16 July 2013

Example of Absolute and Relative Position Property in CSS





Positioning:-
The CSS positioning properties allow you to position an element. It can also place an element behind another, and specify what should happen when an element's content is too big.
Elements can be positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the positioning method.
There are four different positioning methods.

Relative Positioning

A relative positioned element is positioned relative to its normal position.
h2.pos_left
{
position:relative;
left:-20px;
}
h2.pos_right
{
position:relative;
left:20px;
}

The content of a relatively positioned elements can be moved and overlap other elements, but the reserved space for the element is still preserved in the normal flow.
Relatively positioned element are often used as container blocks for absolutely positioned elements.

Absolute positioning

An element which is positioned absolute does not obtain any space in the document. This means that it does not leave an empty space after being positioned.
To position an element absolutely, the position property is set as absolute. You can subsequently use the properties left, right, top, and bottom to place the box.
As an example of absolute positioning, we choose to place 4 boxes in each corner of the document:

 
#box1 {
        position:absolute;
        top: 50px;
        left: 50px;
}
 
#box2 {
        position:absolute;
        top: 50px;
        right: 50px;
}
 
#box3 {
        position:absolute;
        bottom: 50px;
        right: 50px;
}
 
#box4 {
        position:absolute;
        bottom: 50px;
        left: 50px;
}
 

Relative positioning

To position an element relatively, the property position is set as relative. The difference between absolute and relative positioning is how the position is being calculated.
The position for an element which is relatively positioned is calculated from the original position in the document. That means that you move the element to the right, to the left, up or down. This way, the element still obtains a space in the document after it is positioned.
As an example of relative positioning, we can try to position three pictures relatively to their original position on the page. Notice how the pictures leave empty spaces at their original positions in the document:
 
#dog1 {
        position:relative;
        left: 350px;
        bottom: 150px;
}
#dog2 {
        position:relative;
        left: 150px;
        bottom: 500px;
}
 
#dog3 {
        position:relative;
        left: 50px;
        bottom: 700px;
}

Absolute CSS Positioning

Absolute positioning is the easiest to understand. You start with the CSS position property:
position: absolute;
This tells the browser that whatever is going to be positioned should be removed from the normal flow of the document and will be placed in an exact location on the page. It is also taken out of the normal flow of the document - it won't affect how the elements before it or after it in the HTML are positioned on the Web page.
If you want to position an element 10ems from the top of the document window, you would use the "top" offset to position it there with absolute positioning:
position: absolute;
top: 10em;

This element will then always display 10em from the top of the page - no matter what else displays there in normal flow.
The four positioning properties are:
To use them, you need to think of them as offset properties. In other words, an element positioned right 2em is not moved right 2em. It's right side is offset from the right side of the window 2em. The same is true for the other three.

Relative Positioning

Relative positioning uses the same four positioning properties as absolute positioning. But instead of basing the position of the element upon the browser view port, it starts from where the element would be if it were still in the normal flow.
For example, if you have three paragraphs on your based on it's current location - not from the original sides of the view port. Web page, and the third has a position: relative style placed on it, it's position will be offset
<p>Paragraph 1.</p>
<p>Paragraph 2.</p>
<p style="position: relative;left: 2em;">Paragraph 3.</p>

In the above example, the third paragraph will be positioned 3em from the left side of the container element, but will still be below the first two paragraphs. It would remain in the normal flow of the document, and just be offset slightly. If you changed it to position: absolute; anything following it would display on top of it, because it would no longer be in the normal flow of the document.



Example:-



<html>
<head>
<style type="text/css">

a:hover img
  {
  border:3px solid BLACK;
  }

.img1
{
position:absolute;
left:30;
top:45;
}

</style>
</head>
<body>

  <a target="_blank" href="klematis_big.htm">
  <img src="klematis_small.jpg" alt="Klematis" width="110" height="90" />
  </a>

 <img src="klematis_small.jpg" alt="Klematis" width="110" height="90" class="img1"/>
  

</body>
</html>
This entry was posted in :

0 comments:

Post a Comment