Zeal Blog - 泽欧里
梦想 就像鸡蛋
要么孵化 要么臭掉
WWW Zeal Blog

block-level elements的宽度计算

zeal 2006-12-27 00:00 于 WebDev , 2648 字 , Via |  + 0 - 0   English
Made In Zeal 转载请保留原始链接:http://www.zeali.net/entry/472
标签 ( Tags ):

对于 block-level 的页面元素(比如 DIV )来说,要让他相对于父级元素居中显示的话,标准的做法是为该元素定义的样式中包含 margin: auto; 属性。虽然在 IE 下我们使用 text-align: center; 就可得到居中的效果,但如果要考虑到不同浏览器之间的兼容,还是使用标准的方式比较好( text-align 理论上应该只是用来指定 block-level 元素里面所包含的每一行 inline-level 元素 -- 比如 SPAN -- 的对齐方式)。

来自 CSS3 的相关定义

For block-level elements with horizontal flow in a containing block also with horizontal flow, the computed values of the 'width' and margins must satisfy this constraint:

(width of containing block) =
margin-left + border-left + padding-left + width + padding-right + border-right + margin-right

The following cases can occur:

  1. None of width, margin-left and margin-right are specified as 'auto' and the values satisfy the constraint.
  2. None of width, margin-left or margin-right was specified as 'auto' and the equation is not satisfied. There are two sub-cases: (1) if the 'direction' of the element is 'ltr', the specified value of 'margin-right' is ignored and 'margin-right' is set to the value that makes the equation true; (2) if 'direction' is 'rtl', it is 'margin-left' that is ignored and computed from the equation.
  3. If exactly one of width, margin-left or margin-right is 'auto', its value is computed from the equation.
  4. If width and one or both margins are 'auto', the margins that are 'auto' are set to 0 and the equation is solved for width.
  5. If both margin-left and margin-right are 'auto', the equation is solved under the extra constraint that margin-left = margin-right.

If, after solving the equation, width has a value that is smaller than 'min-width', the computed value of 'width' is set to the computed value of 'min-width' and the constraint is evaluated again as if width had been specified with this value.

If, after solving the equation, width has a value that is larger than both 'max-width' and 'min-width', the computed value of 'width' is set to the larger of 'max-width' and 'min-width' and the constraint is evaluated again as if 'width' had been specified with this value.

Note: case 5 can be used to center block-level elements:

BLOCKQUOTE {
    width: 30em;
    margin-left: auto;
    margin-right: auto }

This is different from 'text-align: center', which centers each line inside the block, but not the block inside its parent.

Block-level elements with a vertical flow inside a containing block with a vertical flow are analogous, but with a constraint on height and margin-top/margin-bottom:

(height of containing block) =
margin-top + border-top + padding-top + height + padding-bottom + border-bottom + margin-bottom

This Entry was Last Modified on 2007-05-28 14:58
引用本文(TrackBack): 此功能无限期关闭ing ... 如有引用请在文中标明出处并提供超链接  
没有评论.
对于这篇日志您有什么想法?我想听听您的高见。
如果您不是没有思想的机器Spammer,请告诉我 4 + 8 =
然后任选以下两种方式之一告诉我您的身份:
1. 使用您的 OpenID
2. 使用传统留言格式
(必填) 名字
如何称呼您?
(选填) URL
在此填写您的 Blog / Homepage 的地址,我将非常乐于访问
(选填) Email
Email地址将在经过防SPAM处理后显示在页面。如果您不希望公开自己的Email,请略过此栏
并写下您的见解:
(由于需要进行SPAM过滤,如果评论提交速度慢请耐心等待)
记住我?

  如何进行评论                   当有新回复时给我发送邮件通知
除了 <b> 和 <i> 之外,其他的HTML标签都将从您的评论中去除. URL 或 Email 地址会被自动加上链接.
当您点击发表或者预览之后,系统会自动保存您此次评论的内容,以便当网络原因发表失败时直接按Ctrl-V重新粘贴上次评论内容。
注意:快捷键及自动拷贝功能仅对IE浏览器有效。
发贴统计

Tot 618/1354

日志存档
2008年
2007年
2006年
本 Blog 原创内容
遵循以下授权:

Creative Commons
(创作共用) CC

署名-非商业性
-相同方式分享
Zeal Blog 基于
Powered byPivot - 1.24.1: 'Arcee'
Pivot1.24.1
开发
Launched @
2005-04-27
 里,居也。——《说文》    里,邑也。——《尔雅》。李注:“居之邑也。”沪ICP备05024379号 Back To Home All Links Loading