Preface As we all know, "How to vertically center an element in CSS?" is already a common question. There are many solutions to this problem. These solutions also have their own applicable scenarios and advantages and disadvantages, which are roughly as follows:
So today we will understand the principle of one of the effective but less commonly used solutions, which is: the pseudo-element <style type="text/css"> .parent { display: inline-block; width: 300px; height: 300px; font-size: 0; background: #80848f; text-align: center; } .parent:before { display: inline-block; width: 20px; height: 100%; content: ''; background: #ff9900; vertical-align: middle; } .child { display: inline-block; width: 50px; height: 50px; background: #19be6b; vertical-align: middle; } </style> <div class="parent"> <div class="child">child</div> </div> The result of running the above code is as follows: I believe everyone is already familiar with the code, but do you really understand the principles behind it? Next, let's take a look at how it achieves vertical centering step by step. analyze First of all, we need to know a key point, that is: the position of the parent element's baseline can be changed, it is not fixed, it is very important to remember this. Next, let's simplify the code and remove the key parts. <style type="text/css"> .parent { display: inline-block; width: 300px; height: 300px; /* font-size: 0; */ background: #80848f; text-align: center; } .parent:before { display: inline-block; width: 20px; height: 100%; content: ''; background: #ff9900; /* vertical-align: middle; */ } .child { display: inline-block; width: 50px; height: 50px; background: #19be6b; /* vertical-align: middle; */ } </style> <div class="parent"> <div class="child">child</div> </div> After we comment out It is not difficult to see from the figure that middle: Aligns the middle of the element with the baseline of the parent element plus half of the parent element's So, compare our example:
Summarize In fact, the principle of this vertical centering method mainly has the following points:
This is the end of this article about alternative implementations of CSS vertical centering. For more relevant CSS vertical centering content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope that everyone will support 123WORDPRESS.COM in the future! |
<<: Detailed explanation of Nginx version smooth upgrade solution
>>: Four ways to create objects in JS
Click here to return to the 123WORDPRESS.COM HTML ...
We know that when using HTML on NetEase Blog, we ...
Table of contents What is Rract? background React...
Recently, when I was learning Django, I needed to...
1. Download MySQL Archive (decompressed version) ...
1. All tags must have a corresponding end tag Prev...
The company project was developed in Java and the...
Recently, students from the User Experience Team o...
1. Enter the directory where your project war is ...
Table of contents Preface Problem: Large file cop...
Author: Guan Changlong is a DBA in the Delivery S...
The layout of text has some formatting requiremen...
Solve the problem of Chinese garbled characters i...
1. Table structure TABLE person id name 1 you 2 Y...
Configure multiple servers in nginx.conf: When pr...