先给home.wxml程序:
<view class='imagesize'> <image src="/images/2.png" class='in-image'></image> </view>
1.图片居中(屏幕顶部):
//.wxss里的参数
.imagesize{
display:flex; //flex布局
justify-content: center; //水平轴线居中
}
.imagesize image {
width:400rpx; height:400rpx;
}
上面设置的图片尺寸,仅仅是为了方便看到实际的效果。
2.图片居中(中部,位置可调 →height 和 align-items)
.imagesize{
display:flex; //flex布局高度
height: 600px;
justify-content: center;
align-items:center; //垂直居中
}
.imagesize image {
width:400rpx; height:400rpx;
}
上图的height值分别为: 200px 400px 600px
前两种都不适用全部型号手机,因为手机屏幕尺寸不固定。
但是,对于设计图片位置很有帮助。
3.图片居中(屏幕正中间)
page{
height:100% //满屏设置}
.imagesize{
display:flex;
height: 100%;
//设置布局满屏
justify-content: center;
align-items:center;
}
.imagesize image {
width:400rpx;
height:400rpx;
}
4.给出完整代码(之前的一篇也有完整代码,现给出的加到之前的文件夹下就行):
home.wxml
<view class='imagesize'> <image src="/images/2.png" class='in-image' ></image></view>
home.wxss
page{ height:100%}.imagesize{ display:flex; height: 100%; justify-content: center; align-items:center;}.imagesize image { width:400rpx; height:400rpx; }
5.铺满屏幕:
单独讲铺满屏幕,主要用到mode='widthFix'
具体加在的程序段是.wxml:
<image src="/images/img21.jpg" class='in-image' mode='widthFix'> </image>
以及.wxss的改变:
page{height:100%}.imagesize{ display:flex; height: 100%; justify-content: center; align-items:center;}