HTML and CSS · Lesson 7 of 12

Display and positioning

display decides how a box sits among others; position lets you move it out of that flow. Together they explain every "why won't this go there?".

block, inline, inline-block, none

  • block (div, p, h1): starts on a new line and takes the full width; width and height apply.
  • inline (span, a, strong): flows within the text; width and height are ignored.
  • inline-block: flows like text, but takes width, height and vertical padding.
  • none: removed completely, taking no space (visibility: hidden hides it but keeps the space).
Try itEdit the code: the page updates as you type

position

  • static: the default, in the normal flow.
  • relative: moved from its normal place by top/left, while its space stays; also the anchor for absolute children.
  • absolute: taken out of the flow and placed against its nearest positioned ancestor.
  • fixed: placed against the screen; it stays when you scroll.
  • sticky: normal until you scroll past a point, then it sticks (like a table header).

z-index decides which overlapping box is on top, and only works on positioned elements.

Try itEdit the code: the page updates as you type

Your turn

Put a "NEW" label in the top-left corner of the product box, overlapping its edge.

ExerciseEdit the code: the page updates as you type
Show a solution
<style>
  .product { position: relative; width: 240px; padding: 24px; border: 1px solid #ccc; border-radius: 10px; }
  .label { position: absolute; top: -10px; left: -10px; background: #2b8a3e; color: white; padding: 2px 8px; }
</style>
<div class="product">
  <span class="label">NEW</span>
  Wireless earbuds
</div>

In the interview

display: none vs visibility: hidden?

display: none removes the element from the layout entirely; visibility: hidden makes it invisible but it still takes up its space. Both hide it from screen readers.

relative vs absolute positioning?

relative moves an element from where it would normally be and keeps its space; absolute takes it out of the flow and places it against the nearest positioned ancestor (or the page if there's none).

Why isn't my z-index working?

z-index needs a positioned element (or a flex or grid item), and it only compares elements within the same stacking context: a parent with its own z-index, opacity below 1 or a transform starts a new one.

Your progress stays on this device.