Solve the problem that line-height=height element height but text is not vertically centered

Solve the problem that line-height=height element height but text is not vertically centered

Let me first explain why the text is not vertically centered when line-height is equal to the element height. In fact, when line-height is equal to the element height, the text is not really centered, but looks centered. When the difference between the element height and font-size is large, this is more and more obvious. Here you can refer to What is a baseline?

The green line in the image below is the baseline: line-height refers to the distance between two lines of text [baseline]

Solution 1:

Combining line height, alignment and pseudo elements

.text{
  width: 16px; 
  height: 16px;
  font-size: 10px;
  text-align: center;
}
.text::after{
  content: ' ';
  display: inline-block;
  width: 0;
  height: 100%;
  vertical-align: middle;
  margin-top: 1px;
}

Solution 2:

Use the CSS3 scale property to set all values ​​to double the size and then double the size.

.text{
  width: 32px; 
  height: 32px;
  line-height: 32px;
  font-size: 20px;
  text-align: center;
  transform: scale(0.5);
}

This is the end of this article about solving the problem of line-height=height element height but text is not vertically centered. For more relevant content about line-height=height element height, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  CSS code abbreviation div+css layout code abbreviation specification

>>:  Introduction to the application of HTML tags superscript sup and subscript sub

Recommend

How to configure /var/log/messages in Ubuntu system log

1. Problem Description Today I need to check the ...

Analyze the usage and principles of Vue's provide and inject

First, let's talk about why we use provide/in...

Vue implements image drag and drop function

This article example shares the specific code of ...

Vue integrates Tencent Map to implement API (with DEMO)

Table of contents Writing Background Project Desc...

Teach you how to implement Vue3 Reactivity

Table of contents Preface start A little thought ...

Detailed steps to install mysql5.7.18 on Mac

1. Tools We need two tools now: MySQL server (mys...

Let's talk about MySQL joint query in detail

Table of contents Union query 1. Query the ID and...

Mybatis mysql delete in operation can only delete the first data method

Bugs As shown in the figure, I started to copy th...

MySQL trigger detailed explanation and simple example

MySQL trigger simple example grammar CREATE TRIGG...

Sequence implementation method based on MySQL

The team replaced the new frame. All new business...

Vue3 manual encapsulation pop-up box component message method

This article shares the specific code of Vue3 man...

Detailed steps to install mysql in Win

This article shares the detailed steps of install...

Detailed explanation of how to gracefully delete a large table in MySQL

Preface To delete a table, the command that comes...

Analyze the problem of pulling down the Oracle 11g image configuration in Docker

1. Pull the image docker pull registry.cn-hangzho...

MySQL EXPLAIN statement usage examples

Table of contents 1. Usage 2. Output results 1.id...