`
zha_zi
  • 浏览: 585201 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

CSS Box Model

    博客分类:
  • css
 
阅读更多

The CSS Box Model

 

Your understanding of the box model concept, and how it relates to the way in which an element’s final dimensions are determined, will be essential to your understanding of how an element is positioned on a web page. The box model applies to block-level elements. A companion concept, the inline layout model, defines how inline elements are positioned, and is covered in Inline Formatting.

Calculating Box Dimensions

In CSS2.1, block-level elements can only be rectangular. We calculate the overall dimensions of a block-level element by taking into account the height and width of the content area, as well as any margins, padding, and borders that are applied to the element.

We can define the content width of an element by declaring its width and heightproperties. If no declarations are applied, the default value for the width and heightproperties is auto.

For static (non-positioned) elements, and relatively positioned elements where thewidth property has the value auto, the computed width will be the width of the containing block minus any horizontal margins, borders, padding, and scrollbars. That is, it will be whatever’s left over when horizontal margins, borders, padding, and scrollbars (if any) have been deducted from the width of the containing block.

The containing block is the reference rectangle whose position and dimensions are used for relative calculations of descendant elements’ positions and dimensions. Although elements are positioned with respect to their containing block, they’re not confined by it, and they may overflow. In most cases, generated boxes act as containing blocks for descendant boxes. The full details of containing blocks are covered in Containing Block.

For floated or absolutely positioned elements (including elements for which positionis set to fixed), a width of auto will make the generated box shrink to the intrinsic dimensions of its contents.

Note:Floated Elements and Width

Previously, in CSS2, floated elements without a declared width value would not shrink to wrap their content; instead, they’d expand to the full width of their parent element. This behavior was changed in CSS2.1 to allow the shrink-wrapping to take place. However, in Internet Explorer 6 and earlier versions, a floated element with no declared width value will shrink to wrap its content as per the specifications unless a child element has a layout, in which case the floated parent will expand to fill the available content width of the parent.1

It should also be noted that when a float (without a declared width) contains a right-floated child element, it will also expand to fill the parent’s available content width in IE browsers up to and including version 7 (Firefox up to and including version 2.0 also exhibits this bug but, the problem appears to have been fixed as of Firefox 3.0 Alpha 6).

Therefore, it’s always safer to specify an explicit value for the width of a floated element where possible, and thereby to avoid the buggy behavior described above. However, as long as you’re aware of the problems mentioned above, you’ll likely find that widthless floats can be useful in certain situations, such as fluid-width horizontal menus.

No matter how the content area is positioned, its height value will be equal to the content height if no values have been declared for height, or for min-height and max-height.

Therefore, to ascertain the total space required to place an element on the page, add the content area’s dimensions to any padding, borders, and margins that have been declared. Of course, an element may have no padding, border, or margins, in which case its dimensions will be dictated solely by its content.

If an element contains only floated or absolutely positioned elements, it will have no content at all, and its height will be zero. We’ll discuss this more in Floating and Clearing.

Implementing the Box Model

The box model is best demonstrated with a short example. The calculation we’ll use to ascertain the total space required to accommodate an element on the page (ignoring margin collapse for the time being—see below for more on this) will be as follows:

Total width = left margin + left border + left padding + width +
                   right padding + right border + right margin

Total height = top margin + top border + top padding + height +
                   bottom padding + bottom border + bottom margin

Here’s our example CSS—a rule set that contains declarations for all the box properties of an element that has the class "box":

.box {
  width: 300px;
  height: 200px;
  padding: 10px;
  border: 1px solid #000;
  margin: 15px;
}

The total size of the element above will be calculated as follows:

Total width = 15 + 1 + 10 + 300 + 10 + 1 + 15 = 352px
Total height = 15 + 1 + 10 + 200 + 10 + 1 + 15 = 252px

The above calculation is depicted in Figure 1, which is taken from the element layout display from Firebug, the JavaScript and CSS development add-on for Firefox.

Figure 1. The CSS box model in actionA diagram displaying the width of a box and how it is
          calculated in CSS. The interior dimensions are added to the padding,
          border and margin widths.

In Figure 1, we can clearly see the content area in the center, the padding around the content area, the border area, and the margin area. The outer edge of the content area is called the content edge or inner edge; the outer edge of the padding area is called the padding edge; the outer edge of the border area is called the border edge; and the outer edge of the margin area is called—you guessed it—the margin edge or outer edge.

You can see from this short example that, for this element to fit on the page, we’ll need a space that’s at least 352px wide and 252px high. If the space available is any smaller than this, the element will be misplaced, or will overflow its containing block. Note that Internet Explorer 6 and earlier versions will most likely stretch the containing block to accommodate this extra height, and could severely disrupt the layout. Other browsers will let the element overflow its boundaries, but will ignore the content.

Note:Watch Out for Collapsing Margins

Although margins are included in the above calculations for the total space required to place the element, note that vertically adjacent margins on static (non-positioned) elements would collapse into the bigger margin of the elements that are adjacent above and below. This means that the actual space required to place an element would not necessarily extend from the margin edges of elements existing on the page: only the biggest margin will apply, and the smaller margins will appear to overlap the bigger margins. See Collapsing Margins for the full details of this quite complicated subject.

Practical Considerations of the Box Model

An important point to note is that an element that has its width set to 100% (that is, 100% of the content width of its parent element) shouldn’t have any margins, padding, or borders applied, as this would make it far too big for the space that it sits in. This is often overlooked by authors and can severely disrupt a page’s layout, as content will either overflow or push elements wider than they should be.

The solution, in most cases, is to avoid adding a value for the property width(other than auto), and to apply the margins, padding, and borders only. The widthproperty of a static element will default to auto, and even with padding, borders, and margins added, it will still assume the full available content width.

Of course, this approach may not be feasible in some instances, such as cases where the element is not a static element, and requires the definition of a specific width value (as in the case of a floated element that doesn’t automatically expand to fill its parent). In these cases, you have two options.

If the available space is of a fixed width, you can simply add the value of each component together to ensure that it matches the available width. For example, if the available space is 500px wide, and you require an element to have 20px padding, simply set the width to 460px and the padding to 20px for that element (20 + 460 + 20 = 500). This solution assumes that the length values specified for the element’s box properties use the same unit of measurement, since you won’t be able to add together a mixture of units (200px + 10%, for example, makes no sense in this context).

When the available content space has an unknown width—as in the case of a fluid layout—this method can’t be used, as percentages and pixels can’t be added together. In this case, the solution is to declare a width of 100% for the element concerned, and to apply the padding, border, and margin values to a nested element instead. That nested element has no width declaration, and can display the required padding, borders, and margins without encroaching on the parent element.

Now that you have a clear understanding of the CSS box model, you should also make yourself familiar with what’s commonly called the Internet Explorer 5 box model.

分享到:
评论

相关推荐

    彻底弄懂CSS盒模型 Box Model

    才用例子全面介绍CSS盒模型 Box Model 为css学习找到一条捷径。

    详解CSS中的Box Model盒属性的使用

    主要介绍了详解CSS中的Box Model盒属性的使用,包括弹性盒子模型的相关参数,需要的朋友可以参考下

    24小时自学HTML和CSS

    HOUR 15: Understanding the CSS Box Modeland Positioning HOUR 16: Using CSS to Do More with Lists HOUR 17: Using CSS to Design Navigation HOUR 18: Using Mouse Actions to Modify HOUR 19: Creating Fixed ...

    CSS 盒模型(Box Model)的学习和理解

    CSS盒模型(Box Model)的学习和理解一直是学习DivCSS网页布局的基础内容。但有很多CSS初学者依然不够熟悉,虽然我们提供了很多相关的教程。大家可以多学习与思考。今天发布一篇一位CSS初学者对CSS盒模型(Box Model...

    Learn CSS in One Day and Learn It Well (Volume 2)

    What is the CSS box model? How to position and float your CSS boxes How to hide HTML content How to change the background of CSS boxes How to use the CSS color property to change colors How to modify ...

    css-box-model:获取有关Element的准确且名称明确CSS Box模型信息:package:

    // profit安装 # # yarnyarn add css-box-model# npmnpm install css-box-model --save 箱型作品保证金箱边距+边框+填充+内容边框边框+填充+内容填充盒填充+内容内容盒内容这是我们返回的BoxModel : export ty

    boxmodel:CSS盒模型编辑器界面构建器输入试用

    BoxModel-CSS盒子模型编辑器 BoxModel是一个jQuery插件,可帮助用户创建紧凑的表单输入,以编辑html表单中的box模型css。 它可以提交几个详细信息,例如单独的值以及可以在CSS块中使用的组合的优化单位。 它支持鼠标...

    解析CSS的box model盒模型及其内的子元素布局控制

    css的两种盒模型介绍 CSS中Box model是分为两种,第一种是W3C的标准模型,另一种是IE的传统模型,他们相同之处都是对元素计算尺寸的模型,具体说就是对元素的width,height,padding,border以及元素实际尺寸的计算...

    图文讲解CSS的Box Model盒模型中的边距

    盒模型在Web页面布局中很重要,而对边距的把控是掌握盒模型的关键,下面我们就来以图文讲解CSS的Box Model盒模型中的边距

    css-box-model

    css-box-model

    CSS-box-model

    CSS-box-model

    CSS-Box-Model

    CSS-Box-Model

    HTML和CSS的关键:盒子模型(Box model)

    理解Box model的关键便是margin和padding属性, 而正确理解这两个属性也是学习用css布局的关键. 注: 为什么不翻译margin和padding? 原因一, 在汉语中并没有与之相对应的词语; 原因二: 即使有这样的词语, 由于在编写...

    解决CSS 中box-sizing与background-clip解决背景显示范围的问题

    过去在学习CSS的时候,首要任务就是要理解“box model”,因为box model是CSS里头很重要的模型概念,描述了padding、margin、border与content的空间定位,今天的项目竟然卡在一个简单的小问题,因此就用一篇文章做个...

    css-box-model-document-flow

    css-box-model-document-flow

    F2E-Course:每周课程的例子

    课程进度表wk1:课程简介与起手势wk2:HTML5 + CSS Box model wk3:CSS3 wk4:CSS3+Javascript wk5:版面设计与视觉设计wk6:jQuery wk7:网站行销与Landing Page制作wk8:RWD设计wk9:期中专题DEMO wk10:BootStrap+案例设计...

    css3.0参考手册 CHM

    attribute selectors Basic box model overflow-x , overflow-y Generated Content content Other modules media queries multi-column layout Web fonts speech Copyright © 2009 Tencent ISD webteam. All ...

Global site tag (gtag.js) - Google Analytics