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

Common HTML tag writing errors

We better start paying attention, because HTML Po...

MySQL 8.0.18 installation and configuration method graphic tutorial under MacOS

This article records the installation of MySQL 8....

Detailed explanation of MySQL backup and recovery practice of mysqlbackup

1. Introduction to mysqlbackup mysqlbackup is the...

Does MySql need to commit?

Whether MySQL needs to commit when performing ope...

MySQL daily statistics report fills in 0 if there is no data on that day

1. Problem reproduction: Count the total number o...

JavaScript mobile H5 image generation solution explanation

Now there are many WeChat public account operatio...

Solution to the inconsistency between crontab execution time and system time

Preface In LINUX, periodic tasks are usually hand...

Detailed explanation of non-parent-child component value transfer in Vue3

Table of contents App.vue sub1.vue sub2.vue Summa...

Introduction and usage examples of ref and $refs in Vue

Preface In JavaScript, you need to use document.q...

Basic usage examples of listeners in Vue

Table of contents Preface 1. Basic usage of liste...

A brief analysis of React's understanding of state

How to define complex components (class component...