DEV Community

Cover image for CSS Display and Positioning Basics
Sharique Siddiqui
Sharique Siddiqui

Posted on

CSS Display and Positioning Basics

Building web layouts isn’t just about colors, fonts, and borders—it’s about how elements are displayed on a page and how they relate to each other in space. This is where CSS display and positioning come in. Mastering them is key to creating clean, responsive, and flexible designs.

In this post, we’ll cover the basics of display and position, two of the most important layout concepts in CSS.

The display Property

The display property defines how an element is rendered in the document. By default, HTML elements have a display type—for example, paragraphs (<p>) are block elements, while <span> is inline.

1. Block

Takes up the full width available, starts on a new line.

css
div {
  display: block;
}
Enter fullscreen mode Exit fullscreen mode
Examples: <div>, <p>, <h1>–<h6>
2. Inline

Only takes up as much width as its content, doesn’t force a line break.

css
span {
  display: inline;
}
Enter fullscreen mode Exit fullscreen mode
Examples: <span>, <a>, <em>
3. Inline-block

Behaves like inline elements (sits next to others) but allows setting width, height, margin, and padding (unlike pure inline).

css
button {
  display: inline-block;
}
Enter fullscreen mode Exit fullscreen mode
4. None

Completely removes the element from the layout (not visible, no space reserved).

css
.sidebar {
  display: none;
}
Enter fullscreen mode Exit fullscreen mode
5. Other Modern Options
  • flex → turns container into a flexbox.
  • grid → enables powerful grid layouts.

(We’ll dive deeper into Flexbox and Grid in future posts!)

The position Property

The position property specifies how an element is placed on the page. It works with offset properties (top, right, bottom, left).

1. Static (default)

Every element is static by default—appears in normal document flow. No positioning applied.

css
p {
  position: static;
}
Enter fullscreen mode Exit fullscreen mode
2. Relative

Element stays in normal flow, but can be shifted relative to its original position.

css
.box {
  position: relative;
  top: 20px;
  left: 10px;
}
Enter fullscreen mode Exit fullscreen mode
3. Absolute

Element is positioned relative to the nearest ancestor with position: relative (or non-static). It is removed from normal flow.

css
.child {
  position: absolute;
  top: 0;
  right: 0;
}
Enter fullscreen mode Exit fullscreen mode
4. Fixed

Element is positioned relative to the viewport. It stays in place even when the page is scrolled.

css
.navbar {
  position: fixed;
  top: 0;
  width: 100%;
}
Enter fullscreen mode Exit fullscreen mode
5. Sticky

A hybrid between relative and fixed. It acts like relative until the user scrolls, and then it "sticks" like fixed.

css
header {
  position: sticky;
  top: 0;
}
Enter fullscreen mode Exit fullscreen mode

Z-Index and Stacking

When elements overlap, the z-index property determines which one appears on top.

css
.modal {
  position: absolute;
  z-index: 1000;
}
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Start simple: use block, inline, and inline-block before diving into Flexbox and Grid.
  • Reserve absolute and fixed for cases where normal flow won’t work (like modals, floating buttons).
  • Use sticky for headers and menus that need to remain visible while scrolling.
  • Always test how your layout behaves in different screen sizes.

Final Thoughts

Understanding CSS display and positioning is the first step in mastering layouts. By combining the right display type with smart positioning, you gain precise control over how elements appear, align, and move on the page.

Get comfortable with these basics, and soon you’ll be ready for advanced layout systems like Flexbox and CSS Grid.

Check out the YouTube Playlist for great CSS content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ...CodenCloud

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.