π Introduction
In web development, one of the most important CSS properties is display
. It tells the browser how an element should appear on the page. The three most commonly used display types are:
Inline elements
Block elements
Inline-block elements
Knowing the difference between them will help you build layouts properly and avoid common design problems. Letβs explore each in detail.
πΉ Inline Elements
Inline elements are those that stay in the same line as the text or other inline elements. They do not force a new line on the page.
Key Points About Inline Elements:
Do not start on a new line β Inline elements stay in the same line unless the line runs out of space.
Take only the space needed β They only use as much width as their content requires.
Limited use of width and height β CSS width and height properties do not usually work with inline elements.
Margins and padding β They respect left and proper margins/padding, but top and bottom margins/padding donβt affect layout.
Examples β <span>
, <a>
, <strong>
, <em>
, <img>
.
Example:
<p>This is <span>inline text</span> inside a sentence.</p>
The word inside <span>
stays inline with the sentence.
π Inline elements are useful for styling parts of text or adding icons and links inside a line of text.
πΉ Block Elements
Block elements are those that start on a new line and take up the full width available.
Key Points About Block Elements:
Always start on a new line β Each block element pushes the next element to a new line.
Take full width by default β Even if the content is small, they stretch to cover the entire available width.
Support width and height β You can easily set width, height, margins, and padding.
Good for layout β They are often used to structure a webpage.
Examples β <div>
, <p>
, <h1>
β<h6>
, <section>
, <article>
.
Example:
<p>This is a block element.</p>
<div>This is another block element.</div>
Each element starts on a new line and takes the full width of the parent container.
π Block elements are best for creating sections, containers, and structured layouts.
πΉ Inline-Block Elements
Inline-block elements are a mix of both inline and block behaviors.
Key Points About Inline-Block Elements:
Do not start on a new line β Like inline elements, they stay in the same line.
Allow width and height β Like block elements, you can set width, height, margins, and padding.
Flexible alignment β They can sit side by side while still being styled like block elements.
Good for menus and layouts β Perfect when you want elements next to each other but still want control over their size.
Examples β Often applied with CSS: display: inline-block;
Example:
<style>
.box {
display: inline-block;
width: 100px;
height: 100px;
background: lightblue;
margin: 5px;
}
</style>
<div class="box">Box 1</div>
<div class="box">Box 2</div>
<div class="box">Box 3</div>
All three boxes appear in the same line, but each has its own width and height.
π Inline-block elements are best for navigation menus, buttons, and side-by-side sections.
π Quick Comparison Table
Feature | Inline | Block | Inline-Block |
---|
Starts new line? | β No | β
Yes | β No |
Width & height work? | β No | β
Yes | β
Yes |
Margin/padding top? | β No | β
Yes | β
Yes |
Margin/padding sides? | β
Yes | β
Yes | β
Yes |
Best for | Text styling, links | Layouts, containers | Menus, buttons, small boxes |
π Summary
In CSS, the display
property defines how elements appear on a webpage. Inline elements stay in the same line and are best for styling text. Block elements start on a new line and take the full width, making them suitable for layout and containers. Inline-block elements combine both behaviors: they sit side by side but allow width and height settings. Understanding these differences is essential for creating clean, responsive, and well-structured web designs.