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

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

初始显示

.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文件定义样式。

更多内容欢迎关注博主。

有用的话欢迎打赏。

相关推荐
racerun4 小时前
小程序导航设置更多内容的实现方法
小程序
说私域4 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的超级文化符号构建路径研究
人工智能·小程序·开源
mg6684 小时前
微信小程序入门实例_____快速搭建一个快递查询小程序
微信小程序·小程序
程序员柳5 小时前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
Jyywww1218 小时前
微信小程序学习笔记
笔记·学习·微信小程序
The_era_achievs_hero9 小时前
微信小程序41~50
微信小程序·小程序
走,带你去玩17 小时前
uniapp 微信小程序水印
微信小程序·小程序·uni-app
是一碗螺丝粉17 小时前
🔥 微信H5视频自动播放终极秘籍:WeixinJSBridge竟是官方“通行证”?
微信小程序
一笑code18 小时前
vue/微信小程序/h5 实现react的boundary
微信小程序·vue·react