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

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

初始显示

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

更多内容欢迎关注博主。

有用的话欢迎打赏。

相关推荐
paterWang3 小时前
基于 SSM框架 的 “捷邻小程序” 系统的设计与实现
小程序
Vin__3 小时前
微信小程序客服消息接收不到微信的回调
spring boot·微信小程序·小程序
计算机软件程序设计3 小时前
vue和微信小程序处理markdown格式数据
前端·vue.js·微信小程序
lvbb663 小时前
微信小程序-二维码绘制
微信小程序·小程序
!win !4 小时前
我与微信审核的“相爱相杀”看个人小程序副业
微信小程序·个人开发·个人副业
狂团商城小师妹5 小时前
智慧废品回收小程序php+uniapp
大数据·微信·微信小程序·小程序·uni-app·微信公众平台
毕业设计-017 小时前
0083.基于springboot+uni-app的社区车位租赁系统小程序+论文
spring boot·小程序·uni-app
算是难了10 小时前
微信小程序-组件复用机制behaviors
微信小程序·小程序
说私域10 小时前
电商运营中私域流量的转化与变现:以开源AI智能名片2+1链动模式S2B2C商城小程序为例
人工智能·小程序·开源·流量运营
EasyNVR15 小时前
基于WebRTC与AI大模型接入EasyRTC:打造轻量级、高实时、强互动的嵌入式音视频解决方案
运维·服务器·微信·小程序·webrtc·p2p·智能硬件