在閱讀CSS2 visual formatting model details時,裏面出現了大量的關於replaced element
和non-replaced element
的概念。visual formatting model details
中將element在inline-level and block-level
和replaced and non-replaced
兩個不一樣的維度展開描述。所以,若是咱們想要深刻理解CSS的內部世界,那麼理解replaced element
的概念就是必不可少的了。css
那麼本篇筆記主要解決下面幾個問題:html
replaced element
?intrinsic dimensions
?replaced element
的尺寸?replaced element
?replaced element
w3c css2.2中對replaced element
有一段簡短的描述(來源於3.conformance: Requirements and Recommendations):前端
An element whose content is outside the scope of the CSS formatting model, such as an image, embedded document, or applet. For example, the content of the HTML IMG element is often replaced by the image that its 「src」 attribute designates. Replaced elements often have intrinsic dimensions: an intrinsic width, an intrinsic height, and an intrinsic ratio. For example, a bitmap image has an intrinsic width and an intrinsic height specified in absolute units (from which the intrinsic ratio can obviously be determined). On the other hand, other documents may not have any intrinsic dimensions (for example, a blank HTML document).ios
從這段基本的描述中,咱們能夠總結出什麼:css3
replaced element
的content處於CSS formatting model控制以外。replaced element
好比image
, embedded document
, applet
replaced element
一般包含intrinsic dimensions
: intrinsic width, intrinsic height, intrinsic ratio。可是並非全部的replaced element
都同時包含這三個屬性值。replaced element
之因此冠以replaced
的形容詞,是由於這類element的內容會被指向的外部資源給replace掉。上述的描述中引入一個新的名詞intrinsic dimensions
. 那麼,咱們來看一看什麼事intrinsic dimensions
.算法
intrinsic dimensions
從文字意義上來看,intrinsic dimensions
表示的是內部尺寸。canvas
另外,w3c CSS2.2中一樣對intrinsic dimensions
給出了一段簡短的描述:瀏覽器
The width and height as defined by the element itself, not imposed by the surroundings. CSS does not define how the intrinsic dimensions are found. In CSS 2 only replaced elements can come with intrinsic dimensions. For raster images without reliable resolution information, a size of 1 px unit per image source pixel must be assumed.app
從上面的定義中咱們能夠總結出幾點:less
replaced element
有intrinsic dimensions
.replaced element
的width
和height
都由element
本身定義.可是,咱們光從上面的定義中很難真正的理解intrinsic dimensions
. 所以咱們再來看CSS3,CSS Image Values and Replaced Content Module Level 3 5.1小節有更加精確的定義:
The term intrinsic dimensions refers to the set of the intrinsic height, intrinsic width, and intrinsic aspect ratio (the ratio between the width and height), each of which may or may not exist for a given object. These intrinsic dimensions represent a preferred or natural size of the object itself; that is, they are not a function of the context in which the object is used. CSS does not define how the intrinsic dimensions are found in general. Raster images are an example of an object with all three intrinsic dimensions. SVG images designed to scale might have only an intrinsic aspect ratio; SVG images can also be created with only an intrinsic width or height. CSS gradients, defined in this specification, are an example of an object with no intrinsic dimensions at all. Another example of this is embedded documents, such as the
<iframe>
element in HTML. An object cannot have only two intrinsic dimensions, as any two automatically define the third. If an object (such as an icon) has multiple sizes, then the largest size (by area) is taken as its intrinsic size. If it has multiple aspect ratios at that size, or has multiple aspect ratios and no size, then the aspect ratio closest to the aspect ratio of the default object size is used. Determine this by seeing which aspect ratio produces the largest area when fitting it within the default object size using a contain fit; if multiple sizes tie for the largest area, the wider size is chosen as its intrinsic size.
上面的definition主要包含4大塊內容:
intrinsic dimensions
一般指代intrinsic height
, intrinsic width
, intrinsic aspect ratio(width / height)
三種值的集合。可是對於不一樣replaced element
中的object
來說,這三種value不必定都會存在。intrinsic dimensions
表示的是引入object
的本身的natural size
,也就意味着這是和context
無關的。一般來說,CSS並不會定義如何得到intrinsic dimensions
.這裏若是仔細的話,咱們這裏使用了element
和object
兩個名詞。使用兩個不一樣的名詞是有必定的目的的。首先咱們在html specific replaced element中瞭解到html
中的replaced element
能夠是<audio>
,<img>
,<canvas>
,<embed>
,<iframe>
,<input>
, <object>
, <video>
這幾種。另外咱們在什麼是replaced element
小節中推論到,replaced element
之因此冠以replaced
的形容詞,是由於這類element的內容會被指向的外部資源給replace掉。因此這裏就使用了element
和object
兩個名詞,object
表示引入的外部資源自己,資源自己會有本身固定的的dimensions
。 Note:可是,這並不意味着咱們沒法使用CSS
來控制html element
的外部展現,CSS控制replaced element
的佈局和intrinsic dimension
二者並不衝突。具體細節能夠看下面concrete object size
的算法。
raster image
這個object
同時擁有3個intrinsic dimensions value
。SVG iamges
可能只擁有一個intrinsic aspect ratio
或者intrinsic width
或者intrinsic height
.CSS gradient
3種intrinsic dimension value
都沒有。object
有多個size
。那麼該選擇哪個?規範中是largest size
將會做爲該object
的intrinsic size
.若是有多個aspect ratio
值,那麼最接近ratio of the default object size
的aspect ratio
將會被選擇。若是default object size
使用contain fit
的話,產生最大area的aspect ratio
將會被選擇。可是,這些定義對於咱們實際開發當中有什麼用呢? 換個角度來說,就是咱們在實際開發中,replaced element
的佈局與尺寸該如何來計算呢?
要給出上面的答案,咱們可能還須要知道更多相關的名詞:
specified size The specified size of an object is given by CSS, such as through the ‘width’ and ‘height’ or ‘background-size’ properties. The specified size can be a definite width and height, a set of constraints, or a combination thereof. concrete object size The concrete object size is the result of combining an object's intrinsic dimensions and specified size with the default object size of the context it's used in, producing a rectangle with a definite width and height. default object size The default object size is a rectangle with a definite height and width used to determine the concrete object size when both the intrinsic dimensions and specified size are missing dimensions.
有上面的定義能夠知道:
width
、height
、background-size
屬性。specified size能夠是有限的width和height, constraints集合或者二者的結合。理解了上面三種size
的定義,咱們開始來看看在規範的5.2 CSS Object Negotiation中Objects in CSS 的size是如何被決定而且渲染的:
- When an image or object is specified in a document, such as through a ‘url()’ value in a ‘background-image’ property or an src attribute on an
<img>
element, CSS queries the object for its intrinsic dimensions.- Using the
intrinsic dimensions
, thespecified size
, and thedefault object size
for the context the image or object is used in, CSS then computes aconcrete object size
. (See the following section.) This defines the size and position of the region the object will render in.- CSS asks the object to render itself at the concrete object size. CSS does not define how objects render when the concrete object size is different from the object's intrinsic dimensions. The object may adjust itself to match the concrete object size in some way, or even render itself larger or smaller than the concrete object size to satisfy sizing constraints of its own.
- Unless otherwise specified by CSS, the object is then clipped to the concrete object size.
image or object
在document當中被指定(好比background-image
經過url()
或者<img>
element經過src
來指定),CSS會先去查找object
的intrinsic size
.intrinsic dimensions
, specified size
, 和 default object size
來計算出concrete object size
(具體計算方法看下面).object
根據concrete object size
來渲染本身。CSS並無規定當concrete object size
和intinsic dimensions
不同的時候該如何渲染。object
可能會本身調整本身來適應concrete object size
或者爲了知足本身的sizing constraints來大於或小於concrete object size
。object
會被剪切爲concrete object size
大小。下面咱們展開來說解一下第二步- 如何肯定concrete object size
。在規範5.3. Concrete Object Size Resolution中介紹了Default Sizing Algorithm
:
specified size
是有限的width
和height
,那麼concrete object size
的值就是specified size
.specified size
只有width
或height
其中一個。那麼concrete object size
的相同屬性的值爲specified size
提供。另一個值由如下方式來計算:
intrinsic aspect ratio
, 那麼concrete object size
將使用intrinsic aspect ratio
來計算。object
的intrinsic dimensions
中存在,若是存在,則取用該值。concrete object size
缺乏的dimension則取用default object size
.specified size
沒有任何的constraints:
intrinsic height
或者intrinsic width
,那麼該concrete object size
由intrinsic size
來決定。concrete object size
則被當作是相對於default object size
的contain constraint
。另外,咱們須要瞭解兩個常見的specified sizes
:contain constraint
和cover constraint
.二者都是經過constraint rectangle
和object's intrinsic aspect ratio
來決定concrete object size
的大小。
contain constraint
: concrete object size
將會被設置爲一個根據object's intrinsic aspect ratio
計算出來的最大的rectangle,而且該rectangle的width或height都各自比constraint rectangle
的wdith
和height
小或相等。cover constraint
:concrete object size
會被設置成一個根據object's intrinsic aspect ratio
計算出來的最小的rectangle,而且該rectangle的width或height都各自比constraint rectangle
的width
和height
大或相等。intrinsic aspect ratio
,那麼concrete object size
就是specified constraint rectangle.replaced element
從上面的內容,我能夠知道replaced element
的定義和計算細節。那麼HTML中有哪些元素能夠被稱爲replaced element
呢? 這個咱們得從HTML standard 14.4 replaced element中來看。 規範中主要將replaced element
分爲兩大類:
Embedded content(嵌入內容):
<embed>
, <iframe>
, <video>
這三種元素被看做是replaced elements
.<canvas>
元素來說,若是<canvas>
中表明的是embedded content,那麼該<canvas>
元素也被看成replaced element(好比<canvas>
中的content是bitmap)。不然的話,<canvas>
會被看做是普通的rendering model中的元素。<object>
元素來說,和<canvas>
同樣得分爲兩種狀況。<object>
中展現的是image, plugin, nested browsing context(好比iframe) 時被看作是replaced element.其餘狀況下被認爲是普通元素。<audio>
來說,一般browser會對這類元素有對應的user interface,若是<audio>
的user interface被展現,那麼<audio>
就會被認爲是replaced element
.Images: HTML當中中images主要分爲2種:<img>
和<input type="image">
user agent主要按照下面的規則來render上面兩種元素:
replaced element
,而且根據CSS來render。intrinsic dimensions
(好比來自dimension attributes 或CSS rules)- 該element被認爲是replaced element
:
alt attribute
document
處於quirks mode
.<img>
element表示的是一些文本而且user agent不但願其發生改變: 該元素被看作是non-replaced phrasing element
<img>
element表示的是nonthing,而且user agent不但願其發生改變:該元素被看作是empty inline element
input element
不展現image而且user agent不但願其發生改變:那麼該element被看作是replaced element
(元素包含一個button,button的內容是element的可替換內容)。Note:1. 對於embed, iframe, img, object
元素或者<input type="image">
元素的align attribute
等於center or middle
時,該element的vertical-align
將會被設定爲value(element的vertical middle和parent element baseline 對齊)。
到這裏爲止,咱們基本已經可以來回答在introduction中提出的4個問題。對於更多的細節(好比width
和height
如何計算等等)能夠看specification。