CSS layout tutorial: How to achieve vertical centering

CSS layout tutorial: How to achieve vertical centering

Preface

I have been summarizing my front-end knowledge recently and have also participated in some interviews. During the interview, I encountered a question requiring vertical centering. Now I will summarize this to consolidate my knowledge.

CSS vertical centering

1. Use line-height to achieve centering. This method is suitable for pure text.

<!-- css -->
<style>
.parents {
  height: 400px;
  line-height: 400px;
  width: 400px;
  border: 1px solid red;
  text-align: center;
}

.child {
  background-color: blue;
  color: #fff;
}
 </style>
</head>

<body>
<!-- html -->
<div class="parents">
   <span class="child">css layout, vertical centering</span>
</div>
</body>

2. By setting the relative positioning of the parent container and the absolute positioning of the child, the label is adaptively centered through margin;

<!-- css -->
<style>
.parents {
  height: 400px;
  width: 400px;
  border: 1px solid red;
  position: relative;
}

.child {
  width: 200px;
  height: 100px;
  line-height: 100px;
  text-align: center;
  color: #fff;
  background-color: blue;
  /* Set the four directions to 0, and then use margin to auto to adaptively center*/
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
}
 </style>
</head>

<body>
<!-- html -->
<div class="parents">
  <span class="child">css layout, vertical centering</span>
</div>
</body>

3. Flexible layout flex The parent is set to display: flex; the child is set to margin auto to achieve adaptive centering;

  <!-- css -->
  <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      display: flex;
    }

    .child {
      width: 200px;
      height: 100px;
      line-height: 100px;
      text-align: center;
      color: #333;
      background-color: yellow;
      margin: auto;
  }
 </style>
</head>

<body>
 <!-- html -->
  <div class="parents">
    <span class="child">css layout, vertical centering</span>
  </div>
</body>

4. The parent is set to relative positioning, the child is set to absolute positioning, and it is achieved through displacement transform;

  <!-- css -->
  <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      position: relative;
    }

    .child {
      width: 200px;
      height: 100px;
      line-height: 100px;
      text-align: center;
      color: #fff;
      background-color: green;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  </style>
</head>

<body>
  <!-- html -->
  <div class="parents">
    <span class="child">css layout, vertical centering</span>
  </div>
</body>

5. The parent sets the elastic box and sets the elastic box related properties;

 <!-- css -->
 <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      display: flex;
      justify-content: center; /* horizontal */
      align-items: center; /* vertical */
    }

    .child {
      width: 200px;
      height: 100px;
      color: black;
      background-color: orange;
    }
  </style>
</head>

<body>
  <!-- html -->
  <div class="parents">
    <span class="child"></span>
  </div>
</body>

6. Grid layout, the parent is converted into a table, and then the child is set to inline or inline block. (It should be noted that the prerequisite for using vertical-align: middle is inline elements and elements with a display value of table-cell).

 <!-- css -->
 <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      display: table-cell;
      text-align: center;
      vertical-align: middle;
    }

    .child {
      width: 200px;
      height: 100px;
      color: #fff;
      background-color: blue;
      display: inline-block; /* Child element settings inline or inline block*/
    }
  </style>
</head>

<body>
  <!-- html -->
  <div class="parents">
    <span class="child"></span>
  </div>
</body>

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

<<:  Color hexadecimal color code table display and hexadecimal value comparison display for easy search

>>:  Web skills: Multiple IE versions coexistence solution IETester

Recommend

Syntax alias problem based on delete in mysql

Table of contents MySQL delete syntax alias probl...

HTTPS Principles Explained

As the cost of building HTTPS websites decreases,...

WeChat applet custom tabbar component

This article shares the specific code of the WeCh...

Some notes on mysql self-join deduplication

Let me briefly explain the functional scenario: T...

How to load the camera in HTML

Effect diagram: Overall effect: Video loading: Ph...

Alibaba Cloud Server Linux System Builds Tomcat to Deploy Web Project

I divide the whole process into four steps: Downl...

Docker nginx + https subdomain configuration detailed tutorial

Today I happened to be helping a friend move his ...

Simple setup of VMware ESXi6.7 (with pictures and text)

1. Introduction to VMware vSphere VMware vSphere ...

HTML exceeds the text line interception implementation principle and code

The HTML code for intercepting text beyond multipl...

Basic Implementation of AOP Programming in JavaScript

Introduction to AOP The main function of AOP (Asp...