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
The effect is as follows: The code is as follows ...
Detailed explanation of the usage of DECIMAL in M...
Table of contents Preface 1. Download MySQL 8.0.2...
1. Confirm whether MySQL has been installed. You ...
Adding background image control to a component re...
1. Introduction This article will show you how to...
What does Ctrl+c, Ctrl+d, Ctrl+z mean in Linux? C...
This article shares the specific code of Vue to s...
SQL finds all duplicate records in a table 1. The...
Recently, I ran a spark streaming program in a ha...
1.docker search mysql查看mysql版本 2. docker pull mys...
Introduction Recently, I needed to make a barrage...
Table of contents Placeholder replacement Console...
Table of contents 1. Unzip 2. Create a data folde...
1. HTML tags with attributes XML/HTML CodeCopy co...