HTML & CSSEasy
What is the display property?
display controls how an element generates boxes: its outer role in the surrounding flow (block or inline) and the layout model it uses for its own children (normal flow, flex, grid). It is the single most important layout property, and display: none removes an element from rendering entirely.
Common values
block: starts on a new line and fills the available width (<div>,<p>,<h1>).inline: flows within text; width, height and vertical margins are ignored (<span>,<a>).inline-block: flows inline but respects width, height and padding.flex/inline-flex: one-dimensional layout for children.grid/inline-grid: two-dimensional layout for children.none: removed from layout and from the accessibility tree.contents: the element's own box disappears and its children act as children of its parent (useful for grid layouts; can affect semantics of some elements).flow-root: a block that contains its floats and stops margin collapsing.- Table values:
table,table-cell,list-item.
Two-value syntax
Modern CSS splits it into outer and inner display: display: block flex is the same as flex; inline flex is inline-flex.
Things to remember
- Each element has a default from the browser stylesheet.
display: nonecannot be transitioned in older browsers, but modern browsers supporttransition-behavior: allow-discretewith@starting-style.- Changing
displayon a<ul>or<table>may affect how screen readers announce it in some browsers.
Code example
html
1<style>2 body { font-family: system-ui; }3 .demo > * { background: #dbeafe; border: 2px solid #0b1020; padding: 4px 8px; margin: 4px; }4 .inline { display: inline; width: 200px; } /* width ignored */5 .inline-block { display: inline-block; width: 120px; }6 .flex { display: flex; gap: 8px; }7 .grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }8 .gone { display: none; }9</style>10<div class="demo">11 <div>block (full width)</div>12 <span class="inline">inline</span><span class="inline">inline</span>13 <span class="inline-block">inline-block</span><span class="inline-block">inline-block</span>14 <div class="flex"><b>flex</b><b>items</b><b>in a row</b></div>15 <div class="grid"><b>grid</b><b>three</b><b>columns</b><b>wraps</b></div>16 <div class="gone">display: none (not rendered)</div>17</div>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.