CSS Display & Position

Introduction

CSS display and position properties control how elements are shown and placed on a web page. These properties are essential for creating layouts, aligning elements, and building modern web designs.

In this lesson, you will learn how different display types work and how positioning helps control element placement.

What You’ll Learn

  • CSS display property
  • Block, inline, inline-block
  • Display none vs visibility hidden
  • CSS position property
  • Types of positioning
  • Real-world usage

🔹 CSS Display Property

The display property defines how an element is displayed on the page.

📦 Block Elements

Block elements take full width and start on a new line.

Examples:

  • <div>
  • <p>
  • <h1>

Example:

div {
  display: block;
}

Behavior:

👉 Each element appears on a new line.

🔹 Inline Elements

Inline elements do not start on a new line and take only required width.

Examples:

  • <span>
  • <a>
  • <strong>

Example:

span {
  display: inline;
}

🔹 Inline-Block

Combines features of both block and inline.

div {
  display: inline-block;
  width: 150px;
}

👉 Allows width/height + stays inline.

🔹 Display None

div {
  display: none;
}

👉 Element is completely removed from layout.

🔹 Visibility Hidden

div {
  visibility: hidden;
}

👉 Element is hidden but space remains.

🔹 Flex (Important 🔥)

.container {
  display: flex;
}

👉 Used for modern layouts and alignment.

🔹 CSS Position Property

The position property defines how an element is positioned.

🔹 Relative Position

div {
  position: relative;
  top: 10px;
}

👉 Moves relative to its original position.

🔹 Absolute Position

div {
  position: absolute;
  top: 0;
  left: 0;
}

👉 Positioned relative to nearest positioned parent.

🔹 Fixed Position

div {
  position: fixed;
  top: 0;
}

👉 Stays fixed on screen (navbar example).

🔹 Sticky Position

div {
  position: sticky;
  top: 0;
}

👉 Sticks when scrolling.

🔹 Sticky Position

div {
  position: sticky;
  top: 0;
}

👉 Sticks when scrolling.

🔹 Z-Index (Layering 🔥)

div {
  position: absolute;
  z-index: 10;
}

👉 Higher value = appears on top.

🔹 Display vs Position (Difference)

PropertyPurpose
DisplayLayout behavior
PositionPlacement control

🔹 Real-Life Examples

  • Navigation bars → fixed
  • Popups → absolute
  • Cards layout → flex
  • Sticky header → sticky

🔹 Important Notes

  • Use flex for layouts
  • Avoid overusing absolute positioning
  • Always set parent for absolute elements
  • Use z-index carefully

🔹 Common Mistakes

  • Forgetting position: relative on parent
  • Misusing absolute positioning
  • Confusing display and position
  • Overlapping elements incorrectly

🔹 Best Practices

  • Use flexbox for layout
  • Keep positioning simple
  • Avoid unnecessary complexity
  • Test on different screen sizes

🔹 Practice Tasks

  • Task 1: Create block and inline elements.
  • Task 2: Use inline-block for layout.
  • Task 3: Hide elements using display and visibility.
  • Task 4: Position elements using relative and absolute.
  • Task 5: Create a fixed navbar.

FAQs

What is display property?

It controls how elements appear on the page.

What is position property?

It controls placement of elements.

Difference between display none and visibility hidden?

Display removes element, visibility hides it.

Conclusion

CSS display and position properties are essential for building layouts and controlling element placement. By understanding how elements behave and how positioning works, you can create flexible and responsive designs.

👉 In the next tutorial, you will learn about CSS Flexbox for advanced layouts.

Related Tutorials

  • CSS Introduction

    Introduction CSS stands for Cascading Style Sheets. It is used to style and design web pages created with HTML. While HTML is used to structure content, CSS is used to control how that content looks on the screen. With CSS, you can change colors, fonts, layouts, spacing, and overall appearance of a website. In this…

  • CSS Syntax & Selectors

    Introduction CSS syntax defines how styles are written and applied to HTML elements. Selectors are used to target specific elements on a webpage and apply styles to them. Understanding CSS syntax and selectors is essential for writing effective and clean CSS code. In this lesson, you will learn the basic structure of CSS and different…

  • Colors, Units & Backgrounds

    Introduction CSS allows you to control the appearance of web pages using colors, units, and background properties. These are essential for designing visually appealing and responsive websites. In this lesson, you will learn how to apply colors, use different units, and style backgrounds in CSS. What You’ll Learn These are core CSS skills used on…

  • CSS Borders & Box Model

    Introduction CSS borders and the box model are essential concepts for designing layouts and controlling spacing in web development. Every HTML element is treated as a rectangular box, and understanding how this box works helps you create clean and structured designs. In this lesson, you will learn how to style borders and understand the CSS…

  • CSS Flexbox

    Introduction CSS Flexbox is a powerful layout system used to design flexible and responsive layouts. It makes it easy to align items, distribute space, and create modern UI designs without using complex positioning or floats. Flexbox works on a one-dimensional layout (row or column) and is widely used in real-world web development. What You’ll Learn…

  • CSS Flexbox – Advanced

    Introduction In the previous lesson, you learned the basics of Flexbox, including alignment, direction, and spacing. In this advanced guide, we will explore deeper Flexbox concepts that are used in real-world layouts. These advanced properties help you build more flexible, responsive, and professional designs. What You’ll Learn Quick Recap (Short Section) Flexbox works with: Main…

Leave a Reply

Your email address will not be published. Required fields are marked *