Inline and Block elements

Syed Aquib AliSyed Aquib Ali
3 min read

In HTML and CSS, elements are categorized into two main types based on their display behavior: inline elements and block elements. Understanding the differences between these types is crucial for effective web design and layout.

Inline Elements

Inline elements do not start on a new line and only take up as much width as necessary. They flow within the text and other inline elements, maintaining the normal flow of content. Common inline elements include <span>, <a>, <img>, and <strong>.

Behavior of Inline Elements:

  • Do not start on a new line.

  • Only occupy as much width as necessary.

  • Can contain other inline elements or text, but not block elements.

  • The width and height properties have no effect unless display is changed to a block or inline-block.

<a href="#">This is a link</a>
<span>This is a span element</span>
<img src="image.jpg" alt="Image">
<strong>This text is bold</strong>
a {
    color: blue;
    text-decoration: none;
}

span {
    background-color: yellow;
}

img {
    border: 1px solid black;
}

Block Elements

Block elements start on a new line and take up the full width available by default. They stack vertically, with each block element beginning on a new line. Common block elements include <div>, <p>, <h1> to <h6>, <ul>, <ol>, and <li>.

Behavior of Block Elements:

  • Always start on a new line.

  • Occupy the full width of their container (parent element), if none then takes from body.

  • Can contain other block elements, inline elements, and text.

  • The width and height etc. properties can be set and have an effect.

<div>This is a div element</div>
<p>This is a paragraph</p>
<h1>This is a heading</h1>
<ul>
    <li>List item 1</li>
    <li>List item 2</li>
</ul>
div {
    background-color: lightblue;
    padding: 10px;
    margin-bottom: 10px;
}

p {
    font-size: 16px;
    line-height: 1.5;
}

h1 {
    font-size: 24px;
    color: darkblue;
}

Comparison between Inline and Block Elements

Display Behavior:

  • Inline Elements: Do not start on a new line, flow with text, and take up only as much width as necessary.

  • Block Elements: Start on a new line, take up the full width of the container, and stack vertically.

Containment:

  • Inline Elements: Can contain text and other inline elements but cannot contain block elements.

  • Block Elements: Can contain text, inline elements, and other block elements.

CSS Properties:

  • Inline Elements: width and height etc. properties do not apply unless changed to display: block or display: inline-block.

  • Block Elements: width and height etc. properties can be set and will apply.

Transforming Elements

You can change the default display behavior of elements using the display property in CSS.

Making Inline Elements Behave Like Block Elements:

a {
    display: block;
    padding: 10px;
    background-color: lightgreen;
}

Making Block Elements Behave Like Inline Elements:

div {
    display: inline;
    background-color: lightpink;
}

Using inline-block:

The display: inline-block property combines the characteristics of both inline and block elements. Inline-block elements flow inline with other content but can have width and height etc. set.

div {
    display: inline-block;
    width: 200px;
    height: 100px;
    background-color: lightcoral;
    margin: 10px;
}

Understanding the differences between inline and block elements is essential for web development. Inline elements flow within the text, while block elements start on a new line and take up the full width. The display property allows you to change the default behavior of these elements, providing flexibility in layout and design.

0
Subscribe to my newsletter

Read articles from Syed Aquib Ali directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Syed Aquib Ali
Syed Aquib Ali

I am a MERN stack developer who has learnt everything yet trying to polish his skills 🥂, I love backend more than frontend.