Adscend

Click Here



Click Here



Click Here

9.9.13

Position Absolute Relative Difference

Position(Absolute and Relative)


CSS Position Absolute Relative
Position Absolute Relative
There are two point of origin in CSS positioning:
Absolute and Relative.


Absolute position is determined from very top left of the browser.

Example: 1
<html>
<head>
        <style>
              h2 {
                   position: absolute;
                   top: 20px;//20 pixels down from the top
                   left: 50px;//50 pixels right from the left
                }
               p {
                  position: absolute;
                  top: 50px;//50 pixels down from the top
                  left: 100px;//100 pixels right from the left
                }
          </style>
</head>
<body>
         <h2>This is absolute, different position </h2>
         <p>This is absolute, different position </p>
</body>
</html>
Output

This is absolute, same co-ordinate

This is absolute, same co-ordinate

If we place two elements in the same location, i.e. same co-ordinate, elements will be overlapped.  Lets see following example:

Example: 2
<html>
<head>
        <style>
              h2 {
                   position: absolute;
                   top: 20px;//20 pixels down from the top
                   left: 50px;//50 pixels right from the left
                }
               p {
                  position: absolute;
                  top: 20px;//20 pixels down from the top
                  left: 50px;//50 pixels right from the left
                }
          </style>
</head>
<body>
         <h2>This is absolute, same co-ordinate </h2>
         <p>This is absolute, same co-ordinate </p>
</body>
</html>
Output

This is absolute, same co-ordinate

This is absolute, same co-ordinate

Absolute positioning says "I am going to give you complete control over where place your element, I don't care if any other elements is in my place, I don't care any other text in my way. If is it, I overlap, I only care myself.

In this case, we have to apply “relative” attribute.
Relative position is determined from elements current position of the browser.

Example: 3
<html>
<head>
        <style>
              h2 {
                   position: absolute;
                   top: 20px;//20 pixels down from the top
                   left: 50px;//50 pixels right from the left
                }
               p {
                  position: relative;
                  top: 20px;//20 pixels down from the top
                  left: 50px;//50 pixels right from the left
                }
          </style>
</head>
<body>
         <h2>This is absolute, same co-ordinate </h2>
         <p>This is relative, same co-ordinate </p>
</body>
</html>
Output

This is absolute, same co-ordinate

This is absolute, same co-ordinate

Now the text move away where I want.



No comments:

Post a Comment