Contents
This chapter and the next describe the
In the visual formatting model, each element in the document treegenerates zero or more boxes according to theboxmodel. The layout of these boxes is governed by:
The properties defined in this chapter and the next apply to bothcontinuous media andpaged media.However, themeanings of themarginproperties vary when applied to paged media (see thepage model for details).
The visual formatting model does not specify all aspects offormatting (e.g., it does not specify a letter-spacing algorithm).Conforming user agents may behavedifferently for those formatting issues not covered by thisspecification.
User agents forcontinuous mediagenerally offer users aviewport (a window or otherviewing area on the screen) through which users consult adocument. User agents may change the document's layout when theviewport is resized (see theinitial containing block).
When the viewport is smaller than the area of the canvas on whichthe document is rendered, the user agent should offer a scrollingmechanism.There is at mostone viewport percanvas, but useragents may render to more than one canvas (i.e., provide differentviews of the same document).
In CSS 2.2, many box positions and sizes are calculated with respectto the edgesof a rectangular box called a
Each box is given a position with respect to its containing block,but it is not confined by this containing block; it mayoverflow.
Thedetails ofhow a containing block's dimensions are calculated are described inthenext chapter.
The following sections describe the types of boxes that may begenerated in CSS 2.2. A box's type affects, in part, its behavior in thevisual formatting model. The
Certain values of the Block-level elements –those elements of the source document that are formatted visually asblocks (e.g., paragraphs) – are elements which generate ablock-level principal box. Values of the In CSS 2.2, a block-level box is also a block container boxunless it is a table box or the principal box of a replaced element.Ablock container boxeither contains only block-level boxes or establishes aninline formatting context and thuscontains onlyinline-levelboxes. An element whose principal box is a block container box isablock containerelement. Values of the 'display' property which makea non-replaced element generate a block container include 'block','list-item' and 'inline-block'. Not all block container boxes areblock-level boxes: non-replaced inline blocks and non-replaced tablecells are block containers but are not block-level. Block-level boxesthat are also block containers are called The three terms "block-level box," "block container box," and"block box" are sometimes abbreviated as "block" where unambiguous. In a document like this: (and assuming the DIV and the P both have 'display: block'), theDIV appears to have both inline content and block content. To make iteasier to define the formatting, we assume that there is an Diagram showing thethree boxes, of which one is anonymous, for the example above. In other words: if a block container box (such as that generatedfor the DIV above) has a block-level box inside it (such as the Pabove), then we force it to haveonly block-level boxesinside it. When an inline box contains an in-flow block-level box, the inline box(and its inline ancestors within the same line box) is broken aroundthe block-level box (and any block-level siblings that are consecutiveor separated only by collapsible whitespace and/or out-of-flowelements), splitting the inline box into two boxes (even if eitherside is empty), one on each side of the block-level box(es). The lineboxes before the break and after the break are enclosed in anonymous block boxes, and the block-level box becomes a sibling of those anonymous boxes. When such an inline box is affected by relative positioning, any resulting translation alsoaffects the block-level box contained in the inline box. Example(s): This model would apply in the following example if the followingrules: were used with this HTML document: The P element contains a chunk (C1) of anonymous text followedby a block-level element followed by another chunk (C2) of anonymoustext. The resulting boxes would be a block box representing the BODY,containing an anonymous block box around C1, the SPAN block box, andanother anonymous block box around C2. The properties of anonymous boxes are inherited from theenclosing non-anonymous box (e.g., in the example just below the subsection heading "Anonymous block boxes", the one for DIV). Non-inherited properties have their initial value. For example,the font of the anonymous box is inherited from the DIV, but themargins will be 0. Properties set on elements that cause anonymous block boxes to be generated still apply to the boxes and content of that element. For example, if a border had been set on the P element in the above example, the border would be drawn around C1 (open at the end of the line) and C2 (open at the start of the line). Some user agents have implemented borders on inlines containing blocks in other ways, e.g., by wrapping such nested blocks inside "anonymous line boxes" and thus drawing inline borders around such boxes. As CSS1 and CSS2 did not define this behavior, CSS1-only and CSS2-only user agents may implement this alternative model and still claim conformance to this part of CSS 2.2. This does not apply to UAs developed after this specification was released. Anonymous block boxes are ignored when resolving percentage valuesthat would refer to it: the closest non-anonymous ancestor box is usedinstead. For example, if the child of the anonymous block box insidethe DIV above needs to know the height of its containing block toresolve a percentage height, then it will use the height of thecontaining block formed by the DIV, not of the anonymous block box. Inline-levelelements are those elements of the source document thatdo not form new blocks of content; the content is distributed in lines (e.g., emphasized pieces of textwithin a paragraph, inline images,etc.). The following values of the Aninlinebox is one that is both inline-level and whosecontents participate in its containing inline formatting context. Anon-replaced element with a 'display' value of 'inline' generates aninline box.Inline-level boxes that are not inline boxes (such as replacedinline-level elements, inline-block elements, and inline-tableelements) are calledatomic inline-level boxes becausethey participate in their inline formatting context as a single opaquebox. Any text that is directly contained inside a block container element (notinside an inline element) must be treated as an anonymous inlineelement. In a document with HTML markup like this: the Such anonymous inline boxes inherit inheritable properties fromtheir block parent box. Non-inherited properties have their initialvalue. In the example, the color of the anonymous inline boxes isinherited from the P, but the background is transparent. White space content that would subsequently be collapsed away according to the'white-space' property does not generate any anonymous inline boxes. If it is clear from the context which type of anonymous box ismeant, both anonymous inline boxes and anonymous block boxes aresimply called anonymous boxes in this specification. There are more types of anonymous boxes that arise when formattingtables. [This section exists so that the section numbers are the same as inprevious drafts.'Display:run-in' is now defined in CSS level 3 (seeCSS basic box model).] The values of this property have the following meanings: Please note that a display of 'none' does not create an invisiblebox; it creates no box at all. CSS includes mechanisms that enable anelement to generate boxes in the formatting structure that affectformatting but are not visible themselves. Please consult the sectiononvisibility for details. The computed value is the same as the specified value, except forpositioned and floating elements (seeRelationships between 'display', 'position', and'float') and for the root element. For the root element, the computed value is changed as described in the section on therelationships between 'display', 'position', and 'float'. Note that although theinitialvalue of'display' is'inline', rules in the user agent'sdefault style sheet mayoverride this value. See thesample style sheet for HTML 4 in theappendix. Example(s): Here are some examples of the In CSS 2.2, a box may be laid out according to three An element is calledout offlow if it is floated, absolutely positioned, or is theroot element. An element is called The'position' and The values of this property have the following meanings: Example(s): UAs must not paginate the content of fixed boxes. User agents may treat position as 'static' on the root element. An element is said to bepositionedif its'position' property hasa value other than 'static'. Positioned elements generatepositioned boxes, laid out according to four properties: This property specifies how far anabsolutely positioned box's topmargin edge is offset below the top edge of the box'scontaining block. For relativelypositioned boxes, the offset is with respect to the top edges of thebox itself (i.e., the box is given a position in the normal flow, thenoffset from that position according to these properties). Like 'top', but specifies how far a box's right margin edge isoffset to the left of the right edge of the box'scontaining block. For relativelypositioned boxes, the offset is with respect to the right edge of thebox itself. Like 'top', but specifies how far a box's bottom margin edge isoffset above the bottom of the box'scontaining block. For relativelypositioned boxes, the offset is with respect to the bottom edge of thebox itself. Like 'top', but specifies how far a box's left margin edge isoffset to the right of the left edge of the box'scontaining block. For relativelypositioned boxes, the offset is with respect to the left edge of thebox itself. The values for the four properties have the following meanings: Boxes in the normal flow belong to a Floats, absolutely positioned elements, block containers (such asinline-blocks, table-cells, and table-captions) that are not blockboxes, and block boxes with 'overflow' other than 'visible'(except when that value has been propagated to the viewport) establishnew block formatting contexts for their contents. In a block formatting context, boxes are laid out one after theother, vertically, beginning at the top of a containing block. Thevertical distance between two sibling boxes is determined by the In a block formatting context, each box's left outer edge touches theleft edge of the containing block (for right-to-left formatting, rightedges touch). This is true even in the presence of floats (although abox'sline boxes may shrink due to the floats), unless the boxestablishes a new block formatting context (in which case the box itselfmay become narrower due tothe floats). For information about page breaks in paged media, please consultthe section onallowedpage breaks. An inline formatting context is established by a block containerbox that contains no block-level boxes.In an inline formatting context, boxes are laid out horizontally,one after the other, beginning at the top of a containingblock. Horizontal margins, borders, and padding are respected betweenthese boxes. The boxes may be aligned vertically in different ways: theirbottoms or tops may be aligned, or the baselines of text within themmay be aligned. The rectangular area that contains the boxes that forma line is called aline box. The width of a line box is determined by acontaining block and the presence of floats. The height of a linebox is determined by the rules given in the section online height calculations. A line box is always tall enough for all of the boxes it contains. However, it may be taller than the tallest box it contains (if, for example, boxes are aligned so that baselines line up). When the height of a box B is less than the height of the line box containing it, the vertical alignment of B within the line box is determined by the'vertical-align' property.When several inline-level boxes cannot fit horizontally within a singleline box, they are distributed among two or more vertically-stackedline boxes. Thus, a paragraph is a vertical stack of line boxes. Lineboxes are stacked with no vertical separation (except as specifiedelsewhere) and they never overlap. In general, the left edge of a line box touches the left edgeof its containing block and the right edge touches the right edge ofits containing block. However, floating boxes may come between thecontaining block edge and the line box edge. Thus, although lineboxes in the same inline formatting context generally have the samewidth (that of the containing block), they may vary in width ifavailable horizontal space is reduced due tofloats. Line boxes in the same inline formattingcontext generally vary in height (e.g., one line might contain a tallimage while the others contain only text). When the total width of the inline-level boxes on a line is less than thewidth of the line box containing them, their horizontal distributionwithin the line box is determined by the When an inline box exceeds the width of a line box, it is split into several boxes and these boxes are distributed across several line boxes. If an inline box cannot be split (e.g., if the inline box contains a single character, or language specific word breaking rules disallow a break within the inline box, or if the inline box is affected by a white-space value of nowrap or pre), then the inline box overflows the line box. When an inline box is split, margins,borders, and padding have no visual effect where the split occurs (orat any split, when there are several). Inline boxes may also be split into several boxeswithin thesame line box due tobidirectional textprocessing. Line boxes are created as needed to holdinline-level content within an inline formatting context. Line boxesthat contain no text, nopreserved white space, no inlineelements with non-zero margins, padding, or borders, and no otherin-flow content (such as images, inlineblocks or inline tables), and do not end with a preserved newline mustbe treated as zero-height line boxes for the purposes of determiningthe positions of any elements inside of them, and must be treated asnot existing for any other purpose. Here is an example of inline box construction. The following paragraph(created by the HTML block-level element P) contains anonymous textinterspersed with the elements EM and STRONG: The P element generates a block box that contains five inlineboxes, three of which are anonymous: To format the paragraph, the user agent flows the five boxes intoline boxes. In this example, the box generated for the P elementestablishes the containing block for the line boxes. If the containingblock is sufficiently wide, all the inline boxes will fit into asingle line box: If not, the inline boxes will be split up and distributed acrossseveral line boxes. The previous paragraph might be split as follows: In the previous example, the EM box was split into two EM boxes(call them "split1" and "split2"). Margins, borders,padding, or text decorations have no visible effect after split1 orbefore split2. Consider the following example: Depending on the width of the P, the boxes may be distributed asfollows: Once a box has been laid out according to thenormal flow or floated, it may be shifted relative tothis position. This is calledrelative positioning. Offsetting a box(B1) in this way has no effect on the box (B2) that follows: B2 isgiven a position as if B1 were not offset and B2 is not re-positionedafter B1's offset is applied. This implies that relative positioningmay cause boxes to overlap.However, if relative positioning causes an 'overflow:auto' or'overflow:scroll' box to haveoverflow, the UA must allow the user to access this content (at its offsetposition), which, through the creation of scrollbars, may affect layout. A relatively positioned box keeps its normal flow size, includingline breaks and the space originally reserved for it. The section oncontaining blocks explains when arelatively positioned box establishes a new containing block. For relatively positioned elements, 'left' and 'right' move thebox(es) horizontally, without changing their size. 'Left' moves theboxes to the right, and 'right' moves them to the left. Since boxesare not split or stretched as a result of 'left' or 'right', theused values are always: left = -right. If both 'left' and 'right' are 'auto' (their initial values), theused values are '0' (i.e., the boxes stay in their originalposition). If 'left' is 'auto', its used value is minus the value of 'right'(i.e., the boxes move to the left by the value of 'right'). If 'right' is specified as 'auto', its used value is minus thevalue of 'left'. If neither 'left' nor 'right' is 'auto', the position isover-constrained, and one of them has to be ignored. If the 'direction' property of the containing block is 'ltr', the value of 'left' wins and 'right'becomes -'left'. If 'direction' of the containing block is 'rtl', 'right' wins and 'left' is ignored. Example(s): Example. The following three rules are equivalent: The 'top' and 'bottom' properties move relatively positionedelement(s) up or down without changing their size. 'Top' moves theboxes down, and 'bottom' moves them up. Since boxesare not split or stretched as a result of 'top' or 'bottom', theused values are always: top = -bottom.If both are 'auto', their used values are both '0'. If one of them is'auto', it becomes the negative of the other. If neither is 'auto','bottom' is ignored (i.e., the used value of 'bottom' will beminus the value of 'top'). Note. Dynamic movement of relatively positioned boxes can produceanimation effects in scripting environments (see also the Examples of relative positioning are provided in the sectioncomparing normal flow, floats, and absolutepositioning. A float is a box that is shifted to the left or right on thecurrent line. The most interesting characteristic of a float (or"floated" or "floating" box) is that content may flow along its side(or be prohibited from doing so by the A floated box isshifted to the left or right until its outer edge touches thecontaining block edge or the outer edge of another float. If there is a line box, the outer top of the floated box is aligned with the top of the current line box. If there is not enough horizontal room for thefloat, it is shifted downward until either it fits or there are nomore floats present. Since a float is not in the flow, non-positioned block boxescreated before and after the float box flow vertically as if the floatdid not exist. However,the current and subsequentline boxes created next to the float areshortenedas necessaryto make room for the margin box of the float. A line box is next to a float when there exists a vertical positionthat satisfies all of these four conditions: (a) at or below the topof the line box, (b) at or above the bottom of the line box, (c) belowthe top margin edge of the float, and (d) above the bottom margin edgeof the float. Note: this means that floats with zero outer height ornegative outer height do not shorten line boxes. If a shortened line box is too small to contain any content, thenthe line box is shifted downward (and its width recomputed) untileither some content fits or there are no more floats present.Any content in the current line before a floated box is reflowed in the same line on the other side of the float.In other words, if inline-level boxes are placed on the line before a left float is encountered that fits in the remaining line box space, the left float is placed on that line, aligned with the top of the line box, and then the inline-level boxes already on the line are moved accordingly to the right of the float (the right being the other side of the left float) and vice versa for rtl and right floats. The border box of a table, a block-level replaced element, or an element in the normal flow that establishes a newblock formatting context (such as an element with 'overflow' other than 'visible')must not overlap the margin box of any floats in the same blockformatting context asthe element itself. If necessary, implementations should clear thesaid element by placing it below any preceding floats, but may placeit adjacent to such floats if there is sufficient space.They may even make the border box of said element narrower thandefined bysection 10.3.3.CSS2 does not define when a UA may put said element next to the floator by how much said element may become narrower. Example(s): Example. In the following document fragment, the containing block is too narrow to contain the content next to the float, so the content gets moved to below the floats where it is aligned in the line box according to the text-align property. This fragment might look like this: Several floats may be adjacent, and this model also applies toadjacent floats in the same line. Example(s): The following rule floats all IMG boxes withclass="icon" to the left (andsets the left margin to '0'): Consider the following HTML source and style sheet: The IMG box is floated to the left. The content that follows isformatted to the right of the float, starting on the same line as thefloat. The line boxes to the right of the float are shortened due tothe float's presence, but resume their "normal" width (that of thecontaining block established by the P element) after the float. Thisdocument might be formatted as: Formatting would have been exactly the same if the document hadbeen: because the content to the left of the float is displaced bythe float and reflowed down its right side. As stated insection 8.3.1, the margins of floating boxes nevercollapse with margins ofadjacent boxes. Thus, in the previous example, vertical margins do notcollapse between the P boxand the floated IMG box. The contents of floats are stacked as if floats generated new stacking contexts, except that any positioned elements and elementsthat actually create newstacking contexts take part in the float's parent stacking context. A float can overlap other boxes in the normal flow (e.g., when a normal flow box next to a float has negative margins). When this happens, floats are rendered in front of non-positioned in-flow blocks, but behind in-flow inlines. Example(s): Here is another illustration, showing what happens when a floatoverlaps borders of elements in the normal flow. A floating image obscures bordersof block boxes it overlaps. The following example illustrates the use of the Example(s): Assuming a rule such as this: formatting might look like this: This property specifies whether a box should float to the left,right, or not at all. It may be set for any element, but only applies to elements that generate boxes that are notabsolutely positioned. The values of this property havethe following meanings: User agents may treat float as 'none' on the root element. Here are the precise rules thatgovern the behavior of floats: But in CSS 2.2, if, within the block formatting context, thereis an in-flow negative vertical margin such that the float's positionis above the position it would be at were all such negative marginsset to zero, the position of the float is undefined. References to other elements in these rules refer only to other elements in the sameblock formatting context as the float. Example(s): This HTML fragment results in the b floating to the right. If the P element's width is enough, the a and the b will be side byside. It might look like this: This property indicates which sides of an element's box(es) maynot be adjacent to an earlier floating box. The 'clear'property does not consider floats inside the element itself or inotherblock formattingcontexts. Values have the following meanings when applied to non-floatingblock-level boxes: Values other than 'none' potentially introduce Computing the clearance of an element on which 'clear' is set isdone by first determining the hypothetical position of the element'stop border edge. This position iswhere the actual top border edge would have been if the element's'clear' property had been 'none'. If this hypothetical position of the element's top border edge isnot past the relevant floats, then clearance is introduced, andmargins collapse according to the rules in 8.3.1. Then the amount of clearance is set to the greater of: Alternatively, clearance is set exactly to the amount necessary toplace the border edge of the block even with the bottom outer edge ofthe lowest float that is to be cleared. Note: Both behaviors are allowedpending evaluation of their compatibility with existing Web content. Afuture CSS specification will require either one or the other. Note: The clearance can be negative or zero. Example(s): Example 1. Assume (for the sake of simplicity), that we have justthree boxes, in this order: block B1 with a bottom margin of M1 (B1has no children and no padding or border), floating block F with aheight H, and block B2 with a top margin of M2 (no padding or border,no children). B2 has 'clear' set to 'both'. We also assume B2 is notempty. Without considering the 'clear' property on B2, we have the situation in the diagram below. The margins of B1 and B2 collapse. Let's say the bottom border edge of B1 is at y = 0, then the top of F is at y = M1, the top border edge of B2 is at y = max(M1,M2), and the bottom of F is at y = M1 + H. We also assume that B2 is not below F, i.e., we are in thesituation described in the spec where we need to add clearance. Thatmeans: max(M1,M2) < M1 + H We need to compute clearance C twice, C1 and C2, and keep the greater of the two: C = max(C1,C2). The first way is to put the top of B2 flush with the bottom of F, i.e., at y = M1 + H. That means, because the margins no longer collapse with a clearance between them: bottom of F= top border edge of B2⇔ M1 + H= M1 + C1 + M2⇔ C1= M1 + H - M1 - M2 = H - M2 The second computation is to keep the top of B2 where it is, i.e., at y = max(M1,M2). That means: max(M1,M2)= M1 + C2 + M2⇔ C2= max(M1,M2) - M1 - M2 We assumed that max(M1,M2) < M1 + H, which implies C2 = max(M1,M2) - M1 - M2< M1 + H - M1 - M2 = H - M2⇒ C2< H - M2 And, as C1 = H - M2, it follows that C2 < C1 and hence C = max(C1,C2) = C1 Example(s): Example 2. An example of negative clearance is this situation, inwhich theclearance is -1em. (Assume none of the elements have borders orpadding): Explanation: Without the 'clear', the first and last paragraphs'margins would collapse and the last paragraph's top border edge wouldbe flush with the top of the floating paragraph. But the 'clear'requires the top border edge to bebelow the float, i.e., 2emlower. This means that clearance must be introduced. Accordingly, themargins no longer collapse and the amount of clearance is set so thatclearance + margin-top = 2em, i.e., clearance = 2em - margin-top = 2em- 3em = -1em. When the property is set on floating elements, it results in amodification of therules forpositioning the float. An extra constraint (#10) is added: Note.This propertyapplied to all elements in CSS1. Implementations may therefore have supported this property on all elements. In CSS2 and CSS 2.2 the 'clear' property only applies to block-level elements. Therefore authors should only use this property on block-levelelements. If an implementation does support clear on inline elements, rather than setting a clearance as explained above, the implementation should force a break and effectively insert one or more empty line boxes (or shifting the new line box downward as described insection 9.5) to move the top of the cleared inline's line box to below the respective floating box(es). In the absolute positioning model, a box is explicitly offset withrespect to its containing block. It is removed from the normal flowentirely (it has no impact on later siblings). An absolutelypositioned box establishes a new containing block for normal flowchildren and absolutely (but not fixed) positioned descendants. However, the contents of anabsolutely positioned element do not flow around any other boxes. Theymay obscure the contents of another box (or be obscured themselves), depending on thestack levels of the overlapping boxes. References in this specification to an Fixed positioning is a subcategory of absolute positioning. Theonly difference is that for a fixed positioned box, the containingblock is established by theviewport. Forcontinuous media, fixedboxes do not move when the document is scrolled. In this respect, theyare similar tofixedbackground images. Forpaged media, boxeswith fixed positions are repeated on every page. This is useful forplacing, for instance, a signature at the bottom of each page.Boxes with fixed position that are larger than the page area areclipped. Parts of the fixed position box that are not visible in theinitial containing block will not print. Authors may use fixed positioning to create frame-like presentations.Consider the following frame layout: This might be achieved with the following HTML document andstyle rules: The three properties that affect box generation and layout —'display','position', and'float' — interact as follows: To illustrate the differences between normal flow, relativepositioning, floats, and absolute positioning, we provide a series ofexamples based on the following HTML: In this document, we assume the following rules: The final positions of boxes generated by theouter andinner elements vary in each example. In each illustration,the numbers to the left of the illustration indicate thenormal flow position of the double-spaced (forclarity) lines. Note. The diagrams in this section are illustrative and not to scale. They are meant to highlight the differences between the various positioning schemes in CSS 2.2, and are not intended to be reference renderings of the examples given. Consider the following CSS declarations forouter andinner that do not alter thenormalflow of boxes: The P element contains all inline content:anonymous inline text and two SPANelements. Therefore, all of the content will be laid outin an inline formatting context, within a containing blockestablished by the P element, producing something like: To see the effect ofrelativepositioning, we specify: Text flows normally up to theouter element. Theouter text is then flowed into its normal flow position anddimensions at the end of line 1. Then, the inline boxes containing thetext (distributed over three lines) are shifted as a unit by '-12px'(upwards). The contents ofinner, as a child ofouter, wouldnormally flow immediately after the words "of outer contents" (on line1.5). However, theinner contents are themselves offsetrelative to theouter contents by '12px' (downwards), back totheir original position on line 2. Note that the content followingouter is not affected by therelative positioning ofouter. Note also that had the offset ofouter been '-24px', thetext ofouter and the body text would have overlapped. Now consider the effect offloating theinner element's text to the right by means of the followingrules: Text flows normally up to theinner box, which is pulledout of the flow and floated to the right margin (its To show the effect of the The following rules: cause theinner box to float to the right as before and thedocument's remaining text to flow into the vacated space: However, if the'clear'property on thesibling element is set to 'right' (i.e., thegeneratedsibling box will not accept a position next tofloating boxes to its right), thesibling content begins toflow below the float: Finally, we consider the effect ofabsolute positioning.Consider the following CSS declarations forouter andinner: which cause the top of theouter box to be positioned withrespect to its containing block. The containing block for a positionedbox is established by the nearest positioned ancestor (or, if noneexists, theinitial containingblock, as in our example). The top side of theouter boxis '200px' below the top of the containing block and the left side is'200px' from the left side. The child box ofouter is flowednormally with respect to its parent. The following example shows an absolutely positioned box that is achild of a relatively positioned box. Although the parentouter box is not actually offset, setting its This results in something like the following: If we do not position theouter box: the containing block forinner becomes theinitial containing block (in ourexample). The following illustration shows where theinnerbox would end up in this case. Relative and absolute positioning may be used to implement changebars, as shown in the following example. The following fragment: might result in something like: First, the paragraph (whose containing block sides are shown in theillustration) is flowed normally. Then it is offset '10px' from theleft edge of the containing block (thus, a right margin of '10px' hasbeen reserved in anticipation of the offset). The two hyphens actingas change bars are taken out of the flow and positioned at the currentline (due to 'top: auto'), '-1em' from the left edge of its containingblock (established by the P in its final position). The result isthat the change bars seem to "float" to the left of the currentline. For a positioned box, the Values have the following meanings: In this section, the expression "in front of" means closer to the user as the user faces the screen. In CSS 2.2, each box has a position in three dimensions. In additionto their horizontal and vertical positions, boxes lie along a "z-axis"and are formatted one on top of the other. Z-axis positions areparticularly relevant when boxes overlap visually. This sectiondiscusses how boxes may be positioned along the z-axis. The order in which the rendering tree is painted onto the canvas is described in terms of stacking contexts. Stacking contexts can contain further stacking contexts. A stacking context is atomic from the point of view of its parent stacking context; boxes in other stacking contexts may not come between any of its boxes. Each box belongs to onestacking context. Each positioned box in agiven stacking context has an integer The root element forms the root stacking context. Other stacking contexts are generated by any positioned element (including relatively positioned elements) having a computed value of 'z-index' other than 'auto'. Stacking contexts are not necessarily related to containing blocks. In future levels of CSS, other properties may introduce stacking contexts, for example 'opacity'[CSS3COLOR]. Within each stacking context, the following layers are painted inback-to-front order: Within each stacking context, positioned elements with stack level0 (in layer 6), non-positioned floats (layer 4), inlineblocks (layer 5), and inline tables (layer 5), are paintedas if those elements themselves generated new stacking contexts,except that their positioned descendants and any would-be childstacking contexts take part in the current stacking context. This painting order is applied recursively to each stackingcontext. This description of stacking context painting orderconstitutes an overview of the detailed normative definition inAppendix E. In the following example, the stack levels ofthe boxes (named with their "id" attributes) are:"text2"=0, "image"=1, "text3"=2, and "text1"=3. The"text2" stack level is inherited from the root box. Theothers are specified with the This example demonstrates the notion oftransparency. The default behavior of the background is to allow boxes behind it to be visible. In the example, each box transparently overlays the boxes below it. Thisbehavior can be overridden by using one of the existingbackground properties. Conforming user agents that donot support bidirectional text may ignore the The characters in certain scripts are written from right toleft. In some documents, in particular those written with the Arabicor Hebrew script, and in some mixed-language contexts, text in asingle (visually displayed) block may appear with mixeddirectionality. This phenomenon is called The Unicode standard ([UNICODE],[UAX9]) defines a complexalgorithm for determining the proper directionality of text. Thealgorithm consists of an implicit part based on character properties,as well as explicit controls for embeddings and overrides. CSS 2.2 relieson this algorithm to achieve proper bidirectional rendering. The User agents that support bidirectional text must apply the Unicodebidirectional algorithm to every sequence of inline-level boxes uninterruptedby a forced (bidiclass B) break or block boundary. This sequence forms the"paragraph" unit in the bidirectional algorithm. The paragraph embeddinglevel is set according to the value of the Because the directionality of a text depends on the structure andsemantics of the document language, these properties should in mostcases be used only by designers of document type descriptions (DTDs),or authors of special documents. If a default style sheet specifiesthese properties, authors and users should not specify rules tooverride them. The HTML 4 specification ([HTML4], section 8.2) definesbidirectionality behavior for HTML elements. The style sheetrules that would achieve the bidi behavior specified in[HTML4] aregiven inthe sample style sheet. TheHTML 4 specification also contains more information onbidirectionality issues. This property specifies the base writing direction of blocks andthe direction of embeddings and overrides (see Values for this property have the following meanings: For the'direction'property to affect reordering in inline elements, the Note. The'direction' property, whenspecified for table column elements, is not inherited by cells in thecolumn since columns are not the ancestors of the cells in the document tree. Thus, CSS cannot easily capture the "dir" attribute inheritance rules describedin[HTML4], section 11.3.2.1. Values for this property have the following meanings: The final order of characters in each block container is thesame as if the bidi control codes had been added as described above,markup had been stripped, and the resulting character sequence hadbeen passed to an implementation of the Unicode bidirectionalalgorithm for plain text that produced the same line-breaks as thestyled text.In this process, replaced elements with 'display: inline'are treated asneutral characters, unless their Please note that in order to be able to flow inline boxes in auniform direction (either entirely left-to-right or entirelyright-to-left), more inline boxes (including anonymous inline boxes)may have to be created, and some inline boxes may have to be split upand reordered before flowing. Because the Unicode algorithm has a limit of61 levels ofembedding, care should be taken not to use The following example shows an XML document with bidirectionaltext. It illustrates an important design principle: Example(s): In this example, lowercase letters stand for inherently left-to-right characters anduppercase letters represent inherently right-to-left characters: Since this is XML, the style sheet is responsible for setting thewriting direction. This is the style sheet: The HEBREW element is a block with a right-to-left base direction,the ENGLISH element is a block with a left-to-right basedirection. The PARs are blocks that inherit the base direction fromtheir parents. Thus, the first two PARs are read starting at the topright, the final three are read starting at the top left. Please notethat HEBREW and ENGLISH are chosen as element names for explicitnessonly; in general, element names should convey structure withoutreference to language. The EMPH element is inline-level, and since its value for The formatting of this text might look like this if the line lengthis long: Note that the HE-QUO embedding causes HEBREW18 to be to the rightof english19. If lines have to be broken, it might be more like this: Because HEBREW18 must be read before english19, it is on the lineabove english19. Just breaking the long line from the earlierformatting would not have worked. Note also that the first syllablefrom english19 might have fit on the previous line, but hyphenation ofleft-to-right words in a right-to-left context, and vice versa, isusually suppressed to avoid having to display a hyphen in the middleof a line.9.2.1Block-level elements and block boxes
9.2.1.1Anonymous block boxes
<DIV> Some text <P>More text</DIV>
p { display: inline }span { display: block }
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><HEAD><TITLE>Anonymous text interrupted by a block</TITLE></HEAD><BODY><P>This is anonymous text before the SPAN.<SPAN>This is the content of SPAN.</SPAN>This is anonymous text after the SPAN.</P></BODY>
9.2.2Inline-level elements and inline boxes
9.2.2.1Anonymous inline boxes
<p>Some <em>emphasized</em> text</p>
<p>
generates a block box, with several inline boxes insideit. The box for "emphasized" is an inline box generated by an inlineelement (<em>
), but the other boxes ("Some" and "text") are inline boxes generated by a block-level element (<p>
). The latter are called9.2.3Run-in boxes
9.2.4The
Name: display Value: inline | block | list-item | inline-block |table | inline-table | table-row-group | table-header-group |table-footer-group | table-row | table-column-group | table-column |table-cell | table-caption | none |inherit Initial: inline Applies to: all elements Inherited: no Percentages: N/A Media: all Computed value: see text p { display: block }em { display: inline }li { display: list-item } img { display: none } /* Do not display images */
9.3Positioning schemes
9.3.1Choosing a positioning scheme:
Name: position Value: static | relative | absolute | fixed |inherit Initial: static Applies to: all elements Inherited: no Percentages: N/A Media: visual Computed value: as specified @media screen { h1#first { position: fixed } }@media print { h1#first { position: static }}
9.3.2Box offsets:
Name: top Value: <length> |<percentage> | auto |inherit Initial: auto Applies to: positioned elements Inherited: no Percentages: refer to height of containing block Media: visual Computed value: if specified as a length, the corresponding absolute length; if specified as a percentage, the specified value; otherwise, 'auto'. Name: right Value: <length> |<percentage> | auto |inherit Initial: auto Applies to: positioned elements Inherited: no Percentages: refer to width of containing block Media: visual Computed value: if specified as a length, the corresponding absolute length; if specified as a percentage, the specified value; otherwise, 'auto'. Name: bottom Value: <length> |<percentage> | auto |inherit Initial: auto Applies to: positioned elements Inherited: no Percentages: refer to height of containing block Media: visual Computed value: if specified as a length, the corresponding absolute length; if specified as a percentage, the specified value; otherwise, 'auto'. Name: left Value: <length> |<percentage> | auto |inherit Initial: auto Applies to: positioned elements Inherited: no Percentages: refer to width of containing block Media: visual Computed value: if specified as a length, the corresponding absolute length; if specified as a percentage, the specified value; otherwise, 'auto'. 9.4Normal flow
9.4.1Block formatting contexts
9.4.2Inline formatting contexts
<P>Several <EM>emphasized words</EM> appear<STRONG>in this</STRONG> sentence, dear.</P>
Severalemphasized words appearin this sentence, dear.
Severalemphasized words appearin this sentence, dear.
or like this:Severalemphasizedwords appearin this sentence, dear.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><HTML> <HEAD> <TITLE>Example of inline flow on several lines</TITLE> <STYLE type="text/css"> EM { padding: 2px; margin: 1em; border-width: medium; border-style: dashed; line-height: 2.4em; } </STYLE> </HEAD> <BODY> <P>Several <EM>emphasized words</EM> appear here.</P> </BODY></HTML>
9.4.3Relative positioning
div.a8 { position: relative; direction: ltr; left: -1em; right: auto }div.a8 { position: relative; direction: ltr; left: auto; right: 1em }div.a8 { position: relative; direction: ltr; left: -1em; right: 5em }
9.5Floats
p { width: 10em; border: solid aqua; }span { float: left; width: 5em; height: 5em; border: solid blue; }
...<p> <span> </span> Supercalifragilisticexpialidocious</p>
img.icon { float: left; margin-left: 0;}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><HTML> <HEAD> <TITLE>Float example</TITLE> <STYLE type="text/css"> IMG { float: left } BODY, P, IMG { margin: 2em } </STYLE> </HEAD> <BODY> <P><IMG src=img.png alt="This image will illustrate floats"> Some sample text that has no other... </BODY></HTML>
<BODY> <P>Some sample text <IMG src=img.png alt="This image will illustrate floats"> that has no other...</BODY>
p { clear: left }
9.5.1Positioning the float: the'float' property
Name: float Value: left | right | none |inherit Initial: none Applies to: all, but see9.7 Inherited: no Percentages: N/A Media: visual Computed value: as specified <P>a<SPAN>b</SPAN></P>
9.5.2Controlling flow next to floats:the'clear' property
Name: clear Value: none | left | right | both |inherit Initial: none Applies to: block-level elements Inherited: no Percentages: N/A Media: visual Computed value: as specified <p> First paragraph.<p> Floating paragraph.<p> Last paragraph.
9.6Absolute positioning
9.6.1Fixed positioning
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><HTML> <HEAD> <TITLE>A frame document with CSS</TITLE> <STYLE type="text/css" media="screen"> BODY { height: 8.5in } /* Required for percentage heights below */ #header { position: fixed; width: 100%; height: 15%; top: 0; right: 0; bottom: auto; left: 0; } #sidebar { position: fixed; width: 10em; height: auto; top: 15%; right: auto; bottom: 100px; left: 0; } #main { position: fixed; width: auto; height: auto; top: 15%; right: 0; bottom: 100px; left: 10em; } #footer { position: fixed; width: 100%; height: 100px; top: auto; right: 0; bottom: 0; left: 0; } </STYLE> </HEAD> <BODY> <DIV> ... </DIV> <DIV> ... </DIV> <DIV> ... </DIV> <DIV> ... </DIV> </BODY></HTML>
9.7Relationships between 'display', 'position',and 'float'
Specified value Computed value inline-table table inline, table-row-group, table-column,table-column-group, table-header-group, table-footer-group, table-row,table-cell, table-caption, inline-block block others same as specified 9.8Comparison of normal flow, floats, and absolute positioning
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><HTML> <HEAD> <TITLE>Comparison of positioning schemes</TITLE> </HEAD> <BODY> <P>Beginning of body contents. <SPAN> Start of outer contents. <SPAN> Inner contents.</SPAN> End of outer contents.</SPAN> End of body contents. </P> </BODY></HTML>
body { display: block; font-size:12px; line-height: 200%; width: 400px; height: 400px }p { display: block }span { display: inline }
9.8.1Normal flow
#outer { color: red }#inner { color: blue }
9.8.2Relative positioning
#outer { position: relative; top: -12px; color: red }#inner { position: relative; top: 12px; color: blue }
9.8.3Floating a box
#outer { color: red }#inner { float: right; width: 130px; color: blue }
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><HTML> <HEAD> <TITLE>Comparison of positioning schemes II</TITLE> </HEAD> <BODY> <P>Beginning of body contents. <SPAN id=outer> Start of outer contents. <SPAN id=inner> Inner contents.</SPAN> <SPAN id=sibling> Sibling contents.</SPAN> End of outer contents.</SPAN> End of body contents. </P> </BODY></HTML>
#inner { float: right; width: 130px; color: blue }#sibling { color: red }
#inner { float: right; width: 130px; color: blue }#sibling { clear: right; color: red }
9.8.4Absolute positioning
#outer { position: absolute; top: 200px; left: 200px; width: 200px; color: red;}#inner { color: blue }
#outer { position: relative; color: red }#inner { position: absolute; top: 200px; left: -100px; height: 130px; width: 130px; color: blue;}
#outer { color: red }#inner { position: absolute; top: 200px; left: -100px; height: 130px; width: 130px; color: blue;}
<P style="position: relative; margin-right: 10px; left: 10px;">I used two red hyphens to serve as a change bar. Theywill "float" to the left of the line containing THIS<SPAN style="position: absolute; top: auto; left: -1em; color: red;">--</SPAN>word.</P>
9.9Layered presentation
9.9.1Specifying the stack level: the
Name: z-index Value: auto |<integer> |inherit Initial: auto Applies to: positioned elements Inherited: no Percentages: N/A Media: visual Computed value: as specified <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><HTML> <HEAD> <TITLE>Z-order positioning</TITLE> <STYLE type="text/css"> .pile { position: absolute; left: 2in; top: 2in; width: 3in; height: 3in; } </STYLE> </HEAD> <BODY> <P> <IMG src="butterfly.png" alt="A butterfly image" > <DIV > This text will overlay the butterfly image. </DIV> <DIV> This text will be beneath everything. </DIV> <DIV > This text will underlay text1, but overlay the butterfly image </DIV> </BODY></HTML>
9.10Text direction:the'direction'and'unicode-bidi'properties
Name: direction Value: ltr | rtl |inherit Initial: ltr Applies to: all elements, but see prose Inherited: yes Percentages: N/A Media: visual Computed value: as specified Name: unicode-bidi Value: normal | embed | bidi-override |inherit Initial: normal Applies to: all elements, but see prose Inherited: no Percentages: N/A Media: visual Computed value: as specified <HEBREW> <PAR>HEBREW1 HEBREW2 english3 HEBREW4 HEBREW5</PAR> <PAR>HEBREW6 <EMPH>HEBREW7</EMPH> HEBREW8</PAR></HEBREW><ENGLISH> <PAR>english9 english10 english11 HEBREW12 HEBREW13</PAR> <PAR>english14 english15 english16</PAR> <PAR>english17 <HE-QUO>HEBREW18 english19 HEBREW20</HE-QUO></PAR></ENGLISH>
/* Rules for bidi */HEBREW, HE-QUO {direction: rtl; unicode-bidi: embed}ENGLISH {direction: ltr; unicode-bidi: embed} /* Rules for presentation */HEBREW, ENGLISH, PAR {display: block}EMPH {font-weight: bold}
5WERBEH 4WERBEH english3 2WERBEH 1WERBEH 8WERBEH7WERBEH 6WERBEHenglish9 english10 english11 13WERBEH 12WERBEHenglish14 english15 english16english17 20WERBEH english19 18WERBEH
2WERBEH 1WERBEH -EH 4WERBEH english3 5WERB -EH7WERBEH 6WERBEH 8WERBenglish9 english10 en-glish11 12WERBEH13WERBEHenglish14 english15english16english17 18WERBEH20WERBEH english19
[8]ページ先頭