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

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

初始显示

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

更多内容欢迎关注博主。

有用的话欢迎打赏。

相关推荐
说私域6 小时前
开源链动2+1模式、AI智能名片与S2B2C商城小程序融合下的社交电商营销新范式
人工智能·小程序·开源·零售
十年之少9 小时前
异步编程——微信小程序
笔记·学习·微信小程序
山海青风9 小时前
微信小程序实战案例 - 餐馆点餐系统 阶段 4 - 订单列表 & 状态
微信小程序·小程序
小离a_a10 小时前
小程序css实现容器内 数据滚动 无缝衔接 点击暂停
前端·css·小程序
花千树-01011 小时前
Charles 安装与使用详解:实现 App 与小程序 HTTPS 抓包
网络协议·小程序·https
菜冬眠。11 小时前
uni-app/微信小程序接入腾讯位置服务地图选点插件
前端·微信小程序·uni-app
CrawlerCracker15 小时前
小程序逆向|六六找房|请求头Authorization
javascript·爬虫·python·小程序·网络爬虫·js
依辰16 小时前
可观测性升级:小程序错误监控体系实践
前端·javascript·微信小程序
wangdao121216 小时前
通过微信APPID获取小程序名称
微信·小程序
小兔崽子去哪了17 小时前
微信小程序入门
前端·vue.js·微信小程序