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

HTML+CSS3 code to realize the animation effect of the solar system planets

Make an animation of the eight planets in the sol...

ElementUI implements cascading selector

This article example shares the specific code of ...

CSS3 uses transform to create a moving 2D clock

Now that we have finished the transform course, l...

A brief discussion on macrotasks and microtasks in js

Table of contents 1. About JavaScript 2. JavaScri...

Combining XML and CSS styles

student.xml <?xml version="1.0" enco...

An article to master MySQL index query optimization skills

Preface This article summarizes some common MySQL...

Solution to many line breaks and carriage returns in MySQL data

Table of contents Find the problem 1. How to remo...

How to declare a cursor in mysql

How to declare a cursor in mysql: 1. Declare vari...

Steps for packaging and configuring SVG components in Vue projects

I just joined a new company recently. After getti...

Vue directives v-html and v-text

Table of contents 1. v-text text rendering instru...

Detailed explanation of viewing and setting SQL Mode in MySQL

Viewing and Setting SQL Mode in MySQL MySQL can r...

Limiting the number of short-term accesses to a certain IP based on Nginx

How to set a limit on the number of visits to a c...

Detailed explanation of Nginx Rewrite usage scenarios and code examples

Nginx Rewrite usage scenarios 1. URL address jump...