css自适应布局,缩放保持图片比例

场景一

适合商品类展示型布局,后台的数据呈现列表数据返回,窗口宽度变化,图片比例不变.假设我们的图片宽高固定比例16:9。

效果图满屏:在这里插入图片描述
效果图缩放时:
在这里插入图片描述
移动端:
在这里插入图片描述

直接看代码。里面会注释详细说明

<body>
  <!-- 多嵌套一层div.因为margin外边距设置为百分比%的话,它的基准是上一层dom的宽度 -->
  <div class="parentBox">
    <div class="parent">
    
      <div class="container">
        <img src="https://img0.baidu.com/it/u=399951341,1287964223&fm=253&fmt=auto&app=138&f=JPEG?w=994&h=500" alt="">
      </div>
      
      <div>
        <div ><span class="textOver">我是块级别3我是块级别3</span></div>
        <div><span class="textOver">我是块级别3我是块级别3</span></div>
        <div><span class="textOver">我是块级别3我是块级别3</span></div>
      </div>
    </div>
  </div>
</body>  


<style>
.parentBox{
    display: flex;
    flex-wrap: wrap;
 }
 .parent {
    width: 30%;/*这里想一行排三个。设置成了30%*/
    border-radius: 5px;
    margin-bottom: calc(10% / 2);/*间隙和水平保持一致*/
    background-color: #ffffff;
 }
 .parent:not(:nth-child(3n)) {
    margin-right: calc(10% / 2);/*剩下10%。为两个间隙平分*/
    
 }
 .container {
    width: 100%;
    padding-top: 56.25%; /*这里的百分比是上层(parent )的宽度为标准,这也是多嵌套一层原因*/
    /* 前提是图片宽高比例是16:9,所以这里是9/16。如果不理解,就记着反过来算 */
    position: relative;
 }
/*图片这里就要定位+padding-top或者padding-bottom*/
 .container img {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
 }
 .textOver{
    /* 文字溢出省略 */
    display: -webkit-box!important;
    -webkit-box-orient: vertical!important;
    -webkit-line-clamp: 1!important;
    overflow: hidden!important;
 }
</style>

补充:css属性aspect-ratio: 16/9;(响应式网页设计中,保持一致的纵横比) 可以直接达到这种效果;不过兼容性不太好;如果项目没有兼容性要求;可以用

场景二:一行固定显示的。适用场景:比如移动端个人中心功能模块排列时。基于flex:1实现。始终占满布局

效果图在这里插入图片描述

<body>
<!-- 一行固定元素个数 -->
  <div class="blockBox">
    <div class="blockItem">
      <img class="blockImg" src="https://img0.baidu.com/it/u=1089844382,2208108806&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500" alt="">
      <div class="textCenter">标题1</div>
    </div>
    <div class="blockItem">
      <img class="blockImg" src="https://img0.baidu.com/it/u=1089844382,2208108806&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500" alt="">
      <div class="textCenter">标题2</div>
    </div>
    <div class="blockItem">
      <img class="blockImg" src="https://img0.baidu.com/it/u=1089844382,2208108806&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500" alt="">
      <div class="textCenter">标题3</div>
    </div>
  </div>
</body>

<style>
.blockBox{
    display: flex;
  }
 .blockItem{
    flex: 1;
  }
 .blockImg{
    display: block;
    margin: 0 auto;
  }
 .textCenter{
    text-align: center;
  }
</style>

场景三:h5自适应布局,非弹性布局,根据窗口宽度自适应变化

![在这里插入图片描述](https://img-blog.csdnimg.cn/b36d7604636d401a9dd5ea63e48407d0.png在这里插入图片描述

先看布局



					<view v-if="proList.length>0" class="main_row2_list">
						<!-- 商品item -->
						<view @click="goProDetail" :data-proId='item.id' class="pro_item" v-for="(item,i) in proList"
							:key="i">
							<view>
								<image class="pro_img" :src="item.mainImg" mode="aspectFill"></image>
							</view>
							<view class="pro_name dark-grey fs-base text_clamp1">{{item.name}}</view>
							<view class="pro_spec mid-grey fs-base">规格:{{item.specifications}}</view>
							<view class="main-color">
								<label class="pro_price_unit fs-sm"></label>
								<label class="pro_price fs-lg">{{item.retailPrice}}</label>
							</view>
						</view>
						<!-- 商品item end-->
					</view>

		<style scoped lang="scss">
		.main_row2_list {
			min-height: 0;
			margin: 0 12px;

			.pro_item {
				//商品宽度变量 ;     24px 父级的左右marin之和  /2 一行摆俩  6px单个元素的间隔  16px单个元素的左右padding之和
				$pro_item_with: calc(((100vw - 24px) / 2) - 6px - 16px);
				// 变为行内块元素,也是不使用弹性布局的关键 
				display: inline-block;
				width: $pro_item_with;
				margin-bottom: 12px;
				min-height: 0px;
				padding: 8px;
				border-radius: 8px;
				background-color: #FFFFFF;

				// 图片
				.pro_img {
					width: $pro_item_with;
					height: $pro_item_with;
					border-radius: 8px;
					overflow: hidden;
				}

				// 名称
				.pro_name {
					line-height: 40rpx;
					margin: 4px 0;
				}

				//规格
				.pro_spec {
					line-height: 48rpx;
				}

				// 价格单位
				.pro_price_unit {
					line-height: 40rpx;
				}

				//价格
				.pro_price {
					line-height: 40rpx;
				}
			}
				//odd第奇数个元素加右边距
			.pro_item:nth-child(odd) {
				margin-right: 6px;
			}
				//even 第偶数数个元素加左边距
			.pro_item:nth-child(even) {
				margin-left: 6px;
			}
		}
		</style>


有问题欢迎讨论留言哦,虽然css简单。但是要精通还要多研究
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一行代码上晴天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值