search
HomeWeb Front-endCSS TutorialUsage analysis of display: inline-block in CSS

Usage analysis of display: inline-block in CSS

Jan 02, 2019 am 10:25 AM
csscss3html5react.jsvue.js

The content of this article is about the usage analysis of display: inline-block in CSS. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Mysterious Gap

We create a navigation list and set its list item to inline-block. The main code is as follows:

<div>
  <div><a>我</a></div>
  <div><a>我</a></div>
  <div><a>我</a></div>
</div>
.nav {
  background: #999;
}
.nav-item{
  display:inline-block; /* 设置为inline-block */
  width: 100px;
  background: #ddd;
}

Effect The picture is as follows:

Usage analysis of display: inline-block in CSS

#We can see from the rendering that there is a small gap between the list items, but we have not set the margin horizontal spacing in the code. So how did this gap arise?

This is because when we write code, entering spaces and newlines will generate whitespace characters. The browser will not ignore whitespace characters, and the browser will automatically merge multiple consecutive whitespace characters into one, so a so-called gap is generated.

For the above example, we entered a carriage return and line feed between the list item elements to facilitate reading, and this gap is the blank character generated by this carriage return and line feed.

Similarly for all inline elements (inline, inline-block), line breaks will produce white space gaps.

How to eliminate whitespace characters

From the above we understand that whitespace characters are normal behavior of browsers. But for some scenes, it is not beautiful, and the size of the gap is not controllable, so we often need to remove this blank gap. Generally speaking, we have two methods to remove the gap caused by this line break: code without line breaks and setting font-size.

Code does not wrap

We learned that line breaks are caused by line breaks, so we can write the list item in the above example into one line, so that the white spaces disappear and the gap is no longer exists. The code is as follows:

<div>
  <div>导航</div>
<div>导航</div>
<div>导航</div>
</div>

However, considering the readability and maintainability of the code, we generally do not recommend writing it in one line.

Set font-size

First of all, we must understand that the whitespace character is ultimately a character. Therefore, we can control the size of the gap generated by setting the font-size attribute. We know that if font-size is set to 0, text characters cannot be displayed, then the blank characters will also be gone, and the gap will be gone.

So there is another solution following this idea: remove this gap by setting the font-size of the parent element to 0, and then reset the font-size of the child element to restore the text of the child element. character.

So the code of this method is as follows:

.nav {
  background: #999;
  font-size: 0; /* 空白字符大小为0 */
}
.nav-item{
  display:inline-block;
  width: 100px;
  font-size: 16px; /* 重置 font-size 为16px*/
  background: #ddd;
}

When using this method, you need to pay special attention to the fact that the font-size of its sub-elements must be reset, otherwise it is easy to fall into the pit (the text will not be displayed) .

Alignment issues

Since inline-block is an inline-level element, the vertical-align attribute also applies to it.

Before we formally explain vertical-align, we need to talk about some basic concepts.

Middle line, baseline, top line, bottom line

Middle line (middle), baseline (baseline), top line (text-top, bottom line (text-bottom)) are several basic elements of text. Line, its corresponding position is as shown below:

  • Baseline (base line): the lower edge of the lowercase English letter x.

  • Middle line: the middle of the lowercase English letter x.

  • Top line (text-top): The top of a content area composed of the font-size size of the parent element

  • Bottom line (text- bottom): The bottom of a content area composed of the font-size size of the parent element

  • vertical-align value

vertical-align only Accepts 8 keywords, a percentage value, or a length value. Next we'll look at how each keyword works on inline elements.

  1. baseline The default element's baseline is aligned with the parent element's baseline.

  2. sub Aligns the element's baseline with the subscript baseline of its parent element.

  3. super Aligns the element's baseline to its parent's superscript - baseline.

  4. text-top Aligns the top of the element with the font top of the parent element.

  5. text-bottom Aligns the bottom of the element with the bottom of the parent element's font.

  6. middle Aligns the middle of the element to the baseline plus half the x-height of the parent element.

  7. top Aligns the top of the element and its descendants to the top of the entire row.

  8. bottom Aligns the bottom of the element and its descendants to the bottom of the entire row.

  9. Aligns the element's baseline to a given length above the baseline of its parent element.

  10. Like the value, the percentage is the percentage of the line-height property

##

The above is the detailed content of Usage analysis of display: inline-block in CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault.. If there is any infringement, please contact admin@php.cn delete
So Many Color LinksSo Many Color LinksApr 13, 2025 am 11:36 AM

There's been a run of tools, articles, and resources about color lately. Please allow me to close a few tabs by rounding them up here for your enjoyment.

How Auto Margins Work in FlexboxHow Auto Margins Work in FlexboxApr 13, 2025 am 11:35 AM

Robin has covered this before, but I've heard some confusion about it in the past few weeks and saw another person take a stab at explaining it, and I wanted

Moving Rainbow UnderlinesMoving Rainbow UnderlinesApr 13, 2025 am 11:27 AM

I absolutely love the design of the Sandwich site. Among many beautiful features are these headlines with rainbow underlines that move as you scroll. It's not

New Year, New Job? Let's Make a Grid-Powered Resume!New Year, New Job? Let's Make a Grid-Powered Resume!Apr 13, 2025 am 11:26 AM

Many popular resume designs are making the most of the available page space by laying sections out in a grid shape. Let’s use CSS Grid to create a layout that

One Way to Break Users Out of the Habit of Reloading Too MuchOne Way to Break Users Out of the Habit of Reloading Too MuchApr 13, 2025 am 11:25 AM

Page reloads are a thing. Sometimes we refresh a page when we think it’s unresponsive, or believe that new content is available. Sometimes we’re just mad at

Domain-Driven Design With ReactDomain-Driven Design With ReactApr 13, 2025 am 11:22 AM

There is very little guidance on how to organize front-end applications in the world of React. (Just move files around until it “feels right,” lol). The truth

Detecting Inactive UsersDetecting Inactive UsersApr 13, 2025 am 11:08 AM

Most of the time you don’t really care about whether a user is actively engaged or temporarily inactive on your application. Inactive, meaning, perhaps they

Wufoo   ZapierWufoo ZapierApr 13, 2025 am 11:02 AM

Wufoo has always been great with integrations. They have integrations with specific apps, like Campaign Monitor, Mailchimp, and Typekit, but they also

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools