Home Web Front-end CSS Tutorial How to layout web pages through flex

How to layout web pages through flex

Jan 26, 2019 am 10:27 AM
flex

flex is called elastic layout. When the parent element is set to the flex value, the child elements will automatically become its members, and then various layout effects can be achieved by setting attribute values. For example, by setting the justify-content attribute to the parent element to achieve the horizontal and vertical centering effect

Various page layouts can be implemented simply, completely, and responsively through Flex. With the development of browsers, it is now supported by all browsers, which means that it is now safe to use this feature. Next, I will share with you how to layout web pages through flex

How to layout web pages through flex

[Recommended course: CSS Tutorial

flex layout

is called the "flexible box model". When the parent container sets display: flex, the child element automatically becomes Among its members, the container has two axes by default: the horizontal main axis and the vertical cross axis. The items are arranged along the main axis by default.

Some properties of the flex container (parent container):

flex-direction: determines the direction of the main axis (ie, the arrangement direction of the items)

flex -wrap: If one axis cannot be arranged, how to wrap the line

flex-flow: It is the abbreviation of the flex-direction attribute and the flex-wrap attribute. The default value is row nowrap

justify-content: Defines the alignment of items on the main axis

align-items: Defines how items are aligned on the cross axis

align-content: Defines the alignment of multiple axes. If the item has only one axis, this property has no effect

Example

flex achieves horizontal and vertical centering

* {
            margin: 0;
            padding: 0;
        }

html {
            width: 100%;
            height: 100%;
        }
 body {
            display: flex;
            width: 100%;
            height: 100%;
            background: #eee;
            justify-content: center;
            align-items: center;
        }
 .box {
            width: 200px;
            height: 200px;
            background:pink;
        }
Copy after login

Rendering:

How to layout web pages through flex

Two columns of equal height layout:

Fixed width on the left , the right side occupies the remaining width:

<style>
    *{
        margin: 0;
        padding:0;
    }
    .grid{
        display: flex;
        border: 1px solid #ccc;
    }
    .left{
    
        background-color:pink;
        flex-basis: 200px;
}
    .right{
    
        background-color:skyblue;
        flex-basis:calc(100% - 200px);
    }
</style>
Copy after login

Rendering:

How to layout web pages through flex

##Summary: The above is the relevant content about flex layout, I hope Helpful to everyone.

The above is the detailed content of How to layout web pages through flex. 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 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)

Take you step by step to implement 3D dice using CSS Flex and Grid layout (with code) Take you step by step to implement 3D dice using CSS Flex and Grid layout (with code) Sep 23, 2022 am 09:58 AM

In front-end interviews, we are often asked how to implement dice/mahjong layout using CSS. The following article will introduce to you how to use CSS to create a 3D dice (Flex and Grid layout implement 3D dice). I hope it will be helpful to you!

What are the commonly used Flex layout properties? What are the commonly used Flex layout properties? Feb 25, 2024 am 10:42 AM

What are the common properties of flex layout? Specific code examples are required. Flex layout is a powerful tool for designing responsive web page layouts. It makes it easy to control the arrangement and size of elements in a web page by using a flexible set of properties. In this article, I will introduce the common properties of Flex layout and provide specific code examples. display: Set the display mode of the element to Flex. .container{display:flex;}flex-directi

An article explaining in detail the impact of three flex properties on elements An article explaining in detail the impact of three flex properties on elements Aug 30, 2022 pm 07:50 PM

During development, the flex attribute is often used to act on the child elements of the flexible box, such as: flex:1 or flex: 1 1 auto. So how does this attribute control the behavior of the element? What exactly does flex:1 mean? Let this article take you through a thorough understanding of the flex property!

Take you through the three attributes of Flex layout: flex-grow, flex-shrink, flex-basis Take you through the three attributes of Flex layout: flex-grow, flex-shrink, flex-basis Dec 06, 2022 pm 08:37 PM

This article will give you an in-depth understanding of the three properties of CSS Flex layout: flex-grow, flex-shrink, and flex-basis. I hope it will be helpful to you!

How to achieve sliding menu effect through CSS Flex layout How to achieve sliding menu effect through CSS Flex layout Sep 26, 2023 pm 02:13 PM

How to achieve the sliding menu effect through CSSFlex elastic layout. In web design, sliding menu is a common interactive effect, which can make the web page more smooth and beautiful. This article will teach you how to use CSSFlex elastic layout to achieve this effect, and provide specific code examples. CSSFlex is a new layout method that can easily achieve various complex layout effects. It controls the layout by setting the properties of the container and child elements, of which the flex property is one of the most important properties. First, we need a

Detailed explanation of grid spacing and border processing methods in CSS Flex flexible layout Detailed explanation of grid spacing and border processing methods in CSS Flex flexible layout Sep 26, 2023 am 10:31 AM

Title: Detailed explanation of grid spacing and border processing methods in CSSFlex elastic layout Introduction: CSSFlex elastic layout is a modern page layout method that allows web pages to automatically adapt to different screen sizes and is flexible and responsive. When using CSSFlex flexible layout, we often encounter situations where we need to set grid spacing and borders. This article will introduce in detail the grid spacing and border processing methods in CSSFlex flexible layout, and provide specific code examples. 1. Grid spacing

A detailed explanation of Flex layout in CSS3 A detailed explanation of Flex layout in CSS3 Nov 01, 2022 pm 07:29 PM

This article will take you through the Flex layout in CSS3, I hope it will be helpful to you!

CSS adaptive layout property optimization tips: flex and grid CSS adaptive layout property optimization tips: flex and grid Oct 21, 2023 am 08:03 AM

CSS adaptive layout attribute optimization tips: flex and grid In modern web development, implementing adaptive layout is a very important task. With the popularity of mobile devices and the diversification of screen sizes, it is an essential requirement to ensure that the website can be displayed well on various devices and adapt to different screen sizes. Fortunately, CSS provides some powerful properties and techniques for implementing adaptive layout. This article will focus on two commonly used properties: flex and grid, and provide specific code examples.

See all articles