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

Detailed explanation of Nginx's control over access volume

Purpose Understand the Nginx ngx_http_limit_conn_...

Example code for implementing equal width layout in multiple ways using CSS

The equal-width layout described in this article ...

Prevent HTML and JSP pages from being cached and re-fetched from the web server

After the user logs out, if the back button on the...

Solution for importing more data from MySQL into Hive

Original derivative command: bin/sqoop import -co...

Detailed Linux installation tutorial

(Win7 system) VMware virtual machine installation...

Pure CSS to adjust Div height according to adaptive width (percentage)

Under the requirements of today's responsive ...

Steps to install GRUB on Linux server

How to Install GRUB for Linux Server You cannot u...

How to install Solr 8.6.2 in Docker and configure the Chinese word segmenter

1. Environment version Docker version 19.03.12 ce...

PHP scheduled backup MySQL and mysqldump syntax parameters detailed

First, let's introduce several common operati...

Docker Compose installation methods in different environments

1. Online installation Currently only tried the L...

WeChat applet calculator example

WeChat applet calculator example, for your refere...

Markup Languages ​​- What to learn after learning HTML?

Click here to return to the 123WORDPRESS.COM HTML ...