码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 微信小程序开发常用的布局


    在微信小程序开发中,常用的布局主要包括以下几种:

    1. Flex 布局:Flex 布局是一种弹性盒子布局,通过设置容器的属性来实现灵活的布局方式。它可以在水平或垂直方向上对子元素进行对齐、排列和分布。Flex 布局非常适用于创建响应式布局和自适应布局。
    <view class="container">
      <view class="item">Item 1view>
      <view class="item">Item 2view>
      <view class="item">Item 3view>
    view>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    .container {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
    }
    
    .item {
      flex: 1;
      background-color: #f2f2f2;
      padding: 10px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. Grid 布局:Grid 布局是一种二维网格布局系统,可以将页面划分为多个行和列,使得页面元素可以在网格中灵活地布局。Grid 布局适用于创建复杂的网格结构,可以实现多列布局、响应式布局和自适应布局。
    <view class="container">
      <view class="item">Item 1view>
      <view class="item">Item 2view>
      <view class="item">Item 3view>
    view>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    .container {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 10px;
    }
    
    .item {
      background-color: #f2f2f2;
      padding: 10px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. Float 布局:Float 布局是一种传统的布局方式,通过设置元素的浮动属性来实现元素的排列和对齐。Float 布局适用于创建多列布局和浮动元素的布局。
    <view class="container">
      <view class="item">Item 1view>
      <view class="item">Item 2view>
      <view class="item">Item 3view>
    view>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    .container {
      overflow: hidden; /* 清除浮动 */
    }
    
    .item {
      float: left;
      width: 33.33%;
      background-color: #f2f2f2;
      padding: 10px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. Position 布局:Position 布局是通过设置元素的定位属性来实现元素的自由定位。可以使用相对定位、绝对定位和固定定位来实现元素在页面中的精确布局。
    <view class="container">
      <view class="item">Item 1view>
      <view class="item">Item 2view>
      <view class="item">Item 3view>
    view>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    .container {
      position: relative;
    }
    
    .item {
      position: absolute;
      top: 0;
      left: 0;
      width: 100px;
      height: 100px;
      background-color: #f2f2f2;
      padding: 10px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. Block 布局:Block 布局是一种基于元素块级特性的布局方式,元素默认以块级形式垂直堆叠。Block 布局适用于创建垂直布局和分隔不同部分的页面。
    <view class="container">
      <view class="block">Block 1view>
      <view class="block">Block 2view>
      <view class="block">Block 3view>
    view>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    .container {
      background-color: #f2f2f2;
      padding: 10px;
    }
    
    .block {
      background-color: white;
      padding: 10px;
      margin-bottom: 10px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. Table 布局:Table 布局是通过使用 、 和
      等 HTML 标签来创建表格布局。Table 布局适用于显示和排列多个数据项的情况。
      <table class="table">
        <tr>
          <td>Item 1td>
          <td>Item 2td>
          <td>Item 3td>
        tr>
        <tr>
          <td>Item 4td>
          <td>Item 5td>
          <td>Item 6td>
        tr>
      table>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      .table {
        width: 100%;
        border-collapse: collapse;
      }
      
      td {
        border: 1px solid #ccc;
        padding: 10px;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      1. Card 布局:Card 布局是一种常见的布局方式,通过使用卡片组件或自定义样式来创建卡片式布局。Card 布局适用于展示信息、图片和操作按钮等内容的场景。
      <view class="card">
        <image class="image" src="image.jpg">image>
        <view class="content">
          <text class="title">Card Titletext>
          <text class="description">Card Descriptiontext>
          <button class="button">Click Mebutton>
        view>
      view>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      .card {
        display: flex;
        background-color: white;
        padding: 10px;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      }
      
      .image {
        width: 100px;
        height: 100px;
        object-fit: cover;
        border-radius: 4px;
      }
      
      .content {
        flex: 1;
        margin-left: 10px;
      }
      
      .title {
        font-size: 16px;
        font-weight: bold;
      }
      
      .description {
        margin-top: 8px;
        color: #666;
      }
      
      .button {
        margin-top: 10px;
        background-color: #007aff;
        color: white;
        border: none;
        padding: 8px 16px;
        border-radius: 4px;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38

      这些是微信小程序开发中常用的布局方式。你可以根据具体的页面需求和设计来选择合适的布局方式。这些布局技术可以帮助你创建灵活、响应式和美观的小程序页面。

    2. 相关阅读:
      vscode setting.json 全局设置 工作区设置 位置 优先级
      C语言面试必看-指针笔试题详解
      ZA7783是一颗将单路MIPI DSI信号转换成单路LVDS/TTL信号的转接芯片
      ggcor替代包:linkET,相关图,mantel test可视化
      【示波器专题】示波器带宽对测量的影响
      蜂鸟E203学习笔记(四)——取指
      微信小程序自定义方法submitPwd(e){}传入的e有什么作用
      基于线性回归根据饮食习惯和身体状况估计肥胖水平
      Javaweb之javascript的详细解析
      C++并发与多线程学习笔记--线程启动、结束,创建线程多法
    3. 原文地址:https://blog.csdn.net/ultramand/article/details/136519319
      • 最新文章
      • 沪漂五周年了:我越来越迷茫了
        Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
        MySQL-Seconds_behind_master的精度误差
        [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
        AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
        Agent OS :五种驯服不确定性的范式
        PortSwigger SQL注入LAB11
        数据库即时编译JIT
        [Begin]AI Learn Data Day 0
        深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
      • 热门文章
      • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
        奉劝各位学弟学妹们,该打造你的技术影响力了!
        五年了,我在 CSDN 的两个一百万。
        Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
        面试官都震惊,你这网络基础可以啊!
        你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
        心情不好的时候,用 Python 画棵樱花树送给自己吧
        通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
        13 万字 C 语言从入门到精通保姆级教程2021 年版
        10行代码集2000张美女图,Python爬虫120例,再上征途
      小工具 小游戏
      Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

      京公网安备 11010502049817号