An example of vertical centering of sub-elements in div using Flex layout

An example of vertical centering of sub-elements in div using Flex layout

1. Flex is the abbreviation of Flexible Box, which means "flexible layout" and is used to provide maximum flexibility for the box model. Any container can be specified as a Flex layout. Note that after setting the Flex layout, the float, clear and vertical-align properties of the child elements will become invalid.

Elements that use Flex layout are called Flex containers, or "containers" for short. All its child elements automatically become container members, called Flex items, or "items" for short. By default, a container has two axes: the horizontal main axis and the vertical cross axis. The starting position of the main axis (the intersection with the border) is called the main start, and the ending position is called the main end; the starting position of the cross axis is called the cross start, and the ending position is called the cross end. Items are arranged along the main axis by default. The main axis space occupied by a single item is called the main size, and the cross axis space occupied is called the cross size.

flex-direction flex-wrap flex-flo justify-content align-items align-content

2. Vertical centering of elements within a block

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>About vertical centering of elements</title>
    <style>
        html, body {
            border: 0;
            margin: 0;
            padding: 0;
            height: 100%;
            width: 100%;
        }
 
        .div-main {
            display: flex;
            align-items: center;
            justify-content: center;
            height: 30%;
            width: 50%;
            background: #00a2d4;
        }
 
        .sub-span {
            margin: auto;
            font-size: xx-large;
        }
 
    </style>
</head>
 
<body>
 
<div class="div-main">
 
    <span class="sub-span">
    The Goddess of Luo River
</div>
</body>
</html>

This is the end of this article about examples of vertically centering sub-elements inside a div using Flex layout. For more information about vertically centering sub-elements inside a div using Flex, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope that everyone will support 123WORDPRESS.COM in the future!

<<:  Various methods to implement the prompt function of text box in html

>>:  Analysis of the reasons why MySQL's index system uses B+ tree

Recommend

How to install Linux flash

How to install flash in Linux 1. Visit the flash ...

CSS to achieve the small sharp corner effect of bubbles

Effect picture (the border color is too light, pu...

HTML Tutorial: title attribute and alt attribute

XHTML is the basis of CSS layout. jb51.net has al...

MySQL trigger principle and usage example analysis

This article uses examples to explain the princip...

Docker case analysis: Building a MySQL database service

Table of contents 1 Create configuration and data...

How to use Docker to build enterprise-level custom images

Preface Before leaving get off work, the author r...

Summary of Linux sftp command usage

sftp is the abbreviation of Secure File Transfer ...

Common writing examples for MySQL and Oracle batch insert SQL

Table of contents For example: General writing: S...

Writing Snake Game with Native JS

This article shares the specific code of writing ...

Example of implementing a virtual list in WeChat Mini Program

Table of contents Preface analyze Initial Renderi...

Mini Program Development to Implement Unified Management of Access_Token

Table of contents TOKEN Timer Refresher 2. Intern...

How to process blob data in MySQL

The specific code is as follows: package epoint.m...