Table of Contents
  'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block
Home Web Front-end HTML Tutorial css 文档流中块级非替换元素水平区域的计算规则(1)_html/css_WEB-ITnose

css 文档流中块级非替换元素水平区域的计算规则(1)_html/css_WEB-ITnose

Jun 24, 2016 am 11:16 AM

  最近在读《Basic Visual Formatting in CSS》,结合以前看的《css权威指南》和css标准。今天就做个笔记。
  以前在遇到一些宽度不明确指明的一些布局的时候,虽然凭感觉能猜出个大概,但是总是有点不是很靠谱。直到最近看到这一本书,觉得总觉得挺好的。

  首先要知道,元素除了通过display来指定block、inline、inline-block这一些属性之外,还有一种能在的特性--替换还是非替换。

    替换元素:像img、video、canvas等稳定种指定的内容只是占位,真正的内容实在后面替换上去的。像img内容是图片加载完成之后替换上去的。
    非替换元素:像div、p、span这一些内容就写在文档中。
  而这里要说到的主要是块级非替换元素既display:block的非替换元素,如p、div;还有就是display:list-item,如li。li虽然看display像是和div、p这一些是两类,但是其实li除了多了前面的标志之外,别的行为基本都是一样的。

  下面进入正题,文档流中块级非替换元素的水平格式化和包含块(containing block)还有七个属性有关(border-left、margin-left、padding-left、width、padding-right、margin-right、border-right)。文档流中块级元素的containing block一般指的是父级块级元素的内容区域(content area)。也就是在box-sizing:content-box时,width指定的区域。下面有一条公式:

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

  上面的公式在所有情况下都要成立。但是很快问题就来了,要是我把7个属性的值都设了值,但是加起来不等于containing block的width怎么办;又或者我把值都设置为auto,那又怎么样去计算呢?其实在这七个值中,只有三个值margin-left、margin-right、width能够设置为auto。我们首先看看把这三个属性设置为auto的情况,由于这三个属性可以设置为auto或者是非auto,那么就是说一共有23,8种情况。下面来分别说下:

  1、一个值设置为auto,也就是margin-left、margin-right或者width设置为auto。这三种情况最简单了,直接套上面的公式一算就知道了。

  2、两个值设置为auto。

    2.1、margin-left和margin-right设置为auto,width指定一个非auto的值。这种情况就是我们平时的水平居中,margin-left和margin-right获得相同的值。

    2.2、margin-left和width设置为auto,margin-right指定一个非auto的值。margin-left重设为0,然后就变成了1情况了。

    2.3、margin-right和width设置为auto,margin-left指定一个非auto的值。margin-right重设为0,然后又变成了1情况了。

  3、三个值设置为auto。margin-left和margin-right都重设为0,然后根据1来计算。

  4、假如没有一个设置为auto,在css中叫"over-constrained"(过度约束)。那么在文档流方向为左到右的时候margin-right的值会被忽略,然后还是1情况。这种情况最容易理解错,容易吧"over-constrained"理解成指定了所有的值后使得上面公式左边小于右边。正确的理解应该是"指定了三个值,都没有变通的余地了"。所以只要给margin-left、width和margin-right都指定了auto之外的值(就算是0),也是"over-constrained"了。

 

  上面的内容似乎已经覆盖了所有的情况了,其实不然,还有一种特殊情况就是margin-left,margin-right设置为auto,width有指定值,但是border-left+padding-left+width+padding-right+border-right> width of containing block。按照上面的说法,margin-left和margin-right获得相同的值,但事实是,margin-left和margin-right重设为0,然后过度约束,然后忽略掉一个。

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1258
29
C# Tutorial
1232
24
Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

See all articles