margin/padding-top/bottom 的百分比

本文解释了CSS中margin/padding-top/bottom的百分比为何基于width而非height计算的原因,涉及排版需求、文字书写方向的影响及传统百分比布局的局限性,并介绍了CSS3引入的vw/vh单位如何解决传统布局的问题。
margin/padding-top/bottom 的百分比之所以按照 width 计算,其实理由很简单,就是要匹配主要的 use cases。那就是——要构建在纵横两个方向上相同的 margin/padding。如果两个百分比的相对方式不同,那用百分比就无法得到垂直和水平一致的留白。

有人也许会问,为什么不是垂 直方向上的 height 而是水平方向的 width?这其实容易理解。因为 CSS 的基本模型是着重于“排版”的需求,因此水平和垂直方向其实并不是同等权重的,更精确的说,是文字书写方向决定的。常见的横排文字时,我们排版的出发点是 水平宽度一定,而垂直方向上是可以无限延展的。竖排文字则相反。所以在竖排文字时,margin/padding-* 其实就都按照 height 而不是 width 计算了。类似的且大家更熟悉的是 margin-top/bottom 在垂直方向上的 collapse(或者当竖排文字时是 margin-left/right 水平方向上的 collapse)。为什么只有垂直方向 collapse 而水平就不呢?因为在典型的排版中,段落间的空白进行 collapse 是常见和方便的。而反过来水平方向上就几乎没有那样的需求(只有表格在两个方向上有对称的 border collapse 的需求)。同样的,在排版中,横向百分比控制是常见的需求,但是纵向其实很少这样的需求。有这样需求的其实是GUI界面布局,布局和排版的区别在这个答案 ( 在 CSS 布局中,用 float 好还是用 position 好?分别有什么优势? )我也提到过。

至于说死循环问题,其实这不是根本原因。大家可以想想 width 为什么就不存在死循环了呢?

BTW, 在现代web应用中,其实我们有更多的纵向横向布局分割需求,这是传统的百分比不能很好满足的,所以CSS3实际上加入了 vw 和 vh 单位(相对于viewport的宽度和长度百分比),这比较好的解决了传统CSS中margin/padding的百分比所不能满足的那些需求。
<template> <z-paging> <view class="content" style="position: relative; background-image: url(https://mp-c50f1a3d-000e-4128-b376-c193bd43ced6.cdn.bspapp.com/Lottery.png);"> <view class="" style="padding-bottom: 40%;"> </view> <view class="flex" style=" height: 113vw;"> <view class="w-94" style="z-index: 1;" v-if="!show"> <LuckyWheel ref="myLucky" width="600rpx" height="600rpx" :blocks="blocks" :prizes="prizes" :buttons="buttons" :defaultStyle="defaultStyle" @start="startCallBack" @end="endCallBack" /> </view> </view> <view class="turnplate-bottom " style="background-image: url(https://mp-c50f1a3d-000e-4128-b376-c193bd43ced6.cdn.bspapp.com/turnplate-bottom.png);"> <view class="text-white" style="position: absolute; bottom: 39%; left: 29%;"> Resterende lotteritang 1 </view> </view> <view class="myPrize flex text-white align-center padding-left-sm" @click="myprize"> Mit præmie </view> <view class="padding-lr" style="position:absolute; bottom: 0vh;"> <view class="margin-bottom-sm" style="height: 240rpx; width: 694rpx; background-color: #FFF2D5; border-radius: 10rpx;"> <view class="flex align-center justify-center "> <view class="winningList flex align-center justify-center margin-top-sm"> Vinderliste </view> </view> <scroll-view scroll-y style="height: 130rpx;"> <view class="padding-lr margin-top-sm text-sm flex justify-between align-center" v-for="index in prizeList" key="index" style=" color: #5A3226;"> <view class=""> {{index.time}} </view> <view class=""> {{index.name}} </view> <view class="" style=""> {{index.prize}} </view> </view> </scroll-view> </view> <view class="" style="height: 540 rpx; width: 694rpx; background-color: #FFF2D5; border-radius: 10rpx;"> <view class="flex align-center justify-center "> <view class="winningList flex align-center justify-center margin-top-sm"> Aktivitetsregler </view> </view> <view class="padding text-sm" style=" color: #5A3226;"> <view class="" v-for="item in activityRulesList" :key="item._id"> <u-parse :html="item.content"></u-parse> </view> </view> <view class="flex align-center justify-center "> <view class="winningList flex align-center justify-center"> Præmieuddelingsregler </view> </view> <view class="padding text-sm" style=" color: #5A3226;"> <view class="" v-for="item in redemptionRules" :key="item_id"> <u-parse :html="item.content"></u-parse> </view> </view> </view> </view> </view> <view> <u-popup v-model="show" mode="center" border-radius="10" style="z-index: 2;"> <view class="" style="background-color: #FFF2D5; border-radius: 10rpx; width: 690rpx; height: 580rpx;"> <view class="flex align-center justify-center "> <view class="pop-bgimg flex align-center justify-center margin-top-sm"> — Leveringsadresse — </view> </view> <view class="padding-lr"> <u-field v-model="mobile" label="Modtager" placeholder="Navn" input-align="right" placeholder-style="font-size:26rpx" style="color: #5A3226;"> </u-field> <u-field v-model="mobile" label="Mobilnummer" placeholder="Mobilnummer" input-align="right" placeholder-style="font-size:26rpx" style="color: #5A3226;"> </u-field> <u-field v-model="code" label="Beliggenhed" placeholder="Provins, by, distrikt, gade" input-align="right" placeholder-style="font-size:26rpx; color= #A6866B" style="color: #5A3226;"> </u-field> <u-field v-model="code" label="Detaljeret adresse" placeholder="Bynavne, landsbyen" input-align="right" placeholder-style="font-size:26rpx" style="color: #5A3226;"> </u-field> </view> <view class="flex justify-center align-center margin-top-lg"> <view class="sub-button flex justify-center align-center text-white" @click="startRotate" style="background-image: url('/static/button.png');"> Indsend </view> </view> </view> </u-popup> </view> </z-paging> </template> <style lang="scss" scoped> .content { height: 120vh; /* 改为vh单位确保全屏 */ background-size: 100% 100%; /* 背景图片完全覆盖 */ background-position: center; .winningList { width: 360rpx; height: 60rpx; background-image: url("https://mp-c50f1a3d-000e-4128-b376-c193bd43ced6.cdn.bspapp.com/titlebg.png"); background-repeat: no-repeat; background-size: 100%; } .turnplate { height: 38vh; width: 100vw; min-height: 623rpx; position: absolute; // top: 19vh; z-index: 1; background-position: center; background-repeat: no-repeat; } .click { position: absolute; left: 39vw; top: 32vh; width: 22vmin; height: 25vmin; background-size: contain; background-position: center; background-repeat: no-repeat; z-index: 1; } .turnplate-bottom { position: absolute; height: 24vh; width: 100vw; bottom: 50vh; left: 0; background-size: 46vh; background-repeat: no-repeat; } .myPrize { position: absolute; bottom: 44%; right: -4%; background-image: url("https://mp-c50f1a3d-000e-4128-b376-c193bd43ced6.cdn.bspapp.com/myprize.png"); background-repeat: no-repeat; width: 25vw; height: 6vw; z-index: 1; } } .background-image { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: -1; /* 确保背景在内容下方 */ } .pop-bgimg { width: 350rpx; height: 50rpx; background-image: url("https://mp-c50f1a3d-000e-4128-b376-c193bd43ced6.cdn.bspapp.com/titlebg.png"); background-repeat: no-repeat; background-size: 100%; } .sub-button { width: 300rpx; height: 80rpx; background-repeat: no-repeat; background-size: 100%; } </style> 怎么解决在h5端页面混乱的问题,
09-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值