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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

How do you use CSS to create text effects, such as text shadows and gradients? How do you use CSS to create text effects, such as text shadows and gradients? Mar 14, 2025 am 11:10 AM

The article discusses using CSS for text effects like shadows and gradients, optimizing them for performance, and enhancing user experience. It also lists resources for beginners.(159 characters)

What the Heck Are npm Commands? What the Heck Are npm Commands? Mar 15, 2025 am 11:36 AM

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.

Creating Your Own Bragdoc With Eleventy Creating Your Own Bragdoc With Eleventy Mar 18, 2025 am 11:23 AM

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles