Course

Web Development
  • 1.1 Introduction to the Web
  • 2.1 HTML Basics
  • 2.2 Text & Content
  • 2.3 Layout & Grouping
  • 2.4 Lists & Tables
  • 2.5 Media & Embedding
  • 2.6 Forms
  • 2.7 Semantic HTML
  • 2.8 Accessibility & SEO
  • 3.1 CSS Introduction
  • 3.2 CSS Syntax
  • 3.3 CSS Selectors
  • 3.4 Typography
  • 3.5 Colors & Backgrounds
  • 3.6 Text Styling
  • 3.7 Box Model
  • 3.8 Units
  • 3.9 Display & Positioning
  • 3.10 Layout Systems
  • 3.11 Responsive Design
  • 3.12 Animations
  • 3.13 Modern CSS
  • 3.14 Performance & Accessibility
  • 3.15 Tailwind CSS Basics
  • 4.1 Introduction to JavaScript
  • 4.2 Variables & Scope
  • 4.3 Data Types
  • 4.4 Type Conversion
  • 4.5 Operators
  • 4.6 Control Flow
  • 4.7 Loops
  • 4.8 Functions
  • 4.9 Advanced Functions
  • 4.10 Objects & Arrays
  • 4.11 The this Keyword

3.9 Display & Positioning

Updated Jul 12, 2026

3.9 Display & Positioning

Updated Jul 12, 2026

Display & Positioning

The display and position properties control how elements appear, behave, and are placed on a webpage.

They are used to build layouts, align content, and position elements.


Display Property

The display property determines how an element behaves in the layout.

display: value;

Inline

Inline elements only take up as much width as needed.

They stay on the same line and generally ignore width and height.

Examples:

<span>Hello</span>
<a>Link</a>
<strong>Bold</strong>

Output:

Hello Link Bold

Example:

span {
    width: 300px;
}

The width will not work properly because inline elements do not support width and height.


Block

Block elements take up the full available width and always start on a new line.

Examples:

<div></div>

<p></p>

<h1></h1>

<section></section>

Output:

Block 1

Block 2

Block 3

Example:

div {
    background: lightblue;
}

Inline-Block

Inline-block combines features of both inline and block elements.

It:

  • Stays on the same line
  • Supports width
  • Supports height

Example:

.box {

    display: inline-block;

    width: 150px;

    height: 100px;

}
<div class="box">One</div>
<div class="box">Two</div>

Output:

[One] [Two]

Display Comparison

Display New Line Width & Height
inline ❌ ❌
block ✅ ✅
inline-block ❌ ✅

Visibility

The visibility property controls whether an element is visible.

Visible:

visibility: visible;

Hidden:

visibility: hidden;

Unlike display: none, the element still occupies space.

Item 1

[Hidden Space]

Item 3

display: none vs visibility: hidden

display: none

display: none;

The element is completely removed from the layout.

visibility: hidden

visibility: hidden;

The element becomes invisible but its space remains.


Opacity

The opacity property controls transparency.

opacity: value;

Values range from:

0 → Invisible
1 → Fully Visible

Examples:

opacity: 1;
opacity: 0.5;
opacity: 0;

Example:

.card {
    opacity: 0.8;
}

Position Property

The position property controls where an element appears on the page.

Common values:

  • static
  • relative
  • absolute
  • fixed
  • sticky

Static

The default position for all elements.

position: static;

Elements remain in the normal document flow.


Relative

Moves an element relative to its original position.

.box {

    position: relative;

    top: 20px;

    left: 30px;

}

The original space remains reserved.


Absolute

Removes the element from the normal document flow.

The element is positioned relative to the nearest positioned ancestor.

.box {

    position: absolute;

    top: 0;

    right: 0;

}

Common uses:

  • Badges
  • Dropdowns
  • Tooltips
  • Modals

Relative + Absolute

Parent:

.parent {

    position: relative;

}

Child:

.child {

    position: absolute;

    top: 10px;

    right: 10px;

}

The child is positioned inside the parent.


Fixed

Fixed elements stay in the same place while the page scrolls.

.navbar {

    position: fixed;

    top: 0;

    width: 100%;

}

Common uses:

  • Navigation bars
  • Floating buttons
  • Chat widgets

Sticky

Sticky elements behave like relative until they reach a scroll position.

After that, they behave like fixed.

.sidebar {

    position: sticky;

    top: 0;

}

Common uses:

  • Sticky headers
  • Sidebars
  • Table headers

Position Comparison

Position Normal Flow Scrolls
static ✅ ✅
relative ✅ ✅
absolute ❌ Depends
fixed ❌ ❌
sticky Partial Sticky After Threshold

z-index

The z-index property controls the stacking order of positioned elements.

.box1 {
    z-index: 1;
}

.box2 {
    z-index: 10;
}

box2 appears above box1.

z-index only works on positioned elements.

Supported positions:

  • relative
  • absolute
  • fixed
  • sticky

Example:

.modal {

    position: fixed;

    z-index: 9999;

}

Complete Example

<div class="card">

    Frontend Development

</div>
.card {

    display: inline-block;

    width: 300px;

    padding: 20px;

    position: relative;

    opacity: 0.9;

}

Real-World Examples

Fixed navbar:

.navbar {

    position: fixed;

    top: 0;

    width: 100%;

    z-index: 1000;

}

Notification badge:

.badge {

    position: absolute;

    top: -5px;

    right: -5px;

}

Sticky sidebar:

.sidebar {

    position: sticky;

    top: 20px;

}

Best Practices

  • Use block elements for layouts.
  • Prefer Flexbox or Grid instead of inline-block for modern layouts.
  • Use position: relative on the parent before using absolute children.
  • Prefer sticky over fixed when appropriate for a better scrolling experience.

Key Takeaway

The display property controls how elements behave, while the position property controls where elements appear.

Display
↓
inline
block
inline-block

Position
↓
static
relative
absolute
fixed
sticky

Example:

.card {

    display: block;

    position: relative;

    z-index: 10;

}
3.8 Units3.10 Layout Systems