Table of Contents
flex syntax
Properties on the container" >Properties on the container
Properties on the project
Layout
Home Web Front-end CSS Tutorial Summary of learning Flexbox experience

Summary of learning Flexbox experience

Mar 22, 2017 pm 02:55 PM
flexbox

flex syntax

Elements that adopt Flex layout are called Flex containers (flex containers), referred to as "containers". All its child elements automatically become container members, called Flex items (flex items), referred to as "items".

Containers have two axes by default: the horizontal main axis (main axis) and the vertical cross axis (cross axis). The starting position of the main axis (the intersection with the border) is called main start, and the ending position is called main end; the starting position of the cross axis is called cross start, and the ending position is called cross end.

Items are arranged along the main axis by default. The main axis space occupied by a single item is called main size, and the cross axis space occupied by a single item is called cross size.

Properties on the container

The following 6 properties are set on the container:

flex-direction

flex-wrap

flex-flow

justify-content

align-items

align-content
Copy after login

flex-direction: The flex-direction property determines the direction of the main axis (i.e. the direction in which items are arranged).

row(默认) | row-reverse | column | column-reverse
Copy after login

flex-wrap: By default, items are arranged on a line (also called the "axis"). The flex-wrap attribute defines how to wrap the line if one axis cannot fit.

nowrap(默认) | wrap | wrap-reverse
Copy after login

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

justify-content: The justify-content attribute is defined The alignment of the item on the main axis.

flex-start | flex-end | center | space-between | space-around
Copy after login

align-items attribute: The align-items attribute defines how items are aligned on the cross axis.

flex-start | flex-end | center | baseline | stretch
Copy after login

align-content: The align-content attribute defines the alignment of multiple axes on the cross axis. If the item has only one axis, this property has no effect

flex-start | flex-end | center | space-between | space-around | stretch
Copy after login

Properties on the project

The following 6 properties are set on the project:

order

flex-grow

flex-shrink

flex-basis

flex

align-self
Copy after login

order: order property definition The order in which items are sorted. The smaller the value, the higher the ranking. The default is 0.

flex-grow: The flex-grow attribute defines the magnification ratio of the item. The default is 0, that is, if there is remaining space, it will not be enlarged.

If all items have a flex-grow property of 1, they will equally divide the remaining space (if there is any). If one item's flex-grow property is 2 and the other items are all 1, the former will occupy twice as much remaining space as the other items.

flex-shrink: The flex-shrink attribute defines the shrinkage ratio of the item. The default is 1, that is, if there is insufficient space, the item will shrink.

If the flex-shrink property of all items is 1, when there is insufficient space, they will all be reduced proportionally. If the flex-shrink property of one item is 0 and the other items are 1, the former will not shrink when there is insufficient space.

flex-basis: The flex-basis property defines the main axis space (main size) occupied by the item before allocating excess space. The browser uses this attribute to calculate whether there is extra space on the main axis. Its default value is auto, which is the original size of the project.

flex-basis: <length> | auto; /* default auto */
Copy after login

It can be set to the same value as the width or height attribute (such as 350px), and the item will occupy a fixed space.

flex: The flex attribute is the abbreviation of flex-grow, flex-shrink and flex-basis. The default value is 0 1 auto. The last two properties are optional.

This attribute has two shortcut values: auto (1 1 auto) and none (0 0 auto).

It is recommended to give priority to using this attribute instead of writing three separate attributes separately, because the browser will infer the relevant values.

align-self: The align-self attribute allows a single item to be aligned differently from other items and can override the align-items attribute. The default value is auto, which means inherits the align-items attribute of the parent element. If there is no parent element, it is equivalent to stretch.

align-self: auto | flex-start | flex-end | center | baseline | stretch
Copy after login

Layout

Grid layout

1 Basic grid layout

The simplest grid layout is even distribution. The item can be set to flex:1

.Grid {
  display: flex;
}

.Grid-cell {
  flex: 1;
}
Copy after login

2 Percent layout

The width of a certain grid is a fixed percentage, and the remaining space is evenly allocated to the other grids.

Set width: percentage; or flex: 0 0 percentage; for items that require percentages; set flex for automatically allocated items: 1;

3 Holy Grail Layout

<!DOCTYPE html>
<html>
<head>
<meta name="description" content="flex 圣杯布局">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body class="HolyGrail">
  <header>header</header>
  <p class="HolyGrail-body">
    <main class="HolyGrail-content">content</main>
    <nav class="HolyGrail-nav">left nav</nav>
    <aside class="HolyGrail-ads">right ad</aside>
  </p>
  <footer>footer</footer>
</body>
</html>

*{
  margin: 0;
}
.HolyGrail {
  display: flex;
  min-height: 100vh;
  flex-direction: column;
  text-align: center;
}

header,
footer {
  flex: 0 0 40px;
  background-color: #ccc;
}

.HolyGrail-body {
  display: flex;
  flex: 1;
}

.HolyGrail-content {
  flex: 1;
  background-color: #0f0;
  
}

.HolyGrail-nav, .HolyGrail-ads {
  /* 两个边栏的宽度设为12em */
  flex: 0 0 12em;
  background-color: #00f;
}

.HolyGrail-nav {
  /* 导航放到最左边 */
  order: -1;
  background-color: #f00;
}
Copy after login

View demo

If it is a small screen, the three columns of the torso will automatically become vertical overlaps.

@media (max-width: 768px) {
  .HolyGrail-body {
    flex-direction: column;
    flex: 1;
  }
  .HolyGrail-nav,
  .HolyGrail-ads,
  .HolyGrail-content {
    flex: auto;
  }
}
Copy after login

4 Fluid layout

The number of items in each row is fixed and will be automatically divided into rows.

.parent {
  width: 200px;
  height: 150px;
  background-color: black;
  display: flex;
  flex-flow: row wrap;
  align-content: flex-start;
}

.child {
  box-sizing: border-box;
  background-color: white;
  flex: 0 0 25%;
  height: 50px;
  border: 1px solid red;
}
Copy after login

Compatible

*在旧版的规范中,使用比例伸缩布局时,子元素的内容长短不同会导致无法“等分”,这个时候,我们需要给子元素设置一个“width:0%”来解决问题。

*不要给flexbox里的子元素设置“margin:auto”的属性,在部分安卓机下,它会导致该元素的宽度撑开到100%占位
Copy after login

The above is the detailed content of Summary of learning Flexbox experience. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
CSS layout property optimization tips: position sticky and flexbox CSS layout property optimization tips: position sticky and flexbox Oct 20, 2023 pm 03:15 PM

CSS layout attribute optimization tips: positionsticky and flexbox In web development, layout is a very important aspect. A good layout structure can improve the user experience and make the page more beautiful and easy to navigate. CSS layout properties are the key to achieving this goal. In this article, I will introduce two commonly used CSS layout property optimization techniques: positionsticky and flexbox, and provide specific code examples. 1. positions

Flexible application skills of position attribute in H5 Flexible application skills of position attribute in H5 Dec 27, 2023 pm 01:05 PM

How to flexibly use the position attribute in H5. In H5 development, the positioning and layout of elements are often involved. At this time, the CSS position property will come into play. The position attribute can control the positioning of elements on the page, including relative positioning, absolute positioning, fixed positioning and sticky positioning. This article will introduce in detail how to flexibly use the position attribute in H5 development.

How to use CSS3's flexbox technology to achieve even distribution of web content? How to use CSS3's flexbox technology to achieve even distribution of web content? Sep 11, 2023 am 11:33 AM

How to use CSS3’s flexbox technology to achieve even distribution of web content? With the development of web design, people have higher and higher requirements for web page layout. In order to achieve even distribution of web content, CSS3's flexbox technology has become a very effective solution. This article will introduce how to use flexbox technology to achieve even distribution of web content, and give some practical examples. 1. What is flexbox technology? Flexbox (elastic layout) is a new feature added in CSS3.

HTML tutorial: How to use Flexbox for adaptive equal-height, equal-width, equal-spacing layout HTML tutorial: How to use Flexbox for adaptive equal-height, equal-width, equal-spacing layout Oct 27, 2023 pm 05:51 PM

HTML tutorial: How to use Flexbox for adaptive equal-height, equal-width, equal-spacing layout, specific code examples are required. Introduction: In modern web design, layout is a very critical factor. For pages that need to display a large amount of content, how to reasonably arrange the position and size of elements to achieve good visibility and ease of use is an important issue. Flexbox (flexible box layout) is a very powerful tool through which various flexible layout needs can be easily realized. This article will introduce Flexbox in detail

HTML tutorial: How to use Flexbox for vertical equal height layout HTML tutorial: How to use Flexbox for vertical equal height layout Oct 16, 2023 am 09:12 AM

HTML Tutorial: How to Use Flexbox for Vertical Height Layout In web development, layout has always been an important issue. Especially when it is necessary to implement vertical equal-height layout, the traditional CSS layout method often encounters some difficulties. This problem can be easily solved using Flexbox layout. This tutorial will introduce in detail how to use Flexbox for vertical equal height layout and provide specific code examples. Flexbox is a new feature in CSS3 that can be used to create flexible, responsive layouts.

HTML tutorial: How to use Flexbox for adaptive equal height layout HTML tutorial: How to use Flexbox for adaptive equal height layout Oct 21, 2023 am 10:00 AM

HTML tutorial: How to use Flexbox for adaptive equal-height layout, specific code examples are required. Introduction: In web design and development, implementing adaptive equal-height layout is a common requirement. Traditional CSS layout methods often face some difficulties when dealing with equal height layout, and Flexbox layout provides us with a simple and powerful solution. This article will introduce the basic concepts and common usage of Flexbox layout, and give specific code examples to help readers quickly master the use of Flexbox to implement their own

HTML Tutorial: How to Use Flexbox for Evenly Distributed Layout HTML Tutorial: How to Use Flexbox for Evenly Distributed Layout Oct 16, 2023 am 09:31 AM

HTML Tutorial: How to Use Flexbox for Evenly Distributed Layout Introduction: In web design, it is often necessary to layout elements. Traditional layout methods have some limitations, and Flexbox (flexible box layout) is a layout method that can provide more flexibility and power. This article will introduce how to use Flexbox to achieve even distribution layout, and give specific code examples. 1. Introduction to Flexbox Flexbox is a flexible box layout model introduced in CSS3, which allows elements to

HTML tutorial: How to use Flexbox for scalable equal-height layout HTML tutorial: How to use Flexbox for scalable equal-height layout Oct 27, 2023 pm 12:15 PM

HTML tutorial: How to use Flexbox for scalable equal-height layout, specific code examples are required. Introduction: In web page layout, we often encounter the need to achieve equal-height layout effects. The traditional method is more cumbersome and needs to be implemented using JavaScript or table layout. Using Flexbox, you can easily implement scalable equal-height layouts without relying on other technologies. This article will introduce how to use Flexbox to implement scalable equal-height layout, and attach detailed code examples. one,

See all articles