Search
Close this search box.
Course Content
CSS Basic
0/1
CSS Selectors
0/1
CSS Comments
0/1
CSS Backgrounds
0/1
CSS Borders
0/1
CSS Outline
0/1
CSS Fonts
0/1
CSS Height and Width
0/1
CSS Margins and Paddings
0/2
CSS Icons
0/1
CSS Links
0/1
CSS Lists
0/1
CSS Tables
0/1
CSS Display Properties
0/1
CSS Max-Width Property
0/1
CSS Positioning Elements
0/1
CSS Z-Index Property
0/1
CSS Overflow
0/1
CSS Float
0/1
CSS Opacity
0/1
CSS Forms
0/1
CSS Dropdowns
0/1
CSS Buttons
0/1
CSS Media Queries
0/1
About Lesson

1. Static:

Definition:

In CSS, the position: static property is the default position value for elements. When an element is set to position: static, it follows the normal flow of the document. In other words, it will be positioned according to the normal document flow, and properties like top, right, bottom, and left have no effect on it.

Example:

<!DOCTYPE html>
<html lang=”en”>

<head>
    <meta charset=”UTF-8″>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <title>Static Position Example</title>
    <style>
        .box {
            background-color: lightblue;
            width: 200px;
            height: 100px;
            /* By default, the position is static */
            /* top, right, bottom, left properties have no effect */
            /* These are just for demonstration purposes */
            top: 50px;
            left: 50px;
        }
    </style>
</head>

<body>

    <div class=”box”>
        <p>This is a static positioned box.</p>
    </div>

</body>

</html>

Output:

 

2. Relative Positioning:

Definition:

The element is positioned relative to its normal position.

Example:

<!DOCTYPE html>
<html lang=”en”>

<head>
    <meta charset=”UTF-8″>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <style>
        .relative-position {
            position: relative;
            top: 20px;
            left: 30px;
            background-color: #ffcccb;
            padding: 10px;
        }
    </style>
    <title>Relative Positioning Example</title>
</head>

<body>
    <div class=”relative-position”>
        This is relatively positioned.
    </div>
</body>

</html>

Output:

 

3. Absolute positioning:

Definition:

The element is positioned relative to its nearest positioned ancestor or the initial containing block.

Example:

<!DOCTYPE html>
<html lang=”en”>

<head>
    <meta charset=”UTF-8″>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <style>
        .absolute-position {
            position: absolute;
            top: 50px;
            left: 50px;
            background-color: #aeeeee;
            padding: 10px;
        }
    </style>
    <title>Absolute Positioning Example</title>
</head>

<body>
    <div class=”absolute-position”>
        This is absolutely positioned.
    </div>
</body>

</html>

Output:

 

4. Fixed Positioning:

Definition:

The element is positioned relative to the browser window.

Example:

<!DOCTYPE html>
<html lang=”en”>

<head>
    <meta charset=”UTF-8″>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <style>
        .fixed-position {
            position: fixed;
            top: 10px;
            right: 10px;
            background-color: #98fb98;
            padding: 10px;
        }
    </style>
    <title>Fixed Positioning Example</title>
</head>

<body>
    <div class=”fixed-position”>
        This is fixed positioned.
    </div>
    <!– Some content to demonstrate scrolling –>
    <div style=”height: 1000px;”></div>
</body>

</html>

Output:

4. Sticky Positioning:

Definition:

position: sticky is a CSS property that allows an element to remain positioned based on the user’s scroll position. When the specified element reaches a certain point on the screen (defined by top, right, bottom, or left properties), it becomes “stuck” to that position and remains there as the user continues to scroll.

Example:

<!DOCTYPE html>
<html lang=”en”>

<head>
    <meta charset=”UTF-8″>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <title>Sticky Example</title>
    <style>
        .container {
            height: 1500px;
            /* Added height to demonstrate scrolling */
            position: relative;
            /* Necessary for positioning */
        }

        .sticky {
            position: -webkit-sticky;
            /* For Safari */
            position: sticky;
            top: 20px;
            /* Stick 20px from the top of the viewport */
            background-color: lightblue;
            padding: 10px;
        }

        /* Just for styling purposes */
        .content {
            height: 200px;
            background-color: #f0f0f0;
            margin-bottom: 20px;
            padding: 10px;
        }
    </style>
</head>

<body>
    <div class=”container”>
        <div class=”sticky”>
            This is a sticky element. It will stick 20px from the top of the viewport when you scroll down.
        </div>
        <div class=”content”>
            Content 1
        </div>
        <div class=”content”>
            Content 2
        </div>
        <div class=”content”>
            Content 3
        </div>
    </div>
</body>

</html>

Output: