search
HomeWeChat AppletWeChat DevelopmentDetailed explanation of Flex layout for WeChat development

The WeChat applet page layout adopts Flex layout.
Flex layout is a new solution proposed by W3c in 2009, which can realize various page layouts simply, completely and responsively.
Flex layout provides the alignment, direction and order of elements in the container, and they can even be dynamic or of indeterminate size.
The main feature of Flex layout is the ability to adjust its child elements to fill the appropriate space in the most suitable method in different screen sizes.

Detailed explanation of Flex layout for WeChat development

flex layout

Features of Flex layout:

  • Telescopic in any direction , left, right, down, up

  • The order can be exchanged and rearranged in the style layer

  • The main axis and side axis are convenient to configure

  • Space stretching and filling of child elements

  • Alignment along the container

WeChat small The program implements Flex layout. Let’s briefly introduce the use of Flex layout in WeChat mini programs.

Flexible container

An element with <a href="http://www.php.cn/wiki/927.html" target="_blank">display</a>:flex or display:block is a flex container (flexible container), the sub-elements inside are called flex item (flexible items), the sub-elements in flex container are all laid out using Flex .

  • display:block Specify the block container mode, always use a new line to start display, the view container of the WeChat applet (view, scroll-view and swiper ) are dispaly:block by default.

  • display:flex: Specify the in-line container mode to display child elements in a row. You can use the flex-wrap attribute to specify it. Whether to wrap the line, flex-wrap has three values: nowrap (no line wrap) , wrap (line wrap) , wrap-reverse (wrap the first line) Below)
    Code using display:block(default value):

    <view class="flex-row" style="display: block;">
          <view class="flex-view-item">1</view>
          <view class="flex-view-item">2</view>
          <view class="flex-view-item">3</view>
      </view>

    Display effect:

## The display effect of changing Detailed explanation of Flex layout for WeChat development

#block

to

display:flex:

Detailed explanation of Flex layout for WeChat development

flex

You can see the difference between

block and flex from the rendering. The child element view is displayed in a new line (block ) or inline display (flex).

Main axis and cross axis

FlexThe flex container of the layout can be laid out in any direction. The container has two axes by default:
main axis(main axis) and cross axis(cross axis). The starting position of the spindle is
spindle starting point(main start), the end position of the spindle isspindle end point(main end), and the length of the spindle is spindle length(main size). Similarly, the starting point of the cross axis is
the starting point of the cross axis (cross start), the end position is the end point of the cross axis (cross end), and the length is the length of the cross axis(cross size). See the picture below for details:

Detailed explanation of Flex layout for WeChat development

Flex-direction

Note that

spindle is not necessarily From left to right, similarly side axis is not necessarily from top to bottom, the direction of the main axis is controlled by the flex-direction attribute , it has 4 optional values:

  • row: The horizontal direction from left to right is the main axis

  • row-reverse: The horizontal direction from right to left is the main axis

  • column: The vertical direction from top to bottom is the main axis

  • column-reverseThe vertical direction from bottom to top is the main axis

If the horizontal direction is the main axis, that vertical direction is side axis and vice versa.

Renderings of four main axis direction settings:

Detailed explanation of Flex layout for WeChat development

Example diagram

The example in the picture shows the use of different The

flex-direction value is the difference in the arrangement direction. Example code:

<view >
    <view class="flex-row" style="display: flex;flex-direction: row;">
        <view class="flex-view-item">1</view>
        <view class="flex-view-item">2</view>
        <view class="flex-view-item">3</view>
    </view>
    <view class="flex-column" style="display:flex;flex-direction: column;" >
        <view class="flex-view-item">c1</view>
        <view class="flex-view-item">c2</view>
        <view class="flex-view-item">c3</view>
    </view>
</view>

Running effect:

Detailed explanation of Flex layout for WeChat development
##flex-direction

Alignment

There are two alignment methods for child elements:

justify-conent

Define the alignment of child elements on the main axis align-items
Define child The way elements are aligned on the cross axis

justify-content

There are 5 optional alignment methods:<ul class=" list-paddingleft-2"> <li><p><code>flex-start Spindle start point alignment (default value)

  • flex-end Spindle end point alignment

  • center Align center in main axis

  • space-between Align ends except The child elements at both ends are outside the container at both ends, and the intervals between other child elements are equal

  • space-around The distance between each child element Equal, the distance between the child elements at both ends of the container is the same as the distance between other child elements. The alignment of
    justify-content is related to the direction of the main axis. In the figure below, flex-direction is row, and the main axis method is from the left. Go to the right and describe the display effect of jstify-content5 values:

    Detailed explanation of Flex layout for WeChat development

    ##justify-content

  • align-items represents the alignment on the cross axis:

    • stretch fills the entire container (default Value)

    • flex-start Starting point alignment of side axis

    • flex-end side Align the end point of the axis

    • center Center the alignment in the side axis

    • baseline The alignment of the first line of text of the element

    align-tiems is related to the direction of the cross-axis. The following figure uses flex-direction is row, the side axis direction is from top to bottom , and the 5 values ​​​​describing align-items display effect:

    Detailed explanation of Flex layout for WeChat development

    aign-items

    With the direction of the main axis and the side axis and setting their alignment, most page layouts can be realized.

    【Related recommendations】

    1.

    WeChat public account platform source code download

    2.

    Xiaozhu CMS Life Pass O2O System v2. 0 Exclusive Edition Download

    3.

    小 Pigcms (pigcms) WeChat Marketing System V8.52 Pinhaohao Mall Secondary Development Special Edition

    The above is the detailed content of Detailed explanation of Flex layout for WeChat development. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    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
    WordPress网页错位现象解决攻略WordPress网页错位现象解决攻略Mar 05, 2024 pm 01:12 PM

    WordPress网页错位现象解决攻略在WordPress网站开发中,有时候我们会遇到网页元素错位的情况,这可能是由于不同设备上的屏幕尺寸、浏览器兼容性或者CSS样式设置不当所致。要解决这种错位现象,我们需要仔细分析问题、查找可能的原因,并逐步进行调试和修复。本文将分享一些常见的WordPress网页错位问题以及相应的解决攻略,同时提供具体的代码示例,帮助开

    常用的Flex布局属性有哪些常用的Flex布局属性有哪些Feb 25, 2024 am 10:42 AM

    flex布局的常用属性有哪些,需要具体代码示例Flex布局是一种用于设计响应式网页布局的强大工具。它通过使用一组灵活的属性,可以轻松控制网页中元素的排列方式和尺寸。在本文中,我将介绍Flex布局的常用属性,并提供具体的代码示例。display:设置元素的显示方式为Flex。.container{display:flex;}flex-directi

    如何使用Vue实现响应式布局如何使用Vue实现响应式布局Nov 07, 2023 am 11:06 AM

    Vue是一款非常优秀的前端开发框架,它采用MVVM模式,通过数据的双向绑定实现了非常好的响应式布局。在我们的前端开发中,响应式布局是非常重要的一部分,因为它能够让我们的页面针对不同的设备,显示出最佳的效果,从而提高用户体验。在本文中,我们将会介绍如何使用Vue实现响应式布局,并提供具体的代码实例。一、使用Bootstrap实现响应式布局Bootstrap是一

    如何通过Css Flex 弹性布局实现两栏布局如何通过Css Flex 弹性布局实现两栏布局Sep 26, 2023 am 10:54 AM

    如何通过CSSFlex弹性布局实现两栏布局CSSFlex弹性布局是一种现代的布局技术,它能够简化网页布局的过程,使得设计与开发者们能够轻松创建出灵活且适应各种屏幕尺寸的布局。其中,实现两栏布局是Flex布局中的常见需求之一。在这篇文章中,我们将会介绍如何使用CSSFlex弹性布局来实现一个简单的两栏布局,并提供具体的代码示例。使用Flex容器和项目在使

    解决Vue中flex布局样式问题解决Vue中flex布局样式问题Jun 30, 2023 pm 08:51 PM

    Vue是一种流行的JavaScript框架,广泛应用于前端开发中。它的灵活性和强大的功能使得开发人员可以轻松构建交互丰富的Web应用程序。在Vue开发中,flex布局几乎是无处不在的。然而,使用flex布局时,有时会遇到一些样式问题。本文将介绍一些解决flex布局引起的样式问题的方法。首先,让我们了解一下flex布局的基本概念。Flex布局提供了弹性盒子模型

    PHP微信开发:如何实现消息加密解密PHP微信开发:如何实现消息加密解密May 13, 2023 am 11:40 AM

    PHP是一种开源的脚本语言,广泛应用于Web开发和服务器端编程,尤其在微信开发中得到了广泛的应用。如今,越来越多的企业和开发者开始使用PHP进行微信开发,因为它成为了一款真正的易学易用的开发语言。在微信开发中,消息的加密和解密是一个非常重要的问题,因为它们涉及到数据的安全性。对于没有加密和解密方式的消息,黑客可以轻松获取到其中的数据,对用户造成威胁

    如何通过Css Flex 弹性布局实现不规则的网格布局如何通过Css Flex 弹性布局实现不规则的网格布局Sep 28, 2023 pm 09:49 PM

    如何通过CSSFlex弹性布局实现不规则的网格布局在网页设计中,常常需要使用网格布局来实现页面的分割和排版,通常的网格布局都是规则的,每个网格大小相同,而有时候我们可能需要实现一些不规则的网格布局。CSSFlex弹性布局是一种强大的布局方式,它可以很容易地实现各种网格布局,包括不规则的网格布局。下面我们将介绍如何利用CSSFlex弹性布局来实现不

    如何通过Css Flex 弹性布局实现页面元素的垂直居中如何通过Css Flex 弹性布局实现页面元素的垂直居中Sep 27, 2023 pm 03:52 PM

    如何通过CSSFlex弹性布局实现页面元素的垂直居中在网页设计中,经常会遇到需要将页面元素进行垂直居中的情况。CSSFlex弹性布局是一种优雅简洁且灵活的布局方式,能够轻松实现页面元素的垂直居中。本文将详细介绍如何使用CSSFlex布局实现页面元素的垂直居中,并提供具体的代码示例。一、基本原理使用CSSFlex布局实现页面元素的垂直居中,需要有以下几

    See all articles

    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)
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools