Box properties

  1. content

  2. padding

  3. border

  4. margin

Note: Margins have a specific behavior called margin collapsing: When two boxes touch against one other, the distance between is the value of the value of the largest of the two touching margins, and not their sum.

Margin collapsing

Margin collapsing occurs in three basic cases.

Adjacent siblings(相邻兄弟)

The margins of adjacent(相邻的) siblings(兄弟) are collapsed(except when the later sibling needs to be cleared past floats).For example:

<p>The bottom margin of this paragraph is collapsed...</p>
<p>...with the top margin of this paragraph.</p>

Parent and first/last child

If there is no border, padding, inline content, block_formatting_context(BFC) created or dearance(清除) to seperate the margin-top of a block from the margin-top of its first child block, or no border, padding, inline content, height, min-height, or max-height to seperate the margin-bottom of a block from the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.

如果这里没有border, padding, 内联内容, BFC的创建或清除, 来将块的margin-top与其第一个子块的margin-top分开。或者没有border, padding, inline content, height, min-height, or max-height, 来将块的margin-bottom和最后一个子块的margin-bottom分开,那么这些margins会塌陷。塌陷的margin结束于父容器的外面。

Empty blocks

If there is no border, padding, inline content, height or min-height to sperate a block's margin-top from its margin-bottom, then its top and bottom margins collapse.

Active learning: playing with boxes

  1. 如果设置padding或者margin的宽度是百分比的话, 是相对于父容器的宽度(只是cotent,不包括padding和margin)。

  2. Borders ignore percentages width settings too, but you can use padding to simulate(模仿,假装).

  3. You should have noticed that the total width of a box is the sum of its width, padding-left, padding-right, boder-left, border-right properties. In some cases it annoying (for example, what if you want to have a box with a total width of 50% with border and padding expressed in pixels?) To avoid such problems, it's possible to tweak(用力拉) the box model with property box-sizing. With the value border-box, it changes the box model to this new one (width = content + border + padding; height = content + border + padding).

Advanced box manipulation (先进的盒子操作)

Overflow

When you set the size of a box with absolute value(e.g. a fixed pixel width/height), the content may not fit within the allowed size, in which case the content overflows the box. To control what happens in such cases, we can use the overflow property. It takes several possible values, but the most common are:

  1. auto: If there is too much content, the overflowing content is hidden and scroll bars are shown to let the user scroll to see all the content.

  2. hidden: If there is too much content, the overflowing content is hidden.

  3. visible: If there is too much content, the overflowing content is shown outside of the box(this is usually the default behavior.)

Background clip

Box backgrounds are made up of colors and images, stacked on top of each other(background-color, background-image.) They are applied to a box and drawn under that box.By default, backgrounds extend to the outer of the border.This is often fine, but in some cases it can be annoying (what if you have a tiled(平铺的) background image that you want to only extend to the edge of the content?) This behavior can be adjusted by setting background-clip property on the box.

  1. border-box(this is default)

  2. padding-box

  3. content-box

Outline

使用起来和border差不多,但是它不是盒模型的一部分。

Types of CSS boxes

Everything we're said so far applies to boxes that represented block level elements(块级元素). However, CSS has other types of boxes that behave differently. The type of box applied to an element is specified by the display property. There are many different values available for display, but in this article we will focus on the three most common ones ones;block, inline and inline-block.

  • A block box is defined as a box that's stacked upon other boxes(i.e. content before and after the box appears on a seperate line), and can have width and height set on it. The whole box model as described above applies to block boxes.(块级元素是这样的一个盒子,它堆放在其他盒子上面(补充说明:内容前后都有一个空行,也就是说,block独占一行), 可以设置宽度和高度。上面说的整个盒模型都适用于块级元素。)
  • An inline box is the opposite of a block box: it flows with the document's text(i.e. it will appear on the same line as surrounding text and other inline elements, and its content will break with the flow of the text, like lines of text in a paragraph.) Width and height settings have no effect on inline boxes;any padding, margin, border set on inline boxes will update the position of surrounding text, but will not affect the position of surrounding block boxes.(内联框与块框相反:它与文档的文本一起流动(即,它将与周围文本和其他内联元素显示在同一行上,其内容将与文本流断开,如文本行 在一个段落中。)宽度和高度设置对内联框没有影响; 在内嵌框中设置的任何填充,边距和边框将更新周围文本的位置,但不会影响周围块框的位置。

    )
  • An inline-block box is something in between the first two: it flows with surrounding text without creating line breaks before and after it like an inline box, but it can be sized using width and height and maintains its block integrity(完整) like a block box --- it won't be broken across paragraph lines.

更多的BFC、Visual formatting model

阅读MDN文档之基本盒模型(三)的更多相关文章

  1. 阅读MDN文档之CSS选择器介绍(一)

    本文为阅读MDN文档笔记 目录 Different types of Selectors Attribute Selectors Presence and value attribute select ...

  2. 阅读MDN文档之StylingBoxes(五)

    目录 BoxModelRecap Box properties Overflow Background clip Background origin Outline Advanced box prop ...

  3. HTML文档模式与盒模型

    HTML文档根据文档顶部的doctype声明来决定渲染模式,有标准模式(Standards Mode)与怪异模式(Quirks mode,或叫做混杂模式)两种模式. IE5及以前默认总是表现为怪异模式 ...

  4. 阅读MDN文档之布局(四)

    Introducing positioning Static positioning Relative positioning Introducing top, bottom, left and ri ...

  5. 阅读MDN文档之布局(四)

    Introducing positioning Static positioning Relative positioning Introducing top, bottom, left and ri ...

  6. 阅读MDN文档之层叠与继承(二)

    目录 The cascade Importance Specificity Source order A note on rule mixing Inheritance Controlling inh ...

  7. Emacs阅读chm文档

    .title { text-align: center; margin-bottom: .2em } .subtitle { text-align: center; font-size: medium ...

  8. 前端开发必备之MDN文档

    想下载MDN文档的看前面的内容就可以了. HTML 源码下载 MDN官方下载地址:https://developer.mozilla.org/media/developer.mozilla.org.t ...

  9. MDN 文档高级操作进阶教程

    MDN 文档高级操作进阶教程 MDN 文档, 如何优雅的使用 MDN 文档上的富文本编辑器 pre & 语法高亮器 code & note box source code 上传附件 i ...

随机推荐

  1. Numpy数组对象的操作-索引机制、切片和迭代方法

    前几篇博文我写了数组创建和数据运算,现在我们就来看一下数组对象的操作方法.使用索引和切片的方法选择元素,还有如何数组的迭代方法. 一.索引机制 1.一维数组 In [1]: a = np.arange ...

  2. openstack pike 集群高可用 安装 部署 目录汇总

    # openstack pike 集群高可用 安装部署#安装环境 centos 7 史上最详细的openstack pike版 部署文档欢迎经验分享,欢迎笔记分享欢迎留言,或加QQ群663105353 ...

  3. KVM 虚拟机 安装配置

    原创博文安装配置KVM http://www.cnblogs.com/elvi/p/7718574.htmlweb管理kvm http://www.cnblogs.com/elvi/p/7718582 ...

  4. JS 判断某个字符串是否存在与数组中

    <script> function in_array(stringToSearch, arrayToSearch) { for (s = 0; s < arrayToSearch.l ...

  5. 开源纯C#工控网关+组态软件(七)数据采集与归档

    一.   引子 在当前自动化.信息化.智能化的时代背景下,数据的作用日渐凸显.而工业发展到如今,科技含量和自动化水平均显著提高,但对数据的采集.利用才开始起步. 对工业企业而言,数据采集日益受到重视, ...

  6. sqlserver 查询所有表及记录行数

    --查询所有表名 select name from sysobjects where xtype='u' select * from sys.tables --查询所有表名及对应架构 select t ...

  7. js函数的作用域与this指向

    函数的作用域与this指向是js中很重要的一部分,理清这点东西需要个逻辑,看看我的逻辑怎么样... 下面是个提纲,可以直接挑你感兴趣的条目阅读. 函数的定义方式:直接定义(window下,内部定义), ...

  8. 【微服务】之六:轻松搞定SpringCloud微服务-API网关zuul

    通过前面几篇文章的介绍,我们可以轻松搭建起来微服务体系中比较重要的几个基础构建服务.那么,在本篇博文中,我们重点讲解一下,如何将所有微服务的API同意对外暴露,这个就设计API网关的概念. 本系列教程 ...

  9. 浅谈MySQL中的查询优化

    mysql的性能优化包罗甚广: 索引优化,查询优化,查询缓存,服务器设置优化,操作系统和硬件优化,应用层面优化(web服务器,缓存)等等.这里的记录的优化技巧更适用于开发人员,都是从网络上收集和自己整 ...

  10. 跨平台应用集成(在ASP.NET Core MVC 应用程序中集成 Microsoft Graph)

    作者:陈希章 发表于 2017年6月25日 谈一谈.NET 的跨平台 终于要写到这一篇了.跨平台的支持可以说是 Office 365 平台在设计伊始就考虑的目标.我在前面的文章已经提到过了,Micro ...