Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

CSS Positions — static, relative, absolute, fixed

The position Property

The position property controls how an element is placed in the document. Once positioned, you use top, right, bottom, and left to offset it.

ValueDescriptionOffset relative to
staticDefault - normal document flow, offsets ignoredN/A
relativeOffset from its normal position, still occupies spaceIts own normal position
absoluteRemoved from flow, positioned relative to nearest positioned ancestorNearest positioned ancestor
fixedRemoved from flow, stays fixed on screen during scrollViewport
stickyRelative until scroll threshold, then fixedScroll container
All Position Values - Examples
/* static - default, no positioning */
.static { position: static; }

/* relative - offset from normal position, still takes up space */
.relative {
    position: relative;
    top: 20px;    /* moves DOWN 20px from normal position */
    left: 30px;   /* moves RIGHT 30px */
    /* Original space is preserved */
}

/* absolute - removed from flow, positioned inside nearest positioned ancestor */
.container {
    position: relative; /* establishes positioning context */
    width: 400px;
    height: 300px;
}
.absolute {
    position: absolute;
    top: 0;
    right: 0;       /* top-right corner of .container */
    width: 100px;
    height: 100px;
}

/* Center absolutely positioned element */
.centered {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

/* fixed - stays in viewport during scroll */
.navbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    background: white;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.back-to-top {
    position: fixed;
    bottom: 30px;
    right: 30px;
}

/* sticky - relative until scroll threshold, then fixed */
.sticky-header {
    position: sticky;
    top: 0;         /* sticks when it reaches top of viewport */
    background: white;
    z-index: 100;
}

/* z-index - stacking order (higher = on top) */
.modal-overlay { z-index: 999; }
.modal         { z-index: 1000; }
.tooltip       { z-index: 1001; }

Practical: Tooltip with Absolute Positioning

CSS Tooltip - Absolute Positioning
.tooltip-wrapper {
    position: relative;
    display: inline-block;
}

.tooltip-text {
    position: absolute;
    bottom: 125%;           /* above the element */
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 6px 10px;
    border-radius: 4px;
    white-space: nowrap;
    font-size: 0.85rem;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s;
    z-index: 10;
}

/* Arrow pointing down */
.tooltip-text::after {
    content: '';
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-top-color: #333;
}

.tooltip-wrapper:hover .tooltip-text {
    opacity: 1;
    visibility: visible;
}
<span class="tooltip-wrapper">
    Hover me
    <span class="tooltip-text">This is a tooltip!</span>
</span>

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.