What is display?
The CSS display property controls how an element is laid out on the page โ how much space it takes, how it flows with other elements, and whether it respects width/height settings.
Every HTML element has a default display value. Block elements like <div>, <p>, and <h1> stack vertically and take full width. Inline elements like <span>, <a>, and <strong> flow horizontally and only take the width they need.
Understanding display values is fundamental to CSS layout. It determines whether an element is a block, inline, or something in between โ and affects everything from navigation bars to cards and grids.
display property is one of the most important CSS properties for controlling layout. Master it, and you control how content flows on your page.
display: block
Block elements take the full available width and stack vertically, one below the other. They respect width, height, margin, and padding โ even if they're not explicitly set.
Default block elements: <div>, <p>, <h1> through <h6>, <ul>, <ol>, <section>, <article>, <header>, <footer>.
Use display: block when you want full-width elements that stack vertically. This is the default for most container elements.
display: inline
Inline elements flow horizontally, taking only the width they need. They ignore width, height, margin-top, and margin-bottom settings.
Default inline elements: <span>, <a>, <strong>, <em>, <img>, <button>.
Inline elements are useful for small pieces of text or buttons within a paragraph. But they have limitations โ you can't control their width or vertical margin.
display: inline-block
Inline-block is the best of both worlds. Elements flow horizontally like inline elements, but respect width, height, margin, and padding like block elements.
Inline-block is ideal for buttons, navigation links, and small cards that should sit side-by-side but with full control over spacing and sizing.
Three ways to hide elements
In CSS, you have three main ways to hide an element: display: none, visibility: hidden, and opacity: 0. Each behaves differently.
display: none
Removes the element from the document flow entirely. It takes up no space, and surrounding elements collapse into the freed space.
visibility: hidden
Hides the element visually, but keeps its space in the layout. Surrounding elements don't move.
opacity: 0
Makes the element transparent (invisible), but it still occupies space and can be clicked. Useful for animations and keeping elements interactive while hidden.
display: none= element gone, space freed (best for removing elements)visibility: hidden= invisible but space kept (use when layout must stay stable)opacity: 0= transparent and clickable (use for animations and hover effects)
Try it yourself: Block vs Inline vs Inline-Block
Edit the code below to see how different display values affect layout. Notice how block takes full width, inline flows horizontally, and inline-block combines both.
Try it yourself: Three ways to hide
Edit the code below to see how display:none, visibility:hidden, and opacity:0 differ. Notice how neighboring elements react differently depending on which property you use.
When to use each display value
- display: block โ Use for full-width elements that stack (containers, sections, paragraphs).
- display: inline โ Use for text-like elements within a paragraph (bold, italics, links). Rarely explicitly set since it's the default for <span>, <a>, etc.
- display: inline-block โ Use for buttons, navigation links, and small cards that should sit side-by-side with spacing control.
- display: none โ Use to completely remove elements from the layout (mobile menu toggles, hidden content).
- visibility: hidden โ Use when you need to hide an element but preserve its space in the layout.
- opacity: 0 โ Use for animations, hover effects, and when you need the element to remain clickable while invisible.
display: flex or display: grid instead of inline-block. They're more powerful and flexible. But inline-block is simpler for small, straightforward layouts like navigation bars.
Task: Build a navigation bar with these requirements:
- Use inline-block on navigation links
- Add padding (12px 20px) to each link
- Set a background color and text color
- Add a hover effect that changes the background
- Create a "show/hide" demo: add a button that toggles an element between visible and hidden using either display:none or visibility:hidden
Use the Try It Yourself editor above to build it. Think about which display property to use and why.
Hint: Start with a <nav> container and <a> tags inside. Set each <a> to display: inline-block. For the toggle, create a <button> that calls a JavaScript function to add/remove a class that sets display: none or visibility: hidden.
Key takeaways
display: blockโ Full width, stacks vertically, respects sizing.display: inlineโ Flows horizontally, ignores width/height/vertical margin.display: inline-blockโ Flows horizontally but respects sizing. Perfect for buttons and navigation links.- Three ways to hide:
display: none(removes space),visibility: hidden(keeps space),opacity: 0(transparent and clickable). - Choose the right display value based on your layout needs.
Test your knowledge
You just mastered CSS display.
Imagine what you'll build next.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ