Web Development (Session 07)

Shape Image One

POSITIONS
AND
Z-INDEX IN CSS

Introduction

CSS positioning allows you to control the layout of elements on a web page. This includes defining how elements are placed in relation to one another and to their containing elements. The z-index property controls the stacking order of positioned elements.

CSS Positioning

1. Static Positioning (Default):
  • Elements are positioned according to the normal flow of the document.
  • This is the default position value for all HTML elements.

    div {

        position: static;

    }

2. Relative Positioning:
  • Elements are positioned relative to their normal position.
  • Other elements are not affected and will not move to fill any gap left by the element.

    div {

        position: relative;

        top: 10px;

        left: 20px;

    }

3. Absolute Positioning:
  • Elements are positioned relative to the nearest positioned ancestor (non-static).
  • If there is no such ancestor, the element will be positioned relative to the initial containing block (usually the HTML element).

div {

    position: absolute;

    top: 50px;

    left: 100px;

}

4. Fixed Positioning:
  • Elements are positioned relative to the browser window.
  • They remain in the same position regardless of scrolling.

    div {

        position: fixed;

        top: 0;

        right: 0;

    }

    5. Sticky Positioning:
    • Elements are positioned based on the user’s scroll position.
    • It toggles between relative and fixed positioning depending on the scroll position.

    div {

        position: sticky;

        top: 0;

    }

    Z-Index

    The z-index property determines the stacking order of positioned elements (elements with a position value other than static). An element with a greater z-index value will be in front of an element with a lower z-index value.

    1. Basic Z-Index Usage:
    • Only works on positioned elements (relative, absolute, fixed, or sticky).

    div {

        position: absolute;

        z-index: 1;

    }

    2. Example with Multiple Elements:
    • Elements with higher z-index values appear in front of those with lower values.

    <div class=”box1″>Box 1</div>

    <div class=”box2″>Box 2</div>

    <div class=”box3″>Box 3</div>

    <style>

        .box1 {

            position: absolute;

            top: 50px;

            left: 50px;

            width: 100px;

            height: 100px;

            background-color: red;

            z-index: 1;

        }

        .box2 {

            position: absolute;

            top: 70px;

            left: 70px;

            width: 100px;

            height: 100px;

            background-color: blue;

            z-index: 2;

        }

        .box3 {

            position: absolute;

            top: 90px;

            left: 90px;

            width: 100px;

            height: 100px;

            background-color: green;

            z-index: 3;

        }

    </style>

    Examples and Use Cases

    1. Creating a Layered Layout:

    Using z-index to create a multi-layered design.

    <div class=”layer1″>Layer 1</div>

    <div class=”layer2″>Layer 2</div>

    <div class=”layer3″>Layer 3</div>

    <style>

        .layer1 {

            position: absolute;

            width: 200px;

            height: 200px;

            background-color: lightblue;

            z-index: 1;

        }

        .layer2 {

            position: absolute;

            width: 200px;

            height: 200px;

            background-color: lightgreen;

            top: 20px;

            left: 20px;

            z-index: 2;

        }

        .layer3 {

            position: absolute;

            width: 200px;

            height: 200px;

            background-color: lightcoral;

            top: 40px;

            left: 40px;

            z-index: 3;

        }

    </style>

    2. Fixed Header with Content Scroll:

    Using position: fixed to create a fixed header that stays at the top while the content scrolls.

    <header class=”fixed-header”>Header</header>

    <main>

        <p>Lots of content here…</p>

    </main>

    <style>

        .fixed-header {

            position: fixed;

            width: 100%;

            top: 0;

            background-color: navy;

            color: white;

            padding: 10px;

            text-align: center;

            z-index: 1000;

        }

        main {

            margin-top: 50px;

        }

    </style>

    3. Sticky Navigation Bar:

    Using position: sticky to create a navigation bar that sticks to the top of the viewport when scrolling.

    <nav class=”sticky-nav”>Navigation Bar</nav>

    <main>

        <p>Lots of content here…</p>

    </main>

    <style>

        .sticky-nav {

            position: sticky;

            top: 0;

            background-color: darkslategray;

            color: white;

            padding: 10px;

            text-align: center;

        }

        main {

            margin-top: 50px;

        }

    </style>

    Additional Resources

    Articles:
    Videos:

    CSS Positioning Tutorial on YouTube by Traversy Media

    Conclusion

    Understanding CSS positioning and the z-index property is crucial for creating complex layouts and ensuring elements are displayed correctly on a web page. Practice using these properties to master their usage.

    Optimized by Optimole
    WhatsApp whatsapp
    Messenger messenger
    Instagram instagram
    Call Us phone
    chat