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

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

初始显示

.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>

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

元素居中

水平居中关键代码: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>

元素水平两端对齐

关键代码: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>

 

 元素竖直两端对齐

关键代码: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>

元素左对齐

关键代码: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>

 

元素右对齐

关键代码: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>

 

元素上对齐

关键代码: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>

 

元素下对齐

关键代码: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>

文字居中对齐

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

关键代码: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>

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

更多内容欢迎关注博主。

有用的话欢迎打赏。

相关推荐

  1. el-row中元素如何上下居中对齐

    2024-01-26 09:14:01       35 阅读
  2. 服务系统设计:横向扩展纵向扩展的对比

    2024-01-26 09:14:01       56 阅读
  3. ppt图片居中对齐

    2024-01-26 09:14:01       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-26 09:14:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-26 09:14:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-26 09:14:01       87 阅读
  4. Python语言-面向对象

    2024-01-26 09:14:01       96 阅读

热门阅读

  1. API设计模式:REST、GraphQL、gRPC与tRPC全面解析

    2024-01-26 09:14:01       46 阅读
  2. PyQt中的信号/槽以及纯python实现信号/槽设计模式

    2024-01-26 09:14:01       60 阅读
  3. 五、MySQL的备份及恢复

    2024-01-26 09:14:01       49 阅读
  4. uniapp 框架搭建及使用

    2024-01-26 09:14:01       42 阅读
  5. vue和react的运行机制

    2024-01-26 09:14:01       51 阅读
  6. 如何用GPT制作技术路线图?

    2024-01-26 09:14:01       46 阅读