CSS外边距叠加问题

2024-05-24 05:48

本文主要是介绍CSS外边距叠加问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转自:http://www.cnblogs.com/winter-cn/archive/2012/11/16/2772562.html

读过李松峰老师翻译的新书中《CSS设计师指南(第3版》的外边距叠加部分( http://www.ituring.com.cn/article/16969)实在是有些捉急啊,这地方实在是有些没写到位,也有错误(如“叠加的只是垂直外边距,水平外边距不叠加”)

margin collapsing现象

BFC(block formatting context)中相邻的两个block-level的盒,上一个box的下边距会跟下一个box的上边距发生叠加,即两者取最大的,而不是相加:

<!doctype HTML>
<html>
<head>
<style type="text/css">
#green {margin:10px 10px 10px 10px}#blue {margin:10px 10px 10px 10px}#red {margin:10px 10px 10px 10px}

</style>
</head>
<body>

<div id="green" style="background:lightgreen;height:100px;width:100px;"></div>
<div id="blue" style="background:lightblue;height:100px;width:100px;"></div>
<div id="red" style="background:pink;height:100px;width:100px;"></div>

</body>
</html>

margin collapsing与方向无关

《CSS设计师指南(第3版》中讲到“叠加的只是垂直外边距,水平外边距不叠加”,这个说法是错误的,叠加与否完全取决于两个box是否是位于同一BFC的两个相邻box,与水平垂直无关,下面例子中,更改了body的writing-mode,于是叠加发生在了水平方向(仅ie可看)

<!doctype HTML>
<html>
<head>
<style type="text/css">
#green {margin:10px 10px 10px 10px}#blue {margin:10px 10px 10px 10px}#red {margin:10px 10px 10px 10px}
body {
writing-mode:tb-rl;
}
</style> </head> <body> <div id="green" style="background:lightgreen;height:100px;width:100px;"></div> <div id="blue" style="background:lightblue;height:100px;width:100px;"></div> <div id="red" style="background:pink;height:100px;width:100px;"></div> </body> </html>

margin collapsing仅仅发生在BFC

margin collapsing不发生在IFC

当把元素的display属性设为inline-block,它们都位于IFC当中,所以不论水平还是竖直方向,都不会发生叠加:

<!doctype HTML>
<html>
<head>
<style type="text/css">
#green {margin:10px 10px 10px 10px;
display:inline-block;}#blue {margin:10px 10px 10px 10px;
display:inline-block; }#red {margin:10px 10px 10px 10px;
display:inline-block;}
</style> </head> <body> <div id="green" style="background:lightgreen;height:100px;width:100px;"></div><div id="blue" style="background:lightblue;height:100px;width:100px;"></div><br/><div id="red" style="background:pink;height:100px;width:100px;"></div> </body> </html>
margin collapsing不发生在floats

当把元素的float属性设为left,它们都不在正常流中,更不在BFC中了,同样不论水平还是竖直方向,都不会发生叠加:

<!doctype HTML>
<html>
<head>
<style type="text/css">
#green {margin:10px 10px 10px 10px;
float:left;}#blue {margin:10px 10px 10px 10px;
float:left; }#red {margin:10px 10px 10px 10px;
clear:left;
float:left;}
</style> </head> <body> <div id="green" style="background:lightgreen;height:100px;width:100px;"></div> <div id="blue" style="background:lightblue;height:100px;width:100px;"></div>
<div id="red" style="background:pink;height:100px;width:100px;"></div>
</body> </html>

margin collapsing可能跨元素

当一个容器既没有border又没有padding时,它的第一个子box的的margin也能跟它的上一个容器的margin发生叠加:

<!doctype HTML>
<html>
<head>
<style type="text/css">
#green {margin:10px 10px 10px 10px;
}#blue {margin:5px 10px 10px 10px;
}#red {margin:10px 10px 10px 10px;
}
</style> </head> <body> <div id="green" style="background:lightgreen;height:100px;width:100px;"> </div> <div id="blue" style="background:lightblue;height:100px;width:100px;">
<div id="red" style="background:pink;height:100px;width:100px;"></div>
</div>
</body> </html>

margin collapsing不能跟padding(内边距)折叠

有padding(内边距)的时候,上一节的情况不发生:

<!doctype HTML>
<html>
<head>
<style type="text/css">
#green {margin:10px 10px 10px 10px;
}#blue {margin:10px 10px 10px 10px;
padding:10px 10px 10px 10px;
}#red {margin:10px 10px 10px 10px;
}
</style> </head> <body> <div id="green" style="background:lightgreen;height:100px;width:100px;"> </div> <div id="blue" style="background:lightblue;height:100px;width:100px;">
<div id="red" style="background:pink;height:100px;width:100px;"></div>
</div>
</body> </html>

 

margin collapsing不能跟跨越BFC

当一个容器在其内部创建新的BFC时,跨元素折叠的情况也不会发生。

<!doctype HTML>
<html>
<head>
<style type="text/css">
#green {margin:10px 10px 10px 10px;
}#blue {margin:10px 10px 10px 10px;
overflow:hidden;
}#red {margin:10px 10px 10px 10px;
}
</style> </head> <body> <div id="green" style="background:lightgreen;height:100px;width:100px;"> </div> <div id="blue" style="background:lightblue;height:100px;width:100px;">
<div id="red" style="background:pink;height:100px;width:100px;"></div>
</div>
</body>

这篇关于CSS外边距叠加问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/997544

相关文章

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、