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
Make an animation of the eight planets in the sol...
This article example shares the specific code of ...
Now that we have finished the transform course, l...
Table of contents 1. About JavaScript 2. JavaScri...
student.xml <?xml version="1.0" enco...
Preface This article summarizes some common MySQL...
1. Interconnection between Docker containers Dock...
Table of contents Find the problem 1. How to remo...
How to declare a cursor in mysql: 1. Declare vari...
I just joined a new company recently. After getti...
Table of contents 1. v-text text rendering instru...
Viewing and Setting SQL Mode in MySQL MySQL can r...
How to set a limit on the number of visits to a c...
Introduction to vi/vim They are both multi-mode e...
Nginx Rewrite usage scenarios 1. URL address jump...