CSS position is a property that controls the positioning of HTML elements on a web page. It allows you to specify how an element should be placed in relation to its normal position in the document flow or in relation to other elements.

There are four main values for the position property:

  1. static (Default):

    • This is the default value for most elements. In the normal document flow, elements are laid out in the order they appear in the HTML code. position: static; means the element will not be affected by the top, bottom, left, or right properties. It cannot be moved using the top, bottom, left, or right properties either.
  2. relative:

    • When position: relative; is applied to an element, it remains in the normal document flow but can be moved relative to its normal position using the top, bottom, left, or right properties.
    • When an element is moved with position: relative;, it still occupies its original space in the layout, and other elements are not affected by its movement.
  3. absolute:

    • With position: absolute;, an element is removed from the normal document flow, and its position is based on its nearest positioned ancestor (an ancestor element with a position value of relative, absolute, or fixed).
    • Once an element is positioned absolutely, it can be moved using the top, bottom, left, or right properties. Other elements on the page will not be affected by its movement, and the element will overlap other content if necessary.
  4. fixed:

    • position: fixed; is similar to position: absolute;, but the element is positioned relative to the viewport, meaning it will stay in the same position even when the page is scrolled.
    • Fixed elements are often used for creating headers, footers, or navigation bars that remain visible as the user scrolls through the content.

How to Use position Property:

To use the position property, select the HTML element you want to position and apply the appropriate value. Here's an example:

<!DOCTYPE html>
<html>
<head>
  <title>Position Example</title>
  <style>
    .container {
      width: 200px;
      height: 200px;
      background-color: lightblue;
    }

    .relative-position {
      position: relative;
      top: 20px;
      left: 30px;
    }

    .absolute-position {
      position: absolute;
      top: 50px;
      right: 50px;
    }

    .fixed-position {
      position: fixed;
      bottom: 20px;
      left: 20px;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="relative-position">I am relatively positioned.</div>
    <div class="absolute-position">I am absolutely positioned.</div>
    <div class="fixed-position">I am fixed to the viewport.</div>
  </div>
</body>
</html>

In this example, we have a container <div> with three nested <div> elements. Each nested <div> has a different position value, and we use the top, bottom, left, or right properties to move them accordingly.

  • The first nested <div> has position: relative;, so it is moved 20 pixels down and 30 pixels to the right relative to its normal position in the document flow.
  • The second nested <div> has position: absolute;, so it is positioned 50 pixels from the top and 50 pixels from the right edge of its nearest positioned ancestor (the container <div> in this case).
  • The third nested <div> has position: fixed;, so it is fixed 20 pixels from the bottom and 20 pixels from the left edge of the viewport. It stays in the same position even when scrolling the page.

By using the position property along with the top, bottom, left, or right properties, you can precisely control the positioning of elements on your web page, allowing you to create complex and visually appealing layouts.

Latest Tutorial

Newly published tutorials

Laravel 10 - Tutorial to create CRUD Application

Learn to create crud application using laravel 10. Step by step guidance will help to understand the concept of crud.

Laravel 10 - Tutorial on how to use jQuery to validate the form

Learn how to use jQuery in Laravel 10 to validate the form

Laravel - How to upload file to AWS S3 Bucket

Explore how we can upload file using the laravel on Amazon S3 bucket.