小程序image组件中居中问题

  • • 发表于 8年前
  • • 作者 十方
  • • 9569 人浏览
  • • 3 条评论
  • • 最后编辑时间 8年前
  • • 来自 [问 答]

原创声明:本文为作者原创,未经允许不得转载,经授权转载需注明作者和出处

image组件中镶嵌在view组件中如何实现image组件水平居中
代码如下:
wxml如下:

  1. <view class="infoLeft">
  2. <view class="leftTop">Content</view>
  3. <view class="leftBottom">
  4. <text>测试</text>
  5. <view class="leftBottomIn">
  6. <image src="../images/cs/3037.png" mode="aspectFit" ></image>
  7. </view>
  8. </view>
  9. </view>

wxss如下:

  1. .infoLeft{
  2. width: 374rpx;
  3. background-color: #faf7f7;
  4. border-right: 2rpx solid white;
  5. border-bottom: 2rpx solid white;
  6. }
  7. .leftTop{
  8. width: 100%;
  9. height: 80rpx;
  10. background-color: slategray;
  11. }
  12. .leftBottom{
  13. display: flex;
  14. flex-direction: row;
  15. width: 100%;
  16. align-items: center;
  17. }
  18. .leftBottom text{
  19. width: 50%;
  20. padding-left:10rpx;
  21. background-color: goldenrod;
  22. }
  23. .leftBottom .leftBottomIn{
  24. width: 50%;
  25. align-items: center;
  26. }
  27. .leftBottomIn image{
  28. width: 80%;
  29. /*margin-left:5%;*/
  30. /*background-color: greenyellow;*/
  31. }

效果如下:

`

分享到:

十方
十方 2016-11-20 21:24:25.0

没办法,后面只能用margin padding解决。

十方
十方 2016-11-29 13:18:19.0

找到原因了,原理,flex流布局,加水平居中和垂直居中,另外image控件要设置宽和高。

  1. .more{
  2. display: flex;
  3. flex-direction: row;
  4. margin-top: 6rpx;
  5. justify-content: center; //水平居中
  6. align-items: center; //垂直居中
  7. background-color: white;
  8. }
  9. .more image{
  10. width: 80rpx;
  11. height: 50rpx;
  12. }
Aguy
Aguy 2016-12-04 22:33:08.0

xiexie~

3条评论
我来说两句...
x
 
Ctrl+Enter
作者

十方

十方

APP:1 帖子:4 回复:46 积分:97

已加入社区[3082]天

主人太懒,签名没设置!

作者详情》