Web Development · Module 3 · Lesson 41 min read

Display & Positioning

display

ValueBehaviour
blockfull width, starts on a new line (div, p, h1)
inlineflows inside text; width and height are ignored (span, a)
inline-blockflows inline but respects width, height and padding
flex / gridmodern layout containers (next module)
nonehidden and takes no space

visibility: hidden hides an element but keeps its space.

position

ValueHow it moves
staticnormal flow (default)
relativenudged from its normal spot; becomes the anchor for absolute children
absoluteplaced relative to the nearest positioned ancestor; leaves the flow
fixedstuck to the screen while scrolling (chat buttons)
stickyscrolls normally, then sticks (top: 0) at a point (table headers, nav bars)

Use top, right, bottom and left to place positioned elements, and z-index for which is on top (it only works on positioned or flex/grid items).

Common pattern: a badge on a card

Make the parent position: relative and the badge position: absolute; top: 8px; right: 8px;.

Overflow

overflow: hidden | auto | scroll. Use auto for scrollable areas, and text-overflow: ellipsis with white-space: nowrap for "…".

Example

HTML
<style>
  body { font-family: sans-serif; height: 1200px; }
  .nav {
    position: sticky; top: 0;
    background: #111827; color: white; padding: 12px;
  }
  .card {
    position: relative;
    width: 220px; margin: 24px; padding: 20px;
    border: 1px solid #ddd; border-radius: 12px;
  }
  .badge {
    position: absolute; top: -10px; right: -10px;
    background: crimson; color: white;
    border-radius: 999px; padding: 4px 10px; font-size: 12px;
  }
  .fab {
    position: fixed; right: 20px; bottom: 20px;
    background: #6366f1; color: white;
    border-radius: 50%; width: 56px; height: 56px;
    display: grid; place-items: center; font-size: 24px;
  }
</style>

<div class="nav">Sticky navigation (scroll down)</div>
<div class="card">Product card <span class="badge">SALE</span></div>
<div class="fab">+</div>