Open In App

CSS Positioning Elements

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

CSS positioning allows you to control the layout of HTML elements using properties such as top, right, bottom, and left. Understanding the different types of position properties in CSS—static, relative, absolute, fixed, and sticky—can enhance the design and functionality of web pages. This article explores these properties with examples, illustrating how each can be applied to achieve various layout effects.

There are five different types of position properties available in CSS:

Position PropertyDescription
FixedAn element with position: fixed property remains in the same position relative to the viewport even when the page is scrolled.
StaticDefault positioning method. Elements with position: static are positioned according to the normal flow of the document.
RelativeElements with position: relative are positioned relative to their normal position in the document flow. Other elements will not fill the gap left by this element when adjusted.
AbsolutePositioned concerning its nearest non-static ancestor. Elements with position: absolute are taken out of the normal document flow.
StickyCombines features of position: relative and position: fixed. The element is treated as position: relative until it reaches a specified threshold, then it becomes position: fixed.

This table provides a concise overview of the different positioning methods and their characteristics.

Example 1: The below example illustrates the CSS positioning element by using the position: fixed property.

HTML
<!DOCTYPE html>
<html>

<head>
    <title> CSS Positioning Element</title>
    <style>
    body {
        margin: 0;
        padding: 20px;
        font-family: sans-serif;
        background: #efefef;
    }
    
    .fixed {
        position: fixed;
        background: #cc0000;
        color: #ffffff;
        padding: 30px;
        top: 50;
        left: 10;
    }
    
    span {
        padding: 5px;
        border: 1px #ffffff dotted;
    }
    </style>
</head>

<body>
    <div class="fixed">This div has 
        <span>position: fixed;</span>
    </div> 
    <pre>
            Learn the most common Data Structures & Algorithms 
            to solve coding problems. Enrol now! Master core 
            Data Structures & Algorithms to ace interviews 
            from IIT & Stanford Alumni. TA Support. Placements 
            in Companies. Get Certified. 350+ Problems.A data 
            structure is a particular way of organizing data in 
            a computer so that it can be used effectively.
            Learn the most common Data Structures & Algorithms 
            to solve coding problems. Enrol now! Master core 
            Data Structures & Algorithms to ace interviews 
            from IIT & Stanford Alumni. TA Support. Placements 
            in Companies. Get Certified. 350+ Problems.A data 
            structure is a particular way of organizing data in 
            a computer so that it can be used effectively.
            Learn the most common Data Structures & Algorithms 
            to solve coding problems. Enrol now! Master core 
            Data Structures & Algorithms to ace interviews 
            from IIT & Stanford Alumni. TA Support. Placements 
            in Companies. Get Certified. 350+ Problems.A data 
            structure is a particular way of organizing data in 
            a computer so that it can be used effectively.
            Learn the most common Data Structures & Algorithms 
            to solve coding problems. Enrol now! Master core 
            Data Structures & Algorithms to ace interviews 
            from IIT & Stanford Alumni. TA Support. Placements 
            in Companies. Get Certified. 350+ Problems.A data 
            structure is a particular way of organizing data in 
            a computer so that it can be used effectively.
    </pre> 
</body>

</html>

Output:

Example 2: The below example illustrates the CSS positioning element by using the position: static property.

HTML
<!DOCTYPE html>
<html>

<head>
    <title> CSS Positioning Element</title>
    <style>
    body {
        margin: 0;
        padding: 20px;
        font-family: sans-serif;
        background: #efefef;
    }
    
    .static {
        position: static;
        background: #cc0000;
        color: #ffffff;
        padding: 30px;
    }
    
    span {
        padding: 5px;
        border: 1px #ffffff dotted;
    }
    </style>
</head>

<body>
    <div class="static">
        This div has 
            <span>position: static;</span>
    </div> 
    <pre>
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
    </pre> 
</body>

</html>

Output:

Example 3: The below example illustrates the CSS positioning element by using the position: relative property. In the below illustration, the top and left coordinates of the div are each 100px.

HTML
<!DOCTYPE html>
<html>

<head>
    <title> CSS Positioning Element</title>
    <style>
    body {
        margin: 0;
        padding: 20px;
        font-family: sans-serif;
        background: #efefef;
    }
    
    .relative {
        position: relative;
        background: #cc0000;
        color: #ffffff;
        padding: 30px;
        top:100px;
        left:100px;
    }
    
    span {
        padding: 5px;
        border: 1px #ffffff dotted;
    }
    </style>
</head>

<body>
    <pre>
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        <div class="relative">
            This div has <span>position: relative;</span>
        </div>
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
    </pre> 
</body>

</html>

Output:

Example 4: The below example illustrates the CSS positioning element by using the position: absolute property.

HTML
<!DOCTYPE html>
<html>

<head>
    <title> CSS Positioning Element</title>
    <style>
    body {
        margin: 0;
        padding: 20px;
        font-family: sans-serif;
        background: #efefef;
    }
    
    .absolute {
        position: absolute;
        background: #cc0000;
        color: #ffffff;
        padding: 30px;
        font-size: 15px;
        bottom: 20px;
        right: 20px;
    }
    
    .relative {
        position: relative;
        background: #aad000;
        height: 300px;
        font-size: 30px;
        border: 1px solid #121212;
        text-align: center;
    }
    
    span {
        padding: 5px;
        border: 1px #ffffff dotted;
    }
    
    pre {
        padding: 20px;
        border: 1px solid #000000;
    }
    </style>
</head>

<body>
     <pre>
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
                <div class="relative">
                    <div >
                        This div has 
                        <span>
                            <strong>position: relative;</strong>
                        </span>
                    </div>
                    <div class="absolute">
                       This div has 
                            <span>
                                <strong>position: absolute;</strong>
                            </span>
                    </div>
                </div>
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
    </pre> 
</body>

</html>

Output:

Example 5: The below example illustrates the CSS positioning element by using the position: sticky property.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>CSS Positioning Elements</title>
    <style>
        body {
            margin: 0;
            padding: 20px;
            font-family: sans-serif;
            background: #efefef;
        }

        .sticky {
            position: -webkit-sticky; /* For Safari */
            position: sticky;
            top: 0;
            background: #00aaff;
            color: #ffffff;
            padding: 10px;
            font-size: 20px;
            border: 1px solid #000000;
        }

        pre {
            padding: 20px;
            border: 1px solid #000000;
            overflow: auto;
            max-height: 500px; /* Added to show the effect of scrolling */
        }
    </style>
</head>

<body>
    <pre>
        <div class="sticky">
            This div has 
            <span>
                <strong>position: sticky;</strong>
            </span>
        </div>
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
        Learn the most common Data Structures & Algorithms 
        to solve coding problems. Enrol now! Master core 
        Data Structures & Algorithms to ace interviews 
        from IIT & Stanford Alumni. TA Support. Placements 
        in Companies. Get Certified. 350+ Problems.A data 
        structure is a particular way of organizing data in 
        a computer so that it can be used effectively.
    </pre> 
</body>

</html>

Output:

CSS positioning is essential for creating responsive and visually appealing web designs. By utilizing properties like position: static, relative, absolute, fixed, and sticky, developers can precisely control the placement and behavior of HTML elements on a webpage. Experimenting with these properties can lead to innovative and dynamic CSS layouts, improving the user experience in web design.

Supported Browsers:

  • Google Chrome
  • Microsoft Edge
  • Firefox
  • Opera
  • Safari

CSS Positioning Elements – FAQs

What is CSS positioning?

CSS positioning allows you to control the layout and placement of elements on a webpage using the position property.

What are the different types of positioning in CSS?

The position property has five main values: static, relative, absolute, fixed, and sticky.

What is static positioning?

Static is the default positioning for all elements. Elements are positioned according to the normal document flow.

What is relative positioning?

Relative positioning moves an element relative to its normal position without affecting the layout of surrounding elements. Use top, right, bottom, or left properties to move it.

What is absolute positioning?

Absolute positioning places an element relative to the nearest positioned ancestor (not static). If no positioned ancestor exists, it’s placed relative to the initial containing block (usually the viewport).

What is fixed positioning?

Fixed positioning keeps an element fixed relative to the viewport. It does not move when the page is scrolled.

What is sticky positioning?

Sticky positioning switches between relative and fixed based on the user’s scroll position. It sticks to a position when scrolling past a specified point.



Next Article

Similar Reads

Article Tags :
three90RightbarBannerImg