CSS Display Property Explained with Block, Inline, and Inline-Block

Hi everyone!

In this section, we are going to explore one of the most fundamental and powerful CSS tools — the display property. If we’ve ever worked on web layouts, chances are we’ve already encountered it, even if we didn’t realize its full potential. Now, it’s time to dig deeper and understand how this property shapes the layout of every webpage.

By learning how to use the display property properly, we unlock the ability to control how elements behave in a layout — whether we want them to appear in a row, stack vertically, or hide them from view altogether. The display property is what helps us create flexible, clean, and responsive layouts.

Block and Inline Elements in HTML

Before diving into CSS, let’s start with how elements behave by default in HTML. For instance, when using a <p> tag, we’ll notice that it stretches all the way across the container. That’s because it’s a block-level element.

Now let’s say we want a specific word in that paragraph to have a different style, without breaking the line. That’s where inline elements shine. Here’s a quick example:

<p>Hello, <span>Beautiful</span> World</p>

In this case, the <span> tag doesn’t break the flow — the word “Beautiful” stays on the same line. Inline elements stay in line with surrounding content and only occupy the space their content requires.

Recap:

  • Block-level elements (like <div>, <p>, <h1>) always start on a new line and take up the full width.

  • Inline elements (like <span>, <strong>, <a>) do not start on a new line and stay within the flow of text.

Three Essential CSS Display Values

CSS offers many display values, but these three are foundational:

  1. block

  2. inline

  3. inline-block

Let’s break them down.

display: block

A block-level element:

  • Starts on a new line

  • Stretches across the entire width of the parent container

  • Can be styled using properties like width, height, margin, and padding

Common examples include <div>, <section>, <article>, and <header>.

display: inline

Inline elements:

  • Flow alongside other inline elements

  • Ignore properties like width and height

  • Only occupy space equal to their content

Elements like <span>, <strong>, and <a> are inline by default.

display: inline-block

This is the best of both worlds:

  • Appears inline with other elements

  • Supports width, height, margin, and padding

It’s perfect for items like buttons, menus, and navigation links that should sit side-by-side but still need custom dimensions.

Practical Example — Changing Layouts with Display

Let’s say we want to create three square boxes that appear side by side. Here’s the CSS:

.box {
  display: inline-block;
  width: 200px;
  height: 200px;
  background-color: lightblue;
  margin: 10px;
}

If the screen is wide enough, the boxes appear in a row. Want them to stack vertically instead? Just change the display value:

.box {
  display: block;
  width: 100%;
}

This small change has a big impact on layout — and that’s exactly the kind of power CSS gives us.

Hiding Elements with CSS

Sometimes we need to temporarily hide elements. That’s where display: none comes in:

.hidden {
  display: none;
}

Unlike visibility: hidden, which only hides the element but leaves its space, display: none removes it entirely from the layout. This is especially useful for:

  • Toggling menus

  • Form steps

  • Mobile navigation

Try It Yourself — Live Demo Playground

Want to practice this yourself? Check out this interactive example: https://css-display.vercel.app/

You’ll be able to:

  • Compare block vs inline behavior

  • Test inline-block layouts

  • Change display styles and observe the results in real-time

Use developer tools in your browser to explore and experiment.

Also, try the mini-challenge in the 8.0 CSS Display project. Your task:

  1. Make three boxes display side by side

  2. Then stack them vertically

Just by updating one property — the layout changes completely.

Why the Display Property Is So Important

If we’re serious about web design, then mastering the display property is essential. It’s the building block of all layouts. Whether we’re creating a landing page, a portfolio, or an entire web app — understanding display behavior helps us:

  • Fix common layout issues

  • Manage spacing effectively

  • Structure content with clarity

This foundation will also prepare us for more advanced layout systems like Flexbox and Grid, which we’ll explore soon.

So take your time to experiment with different display values. Observe how they behave across devices and browsers. Mastery here means smoother development later.

Keep building and see you in the next section!

Comments

Postingan Populer

Image

Superingan is a Blogger template designed with simplicity and functionality in mind, offering a clean and user-friendly platform for professional websites. This theme is perfect for various website types, including personal blogs, online magazines, news websites, and portfolios. Features: Responsive Design: Your website will look its best across all devices, from desktops and laptops to tablets and smartphones. Customizable Colors: Easily change the template's colors to match your branding or preferences. Custom Headers: Upload your own header image to add a personal touch to your website. Custom Menus: Create and manage your website's navigation menus with ease. Widget Areas: Add widgets to display additional content and features on your website's sidebar. Clean HTML & CSS Code: The template is built with clean and well-structured code, making it easy to learn and customize further. Superingan is an ideal choice for those seeking a professional website with a sim...

Image

Calculate Business Loan Payments, Interest, and Total Cost Easily and Accurately Loan Amount (USD) Annual Interest Rate (%) Loan Term (Years) Extra Monthly Payment (USD) Reset Calculate Hi everyone! If you're looking for extra capital to launch a startup or expand your existing business, it's important to know how much you'll need to repay—monthly and overall. Don’t commit to a loan without understanding the full picture. That’s where our Online Business Loan Calculator comes in handy. This tool is designed for business owners, entrepreneurs, or anyone seeking a full breakdown of a loan. It helps you calculate monthly payments, total interest, repayment period, and overall cost automatically with just a few simple inputs. How to Use the Business Loan Calculator Fill in the following fields based on your situation: Loan Amount (USD): Ent...

Hi everyone! Let’s explore CSS Grid together—a powerful feature that helps us build two-dimensional layouts on the web. In this guide, we’ll start from the basics and work our way up to controlling the size and position of elements with more flexibility. If you're already familiar with Flexbox, Grid will feel like a fun and useful complement! CSS Grid is perfect for building complex layouts like dashboards, photo galleries, or any page that requires a clean structure both horizontally and vertically. Let’s dive in! What is Grid? By adding display: grid to an element, we turn it into a grid container . While Flexbox works in one direction (horizontal or vertical), Grid works in both—columns and rows simultaneously. Grid was introduced to solve layout challenges that couldn’t easily be handled by Flexbox or older techniques like float . Think of Grid as a two-dimensional layout system that gives you full control over element placement and sizing. Why Use Grid? Great for la...