margin-top在firefox失效的原因及解决办法

本文详细介绍了CSS2.1规范中关于垂直margin折叠的规则及其应用场景,包括浮动元素、绝对定位元素等特殊情况下的处理方式,并通过实例展示了在不同浏览器中的表现差异。

为什么要翻译这篇说明?css2本有人已翻译过,但看一下,很粗糙(不是说自己就怎么怎么样啊,翻译者真的是很值得敬佩的!),近来跟css与xhtml接触得越来越多,但接触得越多,迷惑却总不见少。

现在我觉得很多问题根本不能称之为问题,原因就在于我们的草率理解,比如杀鸡用牛刀,不是不可以,是不合理、不恰当,根源错了,表象也就会错了,如果解决问题从表象入手,难免总会摸不着头脑,还是那句话,要脚踏实地,切莫浮躁。

在这个说明中,“collapsing margins”(折叠margin)的意思是:2个或以上盒模型之间(关系可以是相邻或嵌套)相邻的margin属性(这之间不能有非空内容、 padding区域、border边框或使用清除分离方法)结合表示为一个单独的margin。

在css2.1中,水平的margin不会被折叠。

垂直margin可能在一些盒模型中被折叠:

1、在常规文档流中,2个或以上的块级盒模型相邻的垂直margin会被折叠。
最终的margin值计算方法如下:
a、全部都为正值,取最大者;
b、不全是正值,则都取绝对值,然后用正值减去最大值;
c、没有正值,则都取绝对值,然后用0减去最大值。
注意:相邻的盒模型可能由DOM元素动态产生并没有相邻或继承关系。


2、相邻的和模型中,如果其中的一个是浮动的(floated),垂直margin不会被折叠,甚至一个浮动的盒模型和它的子元素之间也是这样。


3、设置了overflow属性的元素和它的子元素之间的margin不会被折叠(overflow取值为visible除外)。


4、设置了绝对定位(position:absolute)的盒模型,垂直margin不会被折叠,甚至和他们的子元素之间也是一样。


5、设置了display:inline-block的元素,垂直margin不会被折叠,甚至和他们的子元素之间也是一样。


6、如果一个盒模型的上下margin相邻,这时它的margin可能折叠覆盖(collapse through)它。在这种情况下,元素的位置(position)取决于它的相邻元素的margin是否被折叠。

a、如果元素的margin和它的父元素的margin-top折叠在一起,盒模型border-top的边界定义和它的父元素相同。
b、另外,任意元素的父元素不参与margin的折叠,或者说只有父元素的margin-bottom是参与计算的。如果元素的border-top非零,那么元素的border-top边界位置和原来一样。

一个应用了清除操作的元素的margin-top绝不会和它的块级父元素的margin-bottom折叠。
注意,那些已经被折叠覆盖的元素的位置对其他已经被折叠的元素的位置没有任何影响;只有在对这些元素的子元素布局时,border-top边界位置才是必需的。


7、根元素的垂直margin不会被折叠。


浮动的块级元素的margin-bottom总是与它后面的浮动块级兄弟元素(floated next in-flow block-level sibling)的margin-top相邻,除非那个同级元素使用了清除操作。

浮动的块级元素的margin-top和它的第一个浮动块级子元素(floated first in-flow block-level child)的margin-top相邻(如果该元素没有border-top,没有padding-top,并且子元素没有使用清除操作)。

浮动的块级元素的margin-bottom如果符合下列条件,那么它和它的最后一个浮动块级子元素的margin-bottom相邻(如果该元素没有指定padding-bottom或border):


a、指定了height:auto
b、min-height小于元素的实际使用高度(height)
c、max-height大于元素的实际使用高度(height)

如果一个元素的min-height属性设置为0,那么它所拥有的margin是相邻的,并且它既没有border-top和border- bottom,也没有padding-top和padding-bottom,它的height属性可以是0或auto,它不能包含一个内联的盒模型 (line box),它所有的浮动子元素(如果有的话)的margin也都是相邻的。

当一个元素拥有的margin折叠了,并且它使用了清除操作,那么它的margin-top会和紧随其后的兄弟元素的相邻margin折叠,但结果是它的margin将无法和其块级父元素的margin-bottom折叠。

折叠操作是以padding、margin、border的值为基础的(即在浏览器解析所有这些值之后),折叠后的margin计算将覆盖已使用的不同margin的值。

-------------------------------------------

<div id="d0" style="background-color:#333333;height:500px;">

<br style="line-height:0;"/>

//不加入br这一行,在FIREFOX中id1的margin-top:20px将会在d0上作用,使d0上方与body间有20px的间距,d1与d0的上方没有间距,而IE内正常显示

<div id="d1" style="background-color:#000000;margin-top:20px;height:100px;"></div>

<div id="d2" style="background-color:#000000;margin-top:20px;height:100px;"></div>

</div>


come from http://hi.baidu.com/jinglin178/blog/item/e8b8d417ec93a00cc93d6d95.html

<template> <div class="feedback boxBack"> <div class="bg-size"></div> <div class="bg-blur"></div> <div class="mian"> <div class="topbar"> <span class="text">意见反馈</span> </div> <!-- 表单 --> <div class="feedbackBox"> <!-- 浏览器 --> <div class="wrap"> <div class="item-label"> <icon-browser class="browserImg"></icon-browser> <span class="browser">浏览器</span> </div> <el-radio-group v-model="browserValue" class="radio-group"> <el-radio v-for="item in browserOptions" :key="item.id" :label="item.id" class="radio-item"> {{ item.name }} </el-radio> </el-radio-group> <el-input v-if="browserValue === 6" v-model="otherBrowser" class="resetInput" placeholder="请输入浏览器名称" style="margin-top: 10px; width: 200px;" /> </div> <div class="wrap"> <div class="item-label"> <icon-business class="browserImg"></icon-business> <span class="browser">业务</span> </div> <div class="header"> <el-radio-group v-model="businessValue" class="radio"> <el-radio v-for="item in businessOptions" :key="item.id" :label="item.id" class="radio-item"> {{ item.name }} </el-radio> </el-radio-group> </div> </div> <div class="wrap"> <div class="item-label"> <icon-suggestion class="browserImg"></icon-suggestion> <span class="browser">建议</span> </div> <div class="header"> <el-input v-model="suggestion" type="textarea" :rows="4" placeholder="请填写问题描述,10~500个字" class="text-input resetInput" /> </div> </div> <div class="wrap"> <div class="item-label"> <icon-attachment class="browserImg"></icon-attachment> <span class="browser">附件</span> </div> <div class="header"> <upload title="上传附件" :maxSize="10" action="/csc/feedback/upload" v-model="attachmentUrl" @uploadSuccess="handleAttachmentSuccess" @delete="handleAttachmentDelete" /> <div class="file-info"> <div>·类型:图片</div> <div>·格式:仅支持 jpg、gif、png、pdf、bmp格式</div> </div> </div> </div> <div class="wrap"> <div class="form-item"> <div class="item-label"> <icon-email class="browserImg"></icon-email> <span>邮箱</span> </div> <el-input v-model="email" placeholder="请填写邮箱,10~50个字" style="width: 80%;" class="text-input resetInput" /> </div> </div> <div class="button-group"> <el-button type="primary" class="submit-btn" @click="onSubmit">提交反馈</el-button> <el-button type="default" class="cancel-btn" @click="onCancel">取消</el-button> </div> </div> </div> </div> </template> <script setup> import { ref } from 'vue' import { useRouter } from 'vue-router' import { feedback } from '@/api/user' import { ElNotification } from 'element-plus' const otherBrowser = ref('') const router = useRouter() const suggestion = ref('') const radio = ref(3) const browserValue = ref(0) const businessValue = ref(0) const email = ref('') const browserOptions = [ { id: 0, name: 'Google Chrome', }, { id: 1, name: 'Mozilla Firefox', }, { id: 2, name: 'Apple Safari', }, { id: 3, name: '360浏览器', }, { id: 4, name: '夸克浏览器', }, { id: 5, name: 'QQ浏览器', }, { id: 6, name: '其他', }, ] const businessOptions = [ { id: 0, name: '我的账户', }, { id: 1, name: '快速开卡', }, { id: 2, name: '卡片列表', }, { id: 3, name: '持卡信息', }, { id: 4, name: '交易查询', }, { id: 5, name: '卡片资金', }, { id: 6, name: '充值记录', }, ] const onSubmit = () => { if (!suggestion.value.trim() || suggestion.value.length < 10 || suggestion.value.length > 500) { ElNotification.warning('请填写10~500字的问题描述'); return; } if (browserValue.value === 6 && !otherBrowser.value.trim()) { ElNotification.warning('请输入其他浏览器名称'); return; } if (email.value && !/^\S+@\S+\.\S+$/.test(email.value)) { ElNotification.warning('请输入正确的邮箱格式'); return; } let browserStr = ''; if (browserValue.value === 6) { browserStr = otherBrowser.value.trim(); } else { const selectedBrowser = browserOptions.find(item => item.id === browserValue.value); browserStr = selectedBrowser ? selectedBrowser.name : ''; } const selectedBusiness = businessOptions.find(item => item.id === businessValue.value); const businessStr = selectedBusiness ? selectedBusiness.name : ''; feedback({ page: '', size: '', id: '', status: '', isRead: '', browser: browserStr, business: businessStr, reviewer: '', startTime: '', endTime: '' }).then((res) => { ElNotification.log('提交反馈列表成功', response.data); router.go(-1); }).catch(error => { ElNotification.error('接口调用异常', error); }); }; const onCancel = () => { router.go(-1) } </script> <style scoped lang="scss"> .feedback { display: flex; flex-direction: column; height: 100%; border-radius: 0; padding: 34px 20px 70px 20px; position: relative; min-height: 100vh; .mian { display: flex; flex-direction: column; } .topbar { display: flex; justify-content: center; z-index: 100; .text { font-size: 32px; line-height: 42px; background: linear-gradient(90.56deg, #169AF4 0%, #CD17F3 100%); -webkit-background-clip: text; background-clip: text; color: transparent; font-weight: bold; } } .feedbackBox { width: 100%; flex-direction: column; height: 100%; padding: 0 20px 20px 20px; z-index: 100; .wrap { width: 100%; height: auto; background: linear-gradient(90.18deg, #655CB926 0%, #352F8314 100%); border: 1px solid #7948EA; border-radius: 12px; padding: 26px 0 30px 42px; margin-top: 22px; .item-label { display: flex; align-items: center; margin-bottom: 20px; color: #fff; font-size: 18px; .browserImg { color: #fff !important; width: 20px; height: 20px; margin-right: 4px; } } .radio-group { display: flex; flex-wrap: wrap; gap: 70px; padding: 5px 0; margin-left: 20px; } .resetInput { margin-left: 20px; /* 与单选框左侧对齐 */ } .radio { display: flex; flex-wrap: wrap; gap: 95px; padding: 5px 0; margin-left: 20px; } .radio-item { color: #fff; font-size: 16px; } &:hover { color: #d0c4ff; } .browser { color: #ffffff; font-size: 20px; line-height: 29px; } .header { display: flex; .upload-demo { width: 121px; height: 64px; } } .uploader { width: 160px; height: 64px; } .file-info { font-size: 14px; flex: 1; gap: 15px; margin-top: 135px; color: #fff; } } } .button-group { display: flex; width: 100%; margin-top: 60px; justify-content: center; .submit-btn { width: 120px; height: 40px; border-radius: 8px; background: linear-gradient(-96.34deg, #18C8FF 0%, #933FFE 100%); } .cancel-btn { width: 120px; height: 40px; border-radius: 8px; background: #E5E5E51E; border: 1px solid #A6A6A6; } } } .el-textarea__inner { width: 80%; } </style>这段代码为什么提交的时候出现报错的现象
最新发布
11-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值