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

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

初始显示

.wxml

html 复制代码
<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>

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

元素居中

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

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

.wxml

html 复制代码
<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>

元素水平两端对齐

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

html 复制代码
<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>

元素竖直两端对齐

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

html 复制代码
<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>

元素左对齐

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

html 复制代码
<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>

元素右对齐

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

html 复制代码
<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>

元素上对齐

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

html 复制代码
<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>

元素下对齐

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

html 复制代码
<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>

文字居中对齐

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

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

html 复制代码
<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>

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

更多内容欢迎关注博主。

有用的话欢迎打赏。

相关推荐
chaosama10 小时前
微信小程序带参分享、链接功能
微信小程序·小程序
胡西风_foxww10 小时前
微信小程序动态组件加载的应用场景与实现方式
微信小程序·应用·加载·动态组件
ALLSectorSorft13 小时前
上门服务小程序会员系统框架设计
小程序·apache
甜甜的资料库15 小时前
基于小程序老人监护管理系统源码数据库文档
微信小程序
说私域20 小时前
基于定制开发开源AI智能名片S2B2C商城小程序的首屏组件优化策略研究
人工智能·小程序·开源·零售
Uyker1 天前
微信小程序动态效果实战指南:从悬浮云朵到丝滑列表加载
前端·微信小程序·小程序
happyCoder1 天前
uniapp 微信小程序实现定时消息订阅提醒(前后端)
微信小程序
Uyker2 天前
从零开始制作小程序简单概述
前端·微信小程序·小程序
打小就很皮...2 天前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
说私域2 天前
定制开发开源AI智能名片驱动下的海报工厂S2B2C商城小程序运营策略——基于社群口碑传播与子市场细分的实证研究
人工智能·小程序·开源·零售