当前位置: 首页 > news >正文

微信小程序元素/文字在横向和纵向实现居中对齐、两端对齐、左右对齐、上下对齐

元素对齐往往是新学者的一大困惑点,在此总结常用的各种元素和文字对齐方式以供参考:

初始显示

.wxml

<view style="width: 100%;height: 500rpx; background-color: lightgray;"><view style="width: 200rpx;height:100rpx;background-color: aqua;">元素1</view><view style="width: 200rpx;height:100rpx; background-color:yellow">元素2</view>
</view>

ef07b97875d8449e9a7a2de30d9baa9a.png

初始定义元素,默认显示在左上角。

元素居中

水平居中关键代码:justify-content: center;

竖直居中关键代码:align-items: center;

.wxml

<view style="width: 100%;height: 500rpx; background-color: lightgray; display: flex; align-items: center; justify-content: center;"><view style="width: 200rpx;height:100rpx;background-color: aqua;">元素1</view><view style="width: 200rpx;height:100rpx; background-color:yellow">元素2</view>
</view>

e31744bc81494ffdbfb9a5f09f7ac34f.png

元素水平两端对齐

关键代码:justify-content: space-between;

<view style="width: 100%;height: 500rpx; background-color: lightgray; display: flex; align-items: center; justify-content: space-between;"><view style="width: 200rpx;height:100rpx;background-color: aqua;">元素1</view><view style="width: 200rpx;height:100rpx; background-color:yellow">元素2</view>
</view>

d49ecc078f304c77878ff4838b8403d8.png 

 元素竖直两端对齐

关键代码:flex-direction: column; justify-content: space-between;

<view style="width: 100%;height: 500rpx; background-color: lightgray; display: flex; flex-direction: column; justify-content: space-between;align-items: center;"><view style="width: 200rpx;height:100rpx;background-color: aqua;">元素1</view><view style="width: 200rpx;height:100rpx; background-color:yellow">元素2</view>
</view>

116f1adcc7e045068a56730c5fdfe9be.png

元素左对齐

关键代码:justify-content: flex-start;

<view style="width: 100%;height: 500rpx; background-color: lightgray; display: flex; justify-content: flex-start;align-items: center;"><view style="width: 200rpx;height:100rpx;background-color: aqua;">元素1</view><view style="width: 200rpx;height:100rpx; background-color:yellow">元素2</view>
</view>

0d373a64a79c40d9982502d6d187632c.png 

元素右对齐

关键代码:justify-content:flex-end;

<view style="width: 100%;height: 500rpx; background-color: lightgray; display: flex; justify-content:flex-end;align-items: center;"><view style="width: 200rpx;height:100rpx;background-color: aqua;">元素1</view><view style="width: 200rpx;height:100rpx; background-color:yellow">元素2</view>
</view>

 8633651beb9446eb83f7674a06bac28f.png

元素上对齐

关键代码:flex-direction: column; justify-content:flex-start;

<view style="width: 100%;height: 500rpx; background-color: lightgray; display: flex;flex-direction: column; justify-content:flex-start;align-items: center;"><view style="width: 200rpx;height:100rpx;background-color: aqua;">元素1</view><view style="width: 200rpx;height:100rpx; background-color:yellow">元素2</view>
</view>

6d7afc7dac0146eb8ab9f3c5ae4a9d0e.png 

元素下对齐

关键代码:flex-direction: column; justify-content:flex-end;

<view style="width: 100%;height: 500rpx; background-color: lightgray; display: flex;flex-direction: column; justify-content:flex-end;align-items: center;"><view style="width: 200rpx;height:100rpx;background-color: aqua;">元素1</view><view style="width: 200rpx;height:100rpx; background-color:yellow">元素2</view>
</view>

955fa34f927c4062ab7c2585dce14802.png

文字居中对齐

文字其他对齐方式可以参考元素对齐

关键代码:justify-content:center;align-items: center;

<view style="width: 100%;height: 500rpx; background-color: lightgray; display: flex; justify-content:center;align-items: center; "><view style="width: 200rpx;height:100rpx;background-color: aqua; display: flex; align-items: center; justify-content: center;">元素1</view><view style="width: 200rpx;height:100rpx; background-color:yellow; display: flex; align-items: center; justify-content: center;">元素2</view>
</view>

e1c01d9772fb46fd8b1d398aaa9934d2.png

可以在wxml的style参数中修改元素显示样式,也可在wxss文件定义样式。 

更多内容欢迎关注博主。

有用的话欢迎打赏。

 

http://www.lryc.cn/news/288203.html

相关文章:

  • 兼容树莓派扩展模块,专注工业产品开发的瑞米派强势来袭
  • 云原生 - 微信小程序 COS 对象存储图片缓存强制更新解决方案
  • 设计公司设计ppt的优势—南京梵构广告
  • gitlab设置/修改克隆clone地址端口
  • Jellyfin影音服务本地部署并结合内网穿透实现公网访问本地资源
  • 笨蛋学设计模式行为型模式-责任链模式【18】
  • 【.NET Core】深入理解任务并行库 (TPL)
  • win10安装redis并配置加自启动(采用官方推荐unix子系统)
  • 【大数据面试题】HBase面试题附答案
  • SpringBoot中从HikariCP迁移到Oracle UCP指南
  • 第3章 接口和API设计
  • HBase入门:实现原理
  • Redis入门到实战-基础篇+实战篇+高级篇+原理篇
  • redis 工具类
  • 焕新升级,不同以“网” | AnyCase客户端全新上线
  • 导出 MySQL 数据库表结构、数据字典word设计文档
  • conda管理python安装包与虚拟环境的相关命令汇总
  • Vue3引用echart5 报错解决
  • 浅析HTTP协议
  • etcd未授权到控制k8s集群
  • 制作一个简单的HTML个人网站
  • 头歌C语言字符数组
  • 【mongoDB】文档 CRUD
  • 每日一题——LeetCode1337.矩阵中战斗力最弱的K行
  • docker指令存档
  • Pandas ------ 向 Excel 文件中写入含有 multi-index 和 Multi-column 表头的数据
  • ChatGPT 和文心一言 | 两大AI助手哪个更胜一筹
  • flink学习之窗口处理函数
  • Python 基于pytorch从头写GPT模型;实现gpt实战
  • 2023年NOC大赛(学而思赛道)创意编程Python初中组决赛真题