Home Web Front-end CSS Tutorial How to achieve vertical centering of page elements through CSS Flex layout

How to achieve vertical centering of page elements through CSS Flex layout

Sep 27, 2023 pm 03:52 PM
flex layout Center vertically css programming

如何通过Css Flex 弹性布局实现页面元素的垂直居中

How to achieve vertical centering of page elements through CSS Flex elastic layout

In web design, we often encounter situations where page elements need to be vertically centered. CSS Flex layout is an elegant, concise and flexible layout method that can easily achieve vertical centering of page elements. This article will introduce in detail how to use CSS Flex layout to achieve vertical centering of page elements and provide specific code examples.

1. Basic principles

Using CSS Flex layout to achieve vertical centering of page elements requires the following basic principles:

  1. The elements to be vertically centered are The parent container is set to Flex layout.
  2. Use the justify-content attribute to control the alignment of elements on the main axis.
  3. Use the align-items attribute to control the alignment of elements on the cross axis.

2. Specific implementation

The following is a simple example to demonstrate how to use CSS Flex layout to achieve vertical centering of page elements:

  1. HTML structure
<div class="container">
  <div class="content">
    <p>这是要垂直居中的内容</p>
  </div>
</div>
Copy after login
  1. CSS Style
.container {
  display: flex; /* 设置为Flex布局 */
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  height: 100vh; /* 设置容器高度为100视口高度,使容器占据整个屏幕 */
}

.content {
  background-color: #f0f0f0;
  padding: 20px;
}
Copy after login

In the above code, we first set the parent container to Flex layout, and use the justify-content attribute to set its child elements in Align it horizontally in the center, and use the align-items attribute to center align its child elements in the vertical direction. At the same time, in order to make the container occupy the entire screen, we use the height: 100vh attribute.

3. Example effect

Through the above code, we successfully achieved vertical centering of page elements. After you run the sample code, you'll see "This is the content to be vertically centered" displayed vertically centered on the page, and the container will take up the entire screen.

4. Summary

Vertical centering of page elements can be easily achieved through CSS Flex elastic layout. You only need to set the parent container to Flex layout, and use the justify-content and align-items attributes to control the alignment of elements on the main axis and cross axis respectively, to achieve a simple and effective vertically centered layout. I hope this article can help you better apply CSS Flex layout and improve your web design capabilities.

The above is the detailed content of How to achieve vertical centering of page elements through CSS Flex layout. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to center images in html web pages How to center images in html web pages Apr 05, 2024 pm 12:18 PM

In HTML, there are two ways to center-align an image: use CSS: margin: 0 auto; to center the image horizontally, and display: block; to make it occupy the entire width. Use the HTML: <center> element to center the image horizontally, but it is less flexible and does not comply with the latest web standards.

How to adjust text position in dreamweaver How to adjust text position in dreamweaver Apr 09, 2024 am 02:24 AM

Adjusting the text position in Dreamweaver can be completed by the following steps: Select the text and use the text position adjuster to make horizontal adjustments: left alignment, right alignment, center alignment; 2. Make vertical adjustments: top alignment, bottom alignment, vertical center; 3. Press Shift key and use the arrow keys to fine-tune the position; 4. Use shortcut keys to quickly align: left alignment (Ctrl/Cmd + L), right alignment (Ctrl/Cmd + R), center alignment (Ctrl/Cmd + C).

How to center the text box in html How to center the text box in html Apr 22, 2024 am 10:33 AM

There are many ways to center the HTML text box: text input box: use the CSS code input[type="text"] { text-align: center; } text area: use the CSS code textarea { text-align: center; } horizontal centering: Use the text-align: center style on the text box parent element to center it vertically: use the vertical-align attribute input[type="text"] { vertical-align: middle; }Flexbox: use display:

How to center ul content in css How to center ul content in css Apr 26, 2024 pm 12:24 PM

Center UL content in CSS: Use the text-align property: Set the alignment of text, including the content of list items. Use the margin attribute: Set the left and right margins of the element, and use margin: auto to achieve horizontal centering. Use the display attribute: Set the element to inline-block, then center it vertically using text-align: center. Use flexbox properties: Horizontal and vertical centering through justify-content: center and align-items: center.

How to center the frame in html How to center the frame in html Apr 22, 2024 am 10:45 AM

There are four ways to center the HTML frame: margin: 0 auto;: Center the frame horizontally. text-align: center;: Center the frame content horizontally. display: flex; align-items: center;: Center the frame vertically. position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);: Uses CSS transforms to position the frame in the center of the fixed-size frame's container.

How to center the font in sublime How to center the font in sublime Apr 03, 2024 am 10:21 AM

Methods to align text in Sublime Text include: using shortcut keys (paragraph: Ctrl + Alt + C, single line: Ctrl + Alt + E), using the "Align" option on the menu bar, and installing alignment plug-ins (such as AlignTab, Alignment Plugin ), or manual alignment (centered: fills spaces, justified: creates borders).

How to put the image in the middle with css How to put the image in the middle with css Apr 25, 2024 am 11:51 AM

There are three main ways to center an image in CSS: using display: block; and margin: 0 auto;. Use flexbox layout or grid layout and set align-items or justify-content to center. Use absolute positioning, set top and left to 50%, and apply transform: translate(-50%, -50%);.

How to center a div in html How to center a div in html Apr 05, 2024 am 09:00 AM

There are two ways to center a div in HTML: Use the text-align attribute (text-align: center): For simpler layouts. Use flexible layout (Flexbox): Provide more flexible layout control. The steps include: enabling Flexbox (display: flex) in the parent element. Set the div as a Flex item (flex: 1). Use the align-items and justify-content properties for vertical and horizontal centering.

See all articles