Table of Contents
Button
Blocks that can still be broken
Conversion on inline elements
Sub-elements that are not broken in the middle of themselves
How to quickly arrange lists horizontally
Centered list
Home Web Front-end CSS Tutorial When do you use inline-block?

When do you use inline-block?

Apr 04, 2025 am 09:34 AM

When do you use inline-block?

display: inline-block is a classic CSS attribute value! It's not a new feature, and there's no need to worry about browser compatibility. Many developers use it intuitively. But let's take a closer look. In what circumstances does it work? When to choose it compared to other similar options?

Button

The most common answer I hear is: I always use it for buttons .

Ultimately, I think it makes sense, but it leads to a slight misunderstanding I think. The idea is that the element that wants to look like a button (may be used<a></a> ,<button></button> or other elements created) can be arranged inline like nature, but can have margin and padding. This is the part of the misunderstanding: display: inline; elements can still have margin and padding, and their behavior may be consistent with what you expect.

The tricky thing is:

  • The block direction margin of the inline element will be completely ignored
  • padding of inline elements does not affect the height of the text line

So while the style of the button itself is set fairly well, it may not be the case with the parent element and the surrounding text. Here is a demonstration:

Things get worse when the inline button starts wrapping:

So, I think using inline-block on the button is very reasonable. but……

Don't forget inline-flex and inline-grid

With display: inline-flex and inline-grid values, you will get the same good behavior as inline-block , but elements (usually buttons) can benefit from a more powerful inline layout system.

Take a button with an icon as an example, as shown below:

 <a data-line="" href="https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15b" html="">.button svg {
  vertical-align: middle;
}</a>
Copy after login

This can never be completely correct...

But using inline-flex can easily solve this problem:

 .button {
  display: inline-flex;
  align-items: center;
}
Copy after login

With inline-flex or inline-grid you can have all the features of a flexbox or grid layout system in blocks in an inline orientation layout.

Blocks that can still be broken

The inline block element will respect the width. This is another difference between them and purely inline elements. People used to¹ to build a column layout system using inline blocks because it basically does what the float can do without worrying about clearing the float², allowing people to take advantage of a more elegant line breaking method than the float.

The idea that inline block elements are represented as columns that can be broken (even narrowed down to 1 column) still exists today because it is a trick that can be used in HTML emails to allow multi-column layouts to collapse into single columns on small screens without the need for media queries (some email clients do not support media queries).

Dan's example.

Conversion on inline elements

Inline elements cannot be converted. So if you need a conversion, you need to use inline-block .

Sub-elements that are not broken in the middle of themselves

You can use CSS columns on paragraph text, and you don't have to care whether any given paragraph is disconnected across columns. But sometimes CSS columns are used for blocks, which will be awkward. Assume that these blocks have their own background and padding. Disconnecting is very strange visually.

This is a weird trick I can't say I understand 100% but if you use display: inline-block; on these boxes (and probably use width: 100%; to make sure they keep the column width), then they won't break and the padding will be preserved.

How to quickly arrange lists horizontally

This is another very popular answer to my original tweet. List elements stack list items vertically, just like block-level elements. They are not actually blocks. They are display: list-item; , which is actually important here, as we will see. A popular use case is "When I want to arrange a list horizontally" .

So you have a list...

Copy after login
  • Three
  • Small
  • pig

You want to change it to a line, you can...

 li {
  display: inline-block;
}
Copy after login

nailed it.

I quickly tried it out in VoiceOver, and the inline block list still declares elements as lists, but there is no bullets read aloud, which makes sense because they don't exist. This is the problem of changing the way the list items themselves are displayed to non- list-item : they lose their, ahem, list-item features.

Another way is to set the parent element as the flexbox container...

 ul {
  display: flex;
}
Copy after login

...This implements horizontal lines (flexbox default), but retains bullets because you don't change how the list items themselves appear. If you want to delete it manually, it's up to you.

Centered list

Speaking of lists, Jeff Starr just wrote a blog about lists in centered text, which can also become awkward. The embarrassment is that the text inside the list item can be centered, but the list item itself is still full width, creating a situation where the bullet remains aligned to the left.

Jeff's solution is to set the entire list as an inline block. This makes the list width as wide as the natural width of its content, allowing the bullet to leave the left edge and move with the centered content. As long as there are block-level elements in front and behind, this is a good solution.

As an alternative, if the goal is to reduce the width of the list to the width of the content, you can also achieve this without preventing the list from becoming a block-level element:

 ul {
  width: max-content;
  margin: 0 auto;
  text-align: left;
}
Copy after login
  1. There is another tricky problem with inline blocks. Like inline elements, any space between them will actually render as a space. So if there are any spaces between them, two 50% wide inline-block elements will not be displayed in one line. The good news is that there are tricks to solve this problem.
  2. I say "in the past" because, if you were to make a column system today, you would almost certainly use flexbox or grid - or not build "system" at all, because just using these syntax largely negates the need for the system.

The above is the detailed content of When do you use inline-block?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

How to Use CSS Grid for Sticky Headers and Footers How to Use CSS Grid for Sticky Headers and Footers Apr 02, 2025 pm 06:29 PM

CSS Grid is a collection of properties designed to make layout easier than it’s ever been. Like anything, there&#039;s a bit of a learning curve, but Grid is

See all articles