1. Home
css

CSS Tutorial: A Comprehensive Guide

Master CSSS with our in-depth tutorials. From beginner to advanced topics, explore essential concepts and upskill yourself.

  • 23
  • 4
right-top-arrow
11

CSS Inline Block

Updated on 19/09/2024346 Views

The CSS inline block is a versatile tool that allows elements to exhibit characteristics of both inline and block-level elements. It enables elements to be laid out horizontally while still respecting width, height, margins, and padding. In this article, we'll explore when and how to use inline blocks effectively.

Understanding Inline, Block, and Inline Block

In CSS, the display property determines how an element is rendered and how it interacts with other elements on the page. The three main display values are inline, block, and inline block. Let's explore each of them in detail.

Inline Elements

Inline elements are displayed in line with the flow of text. They do not start on a new line and only take up as much width as necessary to accommodate their content. Here are some key characteristics of inline elements:

  1. Flow: Inline elements flow alongside the text content and other inline elements.
  2. Width and Height: Inline elements ignore the width and height properties. Their dimensions are determined by their content.
  3. Margins and Padding: Inline elements respect left and right margins and padding but ignore top and bottom margins. Vertical margins and padding do not affect the height of the line.
  4. Line Breaks: Inline elements do not cause line breaks. They continue on the same line until they reach the end of the container or a line break is manually inserted.

Examples of inline elements include <span>, <a>, <strong>, <em>, and <img>.

Here's an example of inline elements:

Html code

<p>This is a paragraph with <span>inline</span> elements.</p>

Block Elements

Block elements start on a new line and take up the full width available within their container. They create a distinct block-level box. Here are some key characteristics of block elements:

  1. Width: Block elements have a default width of 100%, meaning they expand to fill the available horizontal space within their container.
  2. Height: The height of a block element is determined by its content, unless explicitly set using the height property.
  3. Margins and Padding: Block elements respect all four sides of margins and padding. Vertical margins between block elements collapse.
  4. Line Breaks: Block elements always start on a new line and create a line break after them.

Examples of block elements include <div>, <p>, <h1> to <h6>, <ul>, <ol>, <li>, and <table>.

Here's an example of block elements:

Html Code

<div>This is a block element.</div>

<p>This is another block element.</p>

Inline Block Elements

CSS Inline block elements combine the characteristics of both inline and block elements. They are displayed inline but behave like block-level elements. Here are some key characteristics of inline block elements:

  1. Flow: Inline block elements are displayed inline and flow alongside the text content and other inline or inline block elements.
  2. Width and Height: Inline block elements can have a specified width and height. They respect the width and height properties.
  3. Margins and Padding: Inline block elements respect all four sides of margins and padding. Vertical margins and padding affect the height of the line.
  4. Line Breaks: Inline block elements do not create line breaks. They continue on the same line unless there is not enough space, in which case they wrap to the next line.

Inline block elements are useful for creating layouts where elements need to be displayed inline but also require block-level properties.

Here's an example of inline block elements:

Html Code

<div class="container">

  <div class="inline block">inline block element 1</div>

  <div class="inline block">inline block element 2</div>

  <div class="inline block">inline block element 3</div>

</div>

CSS code

.inline block {

  display: inline block;

  width: 100px;

  height: 100px;

  margin: 10px;

  padding: 10px;

  background-color: #f0f0f0;

}

In this example, the .inline block elements are displayed inline but have a specified width, height, margin, and padding. They will wrap to the next line if there is not enough horizontal space.

Converting Display Values

You can modify the default display behavior of elements using the display property in CSS. For example, you can convert an inline element to a block element or vice versa.

CSS code

span {

  display: block;

}

div {

  display: inline;

}

In the above example, the <span> element, which is normally inline, is converted to a block element, while the <div> element, which is normally block, is converted to an inline element.

Considerations

When working with different display values, keep the following considerations in mind:

  1. Inline Elements: Inline elements should be used for small, non-breaking content such as text formatting, links, or images within a paragraph.
  2. Block Elements: Block elements are suitable for larger, standalone sections of content such as paragraphs, headings, lists, or sections of a page.
  3. inline block Elements: Inline block elements are useful for creating layouts where elements need to be displayed inline but also require block-level properties, such as navigation menus or card-based layouts.
  4. Responsiveness: Consider how the display values affect the responsiveness of your layout. Block elements stack vertically on small screens, while inline and inline block elements may require additional styling to ensure proper wrapping and alignment.
  5. Accessibility: Ensure that your use of display values does not interfere with the accessibility of your content. Properly structure your HTML and use appropriate elements for their intended purpose.

Here's an example demonstrating the differences:

CSS code

.inline {

  display: inline;

}

.block {

  display: block;

}

.inline block {

  display: inline block;

}

When to Use CSS Inline Block?

Horizontal Navigation

One common use case for inline block is creating horizontal navigation menus. By setting display: inline block on the navigation items, they will be laid out horizontally while allowing you to control their dimensions and spacing.

Html code

<ul class="nav">

  <li><a href="#">Home</a></li>

  <li><a href="#">About</a></li>

  <li><a href="#">Services</a></li>

  <li><a href="#">Contact</a></li>

</ul>

CSS code

.nav li {

  display: inline block;

  margin-right: 10px;

}

Buttons with Icons

When creating buttons with icons, inline block can help align the icon and text perfectly. Instead of using vertical-align: middle on the icon, you can use inline-flex or inline-grid on the button itself for better control over alignment.

Html code

<button class="btn">

  <i class="icon"></i>

  Button Text

</button>

CSS Code

.btn {

  display: inline-flex;

  align-items: center;

}

Multi-Column Layouts

Inline block can be used to create multi-column layouts that wrap and collapse on smaller screens without the need for media queries. This technique is commonly used in HTML emails where media query support is limited.

Html code

<div class="column">Column 1</div>

<div class="column">Column 2</div>

<div class="column">Column 3</div>

CSS code

.column {

  display: inline block;

  width: 33.33%;

}

Centering Lists

When centering a list within a container, setting display: inline block on the list itself can prevent bullets from aligning to the left edge. This allows the list to shrink to the width of its content and center properly.

Html code

<div class="centered">

  <ul>

    <li>Item 1</li>

    <li>Item 2</li>

    <li>Item 3</li>

  </ul>

</div>

CSS Code

.centered {

  text-align: center;

}

.centered ul {

  display: inline block;

  text-align: left;

}

Considerations and Alternatives

While CSS inline block is useful in many scenarios, it's important to consider some potential drawbacks and alternatives:

  1. Whitespace: inline block elements are sensitive to whitespace between them, which can cause unwanted gaps. This can be addressed by removing whitespace in the HTML or using font-size: 0 on the parent element.
  2. Vertical Alignment: By default, inline block elements align to the baseline. To achieve perfect centering, you may need to use vertical-align: middle or consider using flexbox or grid instead.
  3. Flexbox and Grid: In many cases, flexbox and grid provide more powerful and flexible layout options. Consider using display: flex or display: grid for more complex layouts or when precise control over alignment and spacing is required.

Conclusion

CSS display inline block is a versatile CSS property that combines the characteristics of inline and block elements. It's particularly useful for creating horizontal navigation, aligning buttons with icons, building multi-column layouts, and centering lists. However, it's important to be aware of potential whitespace issues and consider alternative layout techniques like flexbox and grid when appropriate.

By understanding when and how to use CSS inline block effectively, you can create well-structured and visually appealing web pages. Experiment with different scenarios and explore alternative approaches to find the best solution for your specific layout needs.

Did you find this article helpful? Let us know in the comments below! For more CSS tips and tricks, be sure to check out our other articles on CSS Flexbox and CSS Grid.

Frequently Asked Questions 

1. What is the display inline block in CSS?

CSS display inline block is a property that allows elements to exhibit characteristics of both inline and block-level elements. inline block elements are laid out inline (horizontally) but can have a specified width, height, and respect margins and padding.

2. When should I use CSS inline block?

You should use CSS inline block in the following scenarios:

  • Creating horizontal navigation menus
  • Aligning buttons with icons
  • Building multi-column layouts that wrap and collapse on smaller screens
  • Centering lists within a container

3. What is the difference between inline and inline block?

Let's check inline vs inline block

Inline 

Inline block

Inline elements do not begin on a new line and only take up as much width as necessary. 

Block-level elements begin on a new line and take up the full width available.

They ignore width and height properties and do not respect top and bottom margins.

They respect width, height, margins, and padding.

 4. Why is inline CSS used?

Inline CSS applies styles directly to an HTML element with the help of the style attribute. It is useful for quickly testing or applying specific styles to individual elements. However, it is generally recommended to use external or internal stylesheets for better maintainability and separation of concerns.

5. What is an example of inline block?

Here's an example of using inline block to create a horizontal navigation menu:

Html code

<ul class="nav">

  <li><a href="#">Home</a></li>

  <li><a href="#">About</a></li>

  <li><a href="#">Services</a></li>

  <li><a href="#">Contact</a></li>

</ul>

CSS code

.nav li {

  display: inline block;

  margin-right: 10px;

}

6. Is inline block in CSS faster?

Display inline block in CSS can be marginally faster in terms of initial page load since the styles are directly applied to the elements without the need for additional HTTP requests. However, the performance gain is usually negligible, and the drawbacks of using inline CSS (lack of reusability, difficult maintenance) often outweigh any potential speed benefits.

7. Is an image an inline block?

By default, images (<img>) are inline block elements. They are laid out inline but can have a specified width and height. However, you can change the display property of an image to block or inline if needed.

8. Is inline block in CSS good or bad?

Inline CSS is generally considered inadequate due to the following reasons:

  • It mixes presentation with structure, reducing the maintainability and readability of the code.
  • It makes it difficult to reuse styles across multiple elements or pages.
  • It can lead to repetition and bloated HTML code.

It's recommended to use external or internal stylesheets to keep your styles separate from your HTML structure. However, inline CSS can be useful in specific situations, such as email templates or quick testing.

mukesh

Mukesh

Working with upGrad as a Senior Engineering Manager with more than 10+ years of experience in Software Development and Product Management.

Talk to Career Expert
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...