Summary of Several Methods for Implementing Vertical Centering with CSS

Summary of Several Methods for Implementing Vertical Centering with CSS

In the front-end layout process, it is relatively simple to achieve horizontal centering, which can usually be achieved through margin:0 auto; and parent element text-align: center;. But it is not so easy to achieve vertical centering. Below I will share with you several methods I use to achieve vertical centering in my work.

1. Line-height is equal to height/only set line-height

This method is more suitable for centering text. The core is to set the line-height equal to the height of the box that wraps it, or to set the line-height without setting the height. This method is suitable for scenes where text is centered and the height is fixed. It is convenient and useful to use.

//html
<div class="middle">555</div>
 
//css
.middle{
  height: 50px;
  line-height: 50px;
  background: red;
} 

It is worth noting that

If it is an inline element, since it has no height, you need to convert the inline element into an inline block or block element first.

2. vertical-align: middle

This kind of element centering requires that the parent element has a line height equal to its own height, and this element is an inline block element. Only when all three conditions are met can vertical centering be achieved. The code is as follows:

//html
<div class="main">
   <div class="middle"></div>
</div>

//css
.main {
  width: 200px;
  height: 300px;
  line-height: 300px;
  background: #dddddd;
}
.middle{
  background: red;
  width: 200px;
  height: 50px;
  display: inline-block; //or display: inline-table;
  vertical-align: middle;
} 

It should be noted that this method requires a fixed line height, and the centering achieved is actually approximate centering, not true centering.

3. Absolute positioning plus negative margins

The core of this method is to first set the element to be centered to absolute positioning, and then set its top: 50%; plus margin-top equal to the negative half of its own height to achieve centering. The advantage is that it is easier to implement, the height of the parent element can be a percentage, and there is no need to set the line height. The code is as follows:

//html
<div class="main">
  <div class="middle"></div>
</div>
  
//css
.main {
  width: 60px;
  height: 10%;
  background: #dddddd;
  position: relative; //parent element is set to relative positioning}
.middle{
  position: absolute; //Set to absolute positioning top: 50%; //top value is 50%
  margin-top: -25%; //Set margin-top to half the element height width: 60px;
  height: 50%;
  background: red;
}

4. Absolute positioning plus margin:auto

First the code:

//html
<div class="main">
  <div class="middle"></div>
</div>
  
//css
.main {
  width: 60px;
  height: 10%;
  background: #dddddd;
  position: relative; //parent element is set to relative positioning}
.middle{
  width: 30%;
  height: 50%;
  position: absolute; //Set to absolute positioning top: 0;
  bottom: 0; //Set top and bottom to 0.
  left: 0; //If left and right are also set to 0, both horizontal and vertical centering can be achieved right: 0;
  margin:auto;
  background: red;
}

The advantage of this method is that it can not only achieve vertical centering, but also horizontal centering. The disadvantage is that when the network or performance is poor, the box may not be directly fixed in place, resulting in a poor user experience.

5. Flex layout

Flex layout can easily achieve vertical and horizontal centering, which has many advantages and is widely used on mobile terminals. The disadvantage is that it has poor browser compatibility. The code is as follows:

//html
<div class="main">
  <div class="middle"></div>
</div>
 
//css
.main {
  width: 60px;
  height: 10%;
  background: #dddddd;
  display: flex; //Set to flex
  justify-content: center; // horizontal center align-items: center; // vertical center}
.middle{
  width: 30%;
  height: 50%;
  background: red;
} 

Summarize

The above is a summary of several methods of achieving vertical centering with CSS that I introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time!

<<:  Analysis of the Neglected DOCTYPE Description

>>:  Non-standard implementation code for MySQL UPDATE statement

Recommend

Conditional comment style writing method and sample code

As front-end engineers, IE must be familiar to us...

Several methods to solve the problem of MySQL fuzzy query index failure

When we use the like % wildcard, we often encount...

How to get the intersection/difference/union of two sets in mysql

Common scenarios of MySQL: getting the intersecti...

MySQL SQL statement to find duplicate data based on one or more fields

SQL finds all duplicate records in a table 1. The...

How to implement remote access control in Centos 7.4

1. SSH remote management SSH is a secure channel ...

Linux editing start, stop and restart springboot jar package script example

Preface In the springboot configuration file, the...

Vue.js manages the encapsulation of background table components

Table of contents Problem Analysis Why encapsulat...

A brief discussion on common operations of MySQL in cmd and python

Environment configuration 1: Install MySQL and ad...

Vue Router vue-router detailed explanation guide

Chinese documentation: https://router.vuejs.org/z...

How to use Linux commands in IDEA

Compared with Windows system, Linux system provid...

Learn v-model and its modifiers in one article

Table of contents Preface Modifiers of v-model: l...

Vue3 (III) Website Homepage Layout Development

Table of contents 1. Introduction 2. Actual Cases...

Problems with using multiple single quotes and triple quotes in MySQL concat

When dynamically concatenating strings, we often ...

11 Linux KDE applications you didn't know about

KDE Abbreviation for Kool Desktop Environment. A ...

A useful mobile scrolling plugin BetterScroll

Table of contents Make scrolling smoother BetterS...