Css Inline Block Style Essay

display: inline-block brought a new way to create side by side boxes that collapse and wrap properly depending on the available space in the containing element. It makes layouts that were previously accomplished with floats easier to create. No need to clear floats anymore.

Compared to display: inline, the major difference is that inline-block allows to set a width and height on the element. Also, with display: inline, top and bottom margins & paddings are not respected, and with display: inline-block they are.

Now, the difference between display: inline-block and display: block is that, with display: block, a line break happens after the element, so a block element doesn’t sit next to other elements. Here are some visual examples:

display: inline

Notice here how the width and height are not respected, and how the padding top and bottom are present, but overlap over the lines above and under.

Cheese and wine ricotta danish fontina. Brie cheesy grin paneer squirty cheese taleggio cheesecake goat taleggiogoat taleggio. Bavarian bergkase emmental fromage cheesecake cheese slices cheesy grin queso caerphilly.

display: inline-block

Here the width, height and padding are respected, but the two copies of the element can still sit side by side.

Cheese and wine ricotta danish fontina. Brie cheesy grin paneer squirty cheese taleggio cheesecake goat taleggiogoat taleggio. Bavarian bergkase emmental fromage cheesecake cheese slices cheesy grin queso caerphilly.

display: block

Here again everything is respected, but the elements don’t sit side by side.

Cheese and wine ricotta danish fontina. Brie cheesy grin paneer squirty cheese taleggio cheesecake goat taleggiogoat taleggio. Bavarian bergkase emmental fromage cheesecake cheese slices cheesy grin queso caerphilly.

Browser Support

Can I Use inline-block? Data on support for the inline-block feature across the major browsers from caniuse.com.

1. Introduction

This section is normative.

CSS takes a source document, organized as a of and , and renders it onto a canvas (such as your screen, a piece of paper, or an audio stream). To do this, it generates an intermediary structure, the , which represents the formatting structure of the rendered document. Each in the box tree represents its corresponding element (or pseudo-element) in space and/or time on the canvas, while each in the box tree likewise represents the contents of its corresponding text nodes.

To create the box tree, CSS first uses cascading and inheritance, to assign a computed value for each CSS property to each element and text node in the source tree. (See [CSS3-CASCADE].)

Then, for each element, CSS generates zero or more boxes as specified by that element’s display property. Typically, an element generates a single box. However, some display values (e.g. display: list-item) generate more than one box (e.g. a principal block box and a marker box). And some values (such as none or contents) cause the element and/or its descendants to not generate any boxes at all. Boxes are assigned the same styles as their generating element, unless otherwise indicated. They’re often referred to by their display type—e.g. a box generated by an element with display: block is called a “block box” or just a “block”.

Similarly, each contiguous sequence of sibling text nodes generates a text run, which is assigned the same styles as the generating text nodes.

An is is a box that is not associated with any element. Anonymous boxes are generated in certain circumstances to fix up the box tree when it requires a particular nested structure that is not provided by the boxes generated from the element tree. For example, a table cell box requires a particular type of parent box (the table row box), and will generate an anonymoustable row box around itself if its parent is not a table row box. (See [CSS2] § 17.2.1.) Unlike element-generated boxes, whose styles inherit strictly through the element tree, anonymous boxes (which only exist in the box tree) inherit through their box tree parentage.

In the course of layout, boxes and text runs can be broken into multiple fragments. This happens, for example, when an inline box and/or text run is broken across lines, or when a block box is broken across pages or columns. A box therefore consists of one or more box fragments, and a text run consists of one or more text fragments. See [CSS3-BREAK] for more information on fragmentation.

Note: Many of the CSS specs were written before this terminology was ironed out, or refer to things incorrectly, so view older specs with caution when they’re using these terms. It should be possible to infer from context which term they really mean. Please report errors in specs when you find them, so they can be corrected.

Note: Further information on the “aural” box tree and its interaction with the display property can be found in the CSS Speech Module. [CSS3-SPEECH]

1.1. Module interactions

This module replaces and extends the definition of the display property defined in [CSS2] section 9.2.4.

None of the properties in this module apply to the or pseudo-elements.

1.2. Values

This specification follows the CSS property definition conventions from [CSS2]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Other CSS modules may expand the definitions of these value types.

In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords keywords as their property value. For readability they have not been repeated explicitly.

2. Box Layout Modes: the display property

The display property defines an element’s , which consists of the two basic qualities of how an element generates boxes:

Text runs have no display type.

Some display values have additional side-effects: such as list-item, which also generates a ::marker pseudo-element, and none, which causes the element’s entire subtree to be left out of the box tree.

Values are defined as follows:

= block | inline | run-in ; = flow | flow-root | table | flex | grid | ruby ; = list-item &&<display-outside>?&& [ flow | flow-root ]? ; = table-row-group | table-header-group | table-footer-group | table-row | table-cell | table-column-group | table-column | table-caption | ruby-base | ruby-text | ruby-base-container | ruby-text-container ; = contents | none ; = inline-block | inline-table | inline-flex | inline-grid ;

The following informative table summarizes the values of display:

Note: Following the precedence rules of “most backwards-compatible, then shortest”, serialization of equivalent display values uses the “Short display” column. [CSSOM]

2.1. Outer Display Roles for Flow Layout: the block, inline, and run-in keywords

The <display-outside> keywords specify the element’s outer display type, which is essentially its role in flow layout. They are defined as follows:

The element generates a when placed in flow layout. [CSS2]
The element generates an when placed in flow layout. [CSS2]
The element generates an run-in box, which is a type of inline-level box with special behavior that attempts to merge it into a subsequent block container. See §3 Run-In Layout for details.

If a <display-outside> value is specified but <display-inside> is omitted, the element’s inner display type defaults to flow.

2.2. Inner Display Layout Models: the flow, flow-root, table, flex, grid, and ruby keywords

The <display-inside> keywords specify the element’s inner display type, which defines the type of formatting context that lays out its contents (assuming it is a non-replaced element). They are defined as follows:

The element lays out its contents using (block-and-inline layout).

If its outer display type is inline or run-in, and it is participating in a block or inline formatting context, then it generates an inline box.

Otherwise it generates a block container box.

Depending on the value of other properties (such as position, float, or overflow) and whether it is itself participating in a block or inline formatting context, it either establishes a new block formatting context for its contents or integrates its contents into its parent formatting context. See CSS2.1 Chapter 9. [CSS2]

The element generates a block container box, and lays out its contents using flow layout. It always establishes a new block formatting context for its contents. [CSS2]
The element generates a principal table wrapper box that establishes a block formatting context, and which contains an additionally-generated table box that establishes a table formatting context. [CSS2]
The element generates a principal flex container box and establishes a flex formatting context. [CSS3-FLEXBOX]
The element generates a principal grid container box, and establishes a grid formatting context. [CSS3-GRID-LAYOUT]
The element generates a principal ruby container box, and establishes a ruby formatting context in addition to integrating its base-level contents into its parent formatting context if it is inline. [CSS3RUBY]

If a <display-inside> value is specified but <display-outside> is omitted, the element’s outer display type defaults to block—except for ruby, which defaults to inline.

2.3. Generating Marker Boxes: the list-item keyword

The keyword causes the element to generate a ::marker pseudo-element box [CSS-PSEUDO-4] with the content specified by its list-style properties (CSS 2.1§12.5 Lists) [CSS2] together with a principal box of the specified type for its own contents.

If no inner display type value is specified, the principal box’s inner display type defaults to flow. If no outer display type value is specified, the principal box’s outer display type defaults to block.

Note: In this level, as restricted in the grammar, list-items are limited to the Flow Layout display types (block/inline/run-in with flow/flow-root inner types). This restriction may be relaxed in a future level of this module.

2.4. Layout-Internal Display Types: the table-* and ruby-* keywords

Some layout models, such as table and ruby, have a complex internal structure, with several different roles that their children and descendants can fill. This section defines those “” display values, which only have meaning within that particular layout mode.

Unless otherwise specified, both the inner display type and the outer display type of elements using these display values are set to the given keyword.

The <display-internal> keywords are defined as follows:

, , , , , ,
The element is an . It generates the appropriate which participates in a table formatting context. See CSS2§17.2[CSS2].

table-cell boxes have a flow-rootinner display type.

The element generates a , which is a block box with special behavior with respect to table and table wrapper boxes. See CSS2§17.2[CSS2].

table-caption boxes have a flow-rootinner display type.

, , ,
The element is an . It generates the appropriate which participates in a ruby formatting context. [CSS3RUBY]

ruby-base and ruby-text have a flowinner display type.

Boxes with layout-specific display types generate anonymous wrapper boxes around themselves when placed in an incompatible parent, as defined by their respective specifications.

For example, Table Layout requires that a table-cell box must have a table-row parent box.

If it is misparented, like so:

<divstyle="display:block;"><divstyle="display:table-cell">...</div></div>

It will generate wrapper boxes around itself, producing a structure like:

block box └anonymous table box └anonymous table-row-group box └anonymous table-row box └table-cell box

Even if the parent is another internal table element, if it’s not the correct one, wrapper boxes will be generated. For example, in the following markup:

<divstyle="display:table;"><divstyle="display:table-row"><divstyle="display:table-cell">...</div></div></div>

Anonymous wrapper box generation will produce:

table box └anonymous table-row-group box └table-row box └table-cell box

This "fix-up" ensures that table layout has a predictable structure to operate on.

2.5. Box Generation: the none and contents keywords

While display can control the types of boxes an element will generate, it can also control whether an element will generate any boxes at all.

The <display-box> keywords are defined as follows:

The element itself does not generate any boxes, but its children and pseudo-elements still generate boxes and text runs as normal. For the purposes of box generation and layout, the element must be treated as if it had been replaced in the element tree by its contents (including both its source-document children and its pseudo-elements, such as ::before and ::after pseudo-elements, which are generated before/after the element’s children as normal).

Note: As only the box tree is affected, any semantics based on the document tree, such as selector-matching, event handling, and property inheritance, are not affected.

This value behaves as display: none on replaced elements and other elements whose rendering is not entirely controlled by CSS; see Appendix B: Effects of display: contents on Unusual Elements for details.

Note: Replaced elements and form controls are treated specially because removing only the element’s own generating box is a more-or-less undefined operation. As this behavior may be refined if use cases (and more precise rendering models) develop, authors should use display: none rather than display: contents on such elements for forward-compatibility.

The element and its descendants generate no boxes or text runs.

Similarly, if a text node is defined to behave as display: none, it generates no text runs.

Elements with either of these values do not have inner or outer display types, because they don’t generate any boxes at all.

Note: As these values cause affected elements to not generate a box, anonymous box generation rules will ignore the elided elements entirely, as if they did not exist in the box tree.

Markup-based relationships, however, are not affected by these values, as they are solely rendering-time effects. For example, although they may affect which table cell appears in a column, they do not affect which table cell is associated with a particular column element. Similarly, they cannot affect which HTML element is associated with a particular table or whether a is the name of a .

2.6. Precomposed Inline-level Display Values

CSS level 2 used a single-keyword syntax for display, requiring separate keywords for block-level and inline-level variants of the same layout mode. These <display-legacy> keywords map as follows:

Behaves as inline flow-root.
Behaves as inline table.
Behaves as inline flex.
Behaves as inline grid.

2.7. Automatic Box Type Transformations

Some layout effects require or of the box type, which sets the box’s outer display type to block or inline (respectively). (This has no effects on display types that generate no box at all, such as none or contents.)

Some examples of this include:

If a block flow box is inlinified, its inner display type is set to flow-root so that it remains a block container. If an inline flow box is inlinified, it recursively inlinifies all of its in-flow children, so that no block-level descendants break up the inline formatting context in which it participates.

If a layout-internal box is blockified, its inner display type converts to flow so that it becomes a block container. Inlinification has no effect on layout-internal boxes. (However, placement in such an inline context will typically cause them to be wrapped in an appropriately-typed anonymous inline-level box.)

The root element’s display type is always blockified. Additionally, a display of contents computes to block on the root element.

Note: There are two methods used to fix up box types when a box is mismatched to its context. One is transformation of the computed value of display, such as blockification and inlinification described here. The other, taking place during box tree construction (after computed values have been determined), is the creation of intermediary anonymous boxes, such as happens in tables, ruby, and flow layout.

3. Run-In Layout

A is a box that merges into a block that comes after it, inserting itself at the beginning of that block’s inline-level content. This is useful for formatting compact headlines, definitions, and other similar things, where the appropriate DOM structure is to have a headline preceding the following prose, but the desired display is an inline headline laying out with the text.

For example, dictionary definitions are often formatted so that the word is inline with the definition: <dl class='dict'> <dt>dictionary <dd>a book that lists the words of a language in alphabetical order and gives their meaning, or that gives the equivalent words in a different language. <dt>glossary <dd>an alphabetical list of terms or words found in or relating to a specific subject, text, or dialect, with explanations; a brief dictionary. </dl> <style> .dict > dt { display: run-in; } .dict > dt::after { content: ": " } </style>

Which is formatted as:

dictionary: a book that lists the words of a language in alphabetical order and explains their meaning. glossary: an alphabetical list of terms or words found in or relating to a specific subject, text, or dialect, with explanations; a brief dictionary.

A run-in box behaves exactly as any other inline-level box, except:

  • A run-in box with a flowinner display typeinlinifies its contents.
  • If a run-in sequence is immediately followed by a block box that does not establish a new block formatting context, it is inserted as direct children of the block box after its ::marker pseudo-element’s boxes (if any), but preceding any other boxes generated by the contents of the block (including the box generated by the ::before pseudo-element, if any). This step recurses if possible (so that the run-in effectively becomes part of the deepest subsequent “paragraph” in its formatting context).

    The reparented content is then formatted as if originally parented there. Note that only layout is affected, not inheritance, because property inheritance for non-anonymous boxes is based only on the element tree.

  • Otherwise, an anonymous block box is generated around the run-in sequence and all immediately following inline-level content (up to, but not including, the next run-in sequence, if any).

A is a maximal sequence of consecutive sibling run-in boxes and intervening white space and/or out-of-flow boxes.

Note: This statement implies that out-of-flow boxes are reparented if they are between two run-in boxes. Another alternative would be to leave behind the intervening out-of-flow boxes, or to have out-of-flow boxes impede the running-in of earlier boxes. Implementers and authors are encouraged to contact the CSSWG if they have a preferred behavior, as this one was picked somewhat at random.

This fixup occurs before the anonymous block and inline box fixup described in CSS2§9.2, and affects the determination of the first formatted line of the affected elements as if the run-in sequence were originally in its final location in the box tree.

Note: As the earliest run-in represents the first text on the first formatted line of its containing block, a ::first-letter pseudo-element applied to that block element selects the first letter of the run-in, rather than the first letter of its own contents.

Note: This run-in model is slightly different from the one proposed in earlier revisions of [CSS2].

4. Becoming a formatting context

In some circumstances (See [CSS-CONTAIN-1] or [CSS3-MULTICOL] for examples), a box may need to . If the box already establishes a formatting context of any kind, this condition is satisfied. Otherwise, it is made to establish a BFC by changing its inner display type to flow-root. This change happens at used value time, and does not affect the computed value of the display property.

Appendix A: Glossary

The following terms are defined here for convenience:

When an element generates one or more boxes, one of them is the principal box, which contains its descendant boxes and generated content, and is also the box involved in any positioning scheme.

Some elements may generate additional boxes in addition to the principal box (such as list-item elements, which generate an additional marker box, or table elements, which generate a principaltable wrapper box and an additional table box). These additional boxes are placed with respect to the principal box.

Content that participates in inline layout. Specifically, inline-level boxes and text.
Content that participates in block layout. Specifically, block-level boxes.
A non-replaced inline-level box whose inner display type is flow. The contents of an inline box participate in the same inline formatting context as the inline box itself.
Used as a shorthand for inline box or inline-level box where unambiguous, or as an adjective meaning inline-level. The latter usage is deprecated.
An inline-level box that is replaced (such as an image) or that establishes a new formatting context (such as an inline-block or inline-table) and cannot split across lines (as inline boxes and ruby containers can).

Any inline-level box whose inner display type is not flow establishes a new formatting context of the specified inner display type.

A block container either contains only inline-level boxes participating in an inline formatting context, or contains only block-level boxes participating in a block formatting context (possibly generating anonymous block boxes to ensure this constraint, as defined in CSS2§9.2.1.1).

A block container that contains only inline-level content establishes a new inline formatting context. The element then also generates a root inline box which wraps all of its inline content. Note, this root inline box concept effectively replaces the "anonymous inline element" concept introduced in CSS2§9.2.2.1.

A block container that contains only block-level boxes establishes a new block formatting context if its parent formatting context is not a block formatting context; otherwise, when participating in a block formatting context itself, it either establishes a new block formatting context for its contents or continues the one in which it participates, as determined by the constraints of other properties (such as overflow or align-content).

A block-level box that is also a block container.

Note: Not all block container boxes are block-level boxes: non-replaced inline blocks and non-replaced table cells, for example, are block containers but not block-level boxes. Similarly, not all block-level boxes are block containers: block-level replaced elments (display: block) and flex containers (display: flex), for example, are not block containers.

Used as a shorthand for block box, block-level box, or block container box, where unambiguous.
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 element is often replaced by the image that its 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).

User agents may consider a replaced element to not have any intrinsic dimensions if it is believed that those dimensions could leak sensitive information to a third party. For example, if an HTML document changed intrinsic size depending on the user’s bank balance, then the UA might want to act as if that resource had no intrinsic dimensions.

The content of replaced elements is not considered in the CSS rendering model.

A rectangle that forms the basis of sizing and positioning for the boxes associated with it (usually the children of the box that generated it). Notably, a containing block is not a box (it is a rectangle), however it is often derived from the dimensions of a box. If properties of a containing block are referenced, they reference the values on the box that generated the containing block. (For the initial containing block, the values are taken from the root element.) See [CSS2]Section 9.1.2 and Section 10.1 for details.
A sequence of successive containing blocks that form an ancestor-descendant chain through the containing block relation. For example, an inline box’s containing block is the content box of its closest block container ancestor; if that block container is an in-flow block, then its containing block is formed by its parent block container; if that grandparent block container is absolutely positioned, then its containing block is the padding edges of its closest positioned ancestor (not necessarily its parent), and so on up to the initial containing block.
The containing block of the root element. See CSS2.1§10.1 for continuous media; and [CSS3PAGE] for paged media.
A formatting context is the environment into which a set of related boxes are laid out. Different formatting contexts lay out their boxes according to different rules. For example, a flex formatting context lays out boxes according to the flex layout rules [CSS3-FLEXBOX], whereas a block formatting context lays out boxes according to the block-and-inline layout rules [CSS2].

When a box establishes a new formatting context (whether that formatting context is of the same type as its parent or not), it essentially creates a new, independent layout environment: except through the sizing of the box itself, the layout of its descendants is (generally) not affected by the the rules and contents of the formatting context outside the box, and vice versa.

For example, in a block formatting context, floated boxes affect the layout of surrounding boxes. But their effects do not escape their formatting context: the box establishing their formatting context grows to fully contain them, and floats from outside that box are not allowed to protrude into and affect the contents inside the box.

As another example, margins do not collapse across formatting context boundaries.

Exclusions are able to affect content across formatting context boundaries. (At time of writing, they are the only layout feature that can.) [CSS3-EXCLUSIONS]

Block and inline formatting contexts are defined in CSS 2.1 Section 9.4.
A block container that establishes a new block formatting context.
Abbreviation for block formatting context or block formatting context root. Has various informal definitions referring to boxes which contain internal floats, exclude external floats, and suppress margin collapsing, and may therefore refer specifically to one of:
  • a block container that establishes a new block formatting context for its contents

  • a block box (i.e. a block-level block container) that establishes a block formatting context for its contents (as distinguished from a block box which does not)

  • (very loosely) any block-level box that establishes a new formatting context (other than an inline formatting context)

A box is out-of-flow if it is floated (via float
Categories: 1

0 Replies to “Css Inline Block Style Essay”

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *