• CSS
  • Your first website: Styling the content
  • Challenge: Styling a biography page
  • Combinators
  • Sizing items in CSS
  • Styling tables
  • Challenge: A cool-looking box
  • Styling links
  • Introduction to CSS layout
  • CSS grid layout
  • CSS reference
  • Modules
    1. CSS basic user interface
    2. CSS cascading and inheritance
    3. CSS conditional rules
    4. CSS display
    5. CSS fonts
    6. CSS images
    7. CSS masking
    8. CSS namespaces
    9. CSS paged media
    10. CSS ruby layout
    11. CSS scroll-driven animations
    12. CSS shapes
    13. CSS text decoration
    14. CSS view transitions
    15. -moz-float-edge Non-standard Deprecated
    16. -moz-force-broken-image-icon Non-standard Deprecated
    17. -moz-image-region Non-standard
    18. -moz-orient Non-standard
    19. -moz-user-focus Non-standard Deprecated
    20. -moz-user-input Non-standard Deprecated
  • -webkit-*
    1. -webkit-border-before Non-standard
    2. -webkit-box-reflect Non-standard
    3. -webkit-mask-box-image Non-standard
    4. -webkit-mask-composite Non-standard
    5. -webkit-mask-position-x Non-standard
    6. -webkit-mask-position-y Non-standard
    7. -webkit-mask-repeat-x Non-standard
    8. -webkit-mask-repeat-y Non-standard
    9. -webkit-tap-highlight-color Non-standard
    10. -webkit-text-security Non-standard
    11. -webkit-touch-callout Non-standard
  • align-items
  • anchor-name Experimental
  • animation-*
    1. animation-direction
    2. animation-name
    3. animation-range Experimental
    4. animation-range-end Experimental
    5. animation-range-start Experimental
    6. animation-timeline Experimental
    7. backdrop-filter
    8. background-blend-mode
    9. background-origin
    10. background-repeat
    11. border-block
    12. border-block-end-style
    13. border-block-start-style
    14. border-bottom
    15. border-bottom-style
    16. border-end-end-radius
    17. border-image-repeat
    18. border-inline
    19. border-inline-end-style
    20. border-inline-start-style
    21. border-left
    22. border-radius
    23. border-right-width
    24. border-style
    25. border-top-right-radius
    26. bottom
    27. box-*
      1. box-align Non-standard Deprecated
      2. box-direction Non-standard Deprecated
      3. box-flex Non-standard Deprecated
      4. box-flex-group Non-standard Deprecated
      5. box-lines Non-standard Deprecated
      6. box-ordinal-group Non-standard Deprecated
      7. box-orient Non-standard Deprecated
      8. box-pack Non-standard Deprecated
      9. break-before
      10. clear
      11. clip-*
        1. clip Deprecated
        2. color-interpolation
        3. column-fill
        4. column-rule-style
        5. columns
        6. contain-*
          1. contain-intrinsic-inline-size
          2. container-name
          3. counter-increment
          4. cx
          5. display
          6. field-sizing Experimental
          7. fill-*
            1. filter
            2. flex-*
              1. flex-flow
              2. float
              3. font-family
              4. font-optical-sizing
              5. font-smooth Non-standard
              6. font-stretch Deprecated
              7. font-synthesis-position Experimental
              8. font-variant
              9. font-variant-emoji
              10. font-variation-settings
              11. grid
              12. grid-auto-rows
              13. grid-row
              14. grid-template-areas
              15. height
              16. image-orientation
              17. image-resolution Experimental
            3. inset-block
            4. inset-inline-end
            5. interpolate-size Experimental
            6. justify-self
          8. line-break
          9. line-height-step Experimental
        7. list-*
          1. list-style-type
        8. margin-*
          1. margin-block-start
          2. margin-inline-start
          3. margin-trim Experimental
        9. marker-*
          1. marker-start
        10. mask-*
          1. mask-border-outset
          2. mask-border-width
          3. mask-mode
          4. mask-size
          5. math-shift Experimental
          6. max-inline-size
          7. min-inline-size
          8. object-position
          9. offset-*
            1. offset-path
            2. order
            3. outline-offset
            4. overflow-anchor
            5. overflow-wrap
            6. overlay Experimental
            7. overscroll-*
              1. overscroll-behavior-x
              2. padding-block-end
              3. padding-inline-end
              4. padding-top
            8. page-*
              1. page-break-after Deprecated
              2. page-break-before Deprecated
              3. page-break-inside Deprecated
            9. place-content
            10. position
            11. position-anchor Experimental
            12. position-area Experimental
            13. position-try Experimental
            14. position-try-fallbacks Experimental
            15. position-try-order Experimental
            16. position-visibility Experimental
          10. resize
          11. ruby-align
          12. scale
          13. scroll-*
            1. scroll-margin-block-end
            2. scroll-margin-inline-end
            3. scroll-margin-top
            4. scroll-padding-block-start
            5. scroll-padding-inline-start
            6. scroll-snap-align
            7. scroll-timeline Experimental
            8. scroll-timeline-axis Experimental
            9. scroll-timeline-name Experimental
          14. scrollbar-*
            1. shape-image-threshold
            2. speak-as Experimental
            3. stroke-dasharray
            4. stroke-miterlimit
            5. table-layout
            6. text-*
              1. text-box
              2. text-decoration
              3. text-decoration-skip Experimental
              4. text-emphasis
              5. text-indent
              6. text-rendering
              7. text-size-adjust Experimental
              8. text-spacing-trim Experimental
              9. text-wrap
              10. timeline-scope Experimental
              11. transform-box
              12. transition-behavior
              13. transition-timing-function
            7. user-modify Non-standard Deprecated
            8. view-timeline Experimental
            9. view-timeline-axis Experimental
            10. view-timeline-inset Experimental
            11. view-timeline-name Experimental
            12. white-space-collapse
            13. word-break
            14. y
            15. Attribute selectors
            16. Universal selectors
          15. Combinators
            1. Column combinator Experimental
            2. Selector list
            3. :-moz-broken Non-standard Deprecated
            4. :-moz-drag-over Non-standard
            5. :-moz-first-node Experimental Non-standard
            6. :-moz-handler-blocked Non-standard
            7. :-moz-handler-crashed Non-standard
            8. :-moz-handler-disabled Non-standard
            9. :-moz-last-node Experimental Non-standard
            10. :-moz-loading Non-standard
            11. :-moz-locale-dir(ltr) Non-standard
            12. :-moz-locale-dir(rtl) Non-standard
            13. :-moz-only-whitespace Non-standard
            14. :-moz-submit-invalid Non-standard
            15. :-moz-suppressed Non-standard
            16. :-moz-user-disabled Non-standard
            17. :-moz-window-inactive Non-standard
          16. :blank Experimental
          17. :current Experimental
          18. :disabled
          19. :first-child
          20. :focus-within
        11. :has()
        12. :hover
        13. :is()
        14. :left
        15. :local-link Experimental
        16. :nth-child()
        17. :only-child
        18. :out-of-range
        19. :placeholder-shown
        20. :read-write
        21. :scope
        22. :target
        23. :target-within Experimental
        24. :visited
        25. ::-moz-color-swatch Non-standard
        26. ::-moz-focus-inner Experimental Non-standard
        27. ::-moz-list-bullet Experimental Non-standard
        28. ::-moz-list-number Experimental Non-standard
        29. ::-moz-meter-bar Non-standard
        30. ::-moz-progress-bar Experimental Non-standard
        31. ::-moz-range-progress Non-standard
        32. ::-moz-range-thumb Non-standard
        33. ::-moz-range-track Non-standard
      12. ::-webkit-*
        1. ::-webkit-inner-spin-button Non-standard
        2. ::-webkit-meter-bar Non-standard Deprecated
        3. ::-webkit-meter-even-less-good-value Non-standard
        4. ::-webkit-meter-inner-element Non-standard
        5. ::-webkit-meter-optimum-value Non-standard
        6. ::-webkit-meter-suboptimum-value Non-standard
        7. ::-webkit-progress-bar Non-standard
        8. ::-webkit-progress-inner-element Non-standard
        9. ::-webkit-progress-value Non-standard
        10. ::-webkit-scrollbar Non-standard
        11. ::-webkit-search-cancel-button Non-standard
        12. ::-webkit-search-results-button Non-standard
        13. ::-webkit-slider-runnable-track Non-standard
        14. ::-webkit-slider-thumb Non-standard
      13. ::cue
      14. ::first-line
      15. ::part()
      16. ::spelling-error
      17. ::view-transition-image-pair
      18. @color-profile
      19. @document Non-standard Deprecated
      20. @import
      21. @namespace
      22. @position-try Experimental
      23. @supports
      24. -moz-image-rect Non-standard Deprecated
      25. anchor-size() Experimental
      26. anchor() Experimental
      27. attr()
      28. calc-size() Experimental
      29. color-mix()
      30. cos()
      31. cubic-bezier()
      32. element() Experimental
      33. fit-content()
      34. hwb()
      35. inset()
      36. lch()
      37. log()
      38. min()
      39. oklch()
      40. palette-mix() Experimental
      41. scale3d()
      42. scroll() Experimental
      43. sin()
      44. steps()
      45. translateZ()
      46. view() Experimental
      47. <angle-percentage>
      48. <blend-mode>
      49. <color-interpolation-method>
      50. <custom-ident>
      51. <display-inside>
      52. <display-outside>
      53. <frequency-percentage>
      54. <hex-color>
      55. <image>
      56. <line-style>
      57. <overflow>
      58. <position-area> Experimental
      59. <resolution>
      60. <shape> Deprecated
      61. <time-percentage>
      62. Guides
      63. Animations
        1. Box alignment in block layout
        2. Introduction to the CSS basic box model
        3. Using relative colors
        4. Basic concepts of multi-column layout
        5. Handling content breaks in multi-column layout
      64. Conditional rules
        1. Using container size and style queries
      65. CSSOM view
        1. Introduction to formatting contexts
        2. Basic concepts of flexbox
        3. Controlling ratios of flex items along the main axis
        4. Variable fonts guide
      66. Grid
        1. Grid template areas
        2. Grids, logical values, and writing modes
        3. Masonry layout Experimental
      67. Images
        1. Basic concepts of logical properties and values
        2. Using CSS math functions
      68. Media queries
        1. Printing
      69. Nesting style rules
        1. Understanding z-index
      70. Scroll snap
        1. Basic shapes with shape-outside
        2. Using CSS transitions
      71. Center an element
      72. List group with badges
      73. Color picker tool
      74. Learn more
      75. See full compatibility
  • The display grid or flex.

    Formally, the display property sets an element's inner and outer display types. The outer type sets an element's participation in flow layout; the inner type sets the layout of children. Some values of display are fully defined in their own individual specifications; for example the detail of what happens when display: flex is declared is defined in the CSS Flexible Box Model specification.

    Try it

    display: block;
    
    display: inline-block;
    
    display: none;
    
    display: flex;
    
    display: grid;
    
    <p>
      Apply different <code>display</code> values on the dashed orange-bordered
      <code>div</code>, which contains three child elements.
    </p>
    <section class="default-example" id="default-example">
      <div class="example-container">
        Some text A.
        <div id="example-element">
          <div class="child">Child 1</div>
          <div class="child">Child 2</div>
          <div class="child">Child 3</div>
        </div>
        Some text B.
      </div>
    </section>
    
    .example-container {
      width: 100%;
      height: 100%;
    }
    
    code {
      background: #8888;
    }
    
    #example-element {
      border: 3px dashed orange;
    }
    
    .child {
      display: inline-block;
      padding: 0.5em 1em;
      background-color: #ccccff;
      border: 1px solid #ababab;
      color: black;
    }
    

    Syntax

    css
    /* precomposed values */
    display: block;
    display: inline;
    display: inline-block;
    display: flex;
    display: inline-flex;
    display: grid;
    display: inline-grid;
    display: flow-root;
    
    /* box generation */
    display: none;
    display: contents;
    
    /* multi-keyword syntax */
    display: block flex;
    display: block flow;
    display: block flow-root;
    display: block grid;
    display: inline flex;
    display: inline flow;
    display: inline flow-root;
    display: inline grid;
    
    /* other values */
    display: table;
    display: table-row; /* all table elements have an equivalent CSS display value */
    display: list-item;
    
    /* Global values */
    display: inherit;
    display: initial;
    display: revert;
    display: revert-layer;
    display: unset;
    

    The CSS display property is specified using keyword values.

    Grouped values

    The keyword values can be grouped into six value categories.

    Outside

    <display-outside>

    These keywords specify the element's outer display type, which is essentially its role in flow layout:

    block

    The element generates a block box, generating line breaks both before and after the element when in the normal flow.

    inline

    The element generates one or more inline boxes that do not generate line breaks before or after themselves. In normal flow, the next element will be on the same line if there is space.

    Note: When browsers that support multi-keyword syntax encounter a display property that only has an outer value (e.g., display: block or display: inline), the inner value is set to flow (e.g., display: block flow and display: inline flow).

    Note: To be sure layouts work on older browsers, you may use single-value syntax, for example display: inline flex could have the following fallback

    css
    .container {
      display: inline-flex;
      display: inline flex;
    }
    

    See Using the multi-keyword syntax with CSS display for more information.

    Inside

    <display-inside>

    These keywords specify the element's inner display type, which defines the type of formatting context that its contents are laid out in (assuming it is a non-replaced element):

    flow

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

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

    Depending on the value of other properties (such as block formatting context (BFC) for its contents or integrates its contents into its parent formatting context.

    flow-root

    The element generates a block box that establishes a new block formatting context, defining where the formatting root lies.

    table

    These elements behave like HTML <table> elements. It defines a block-level box.

    flex

    The element behaves like a block-level element and lays out its content according to the flexbox model.

    grid

    The element behaves like a block-level element and lays out its content according to the grid model.

    ruby

    The element behaves like an inline-level element and lays out its content according to the ruby formatting model. It behaves like the corresponding HTML <ruby> elements.

    Note: When browsers that support multi-keyword syntax encounter a display property that only has an inner value (e.g., display: flex or display: grid), the outer value is set to block (e.g., display: block flex and display: block grid).

    List Item

    <display-listitem>

    The element generates a block box for the content and a separate list-item inline box.

    A single value of list-item will cause the element to behave like a list item. This can be used together with list-style-position.

    list-item can also be combined with any <display-inside> keyword.

    Note: In browsers that support the multi-keyword syntax, if no inner value is specified, it will default to flow. If no outer value is specified, the principal box will have an outer display type of block.

    Internal

    <display-internal>

    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 "internal" display values, which only have meaning within that particular layout mode.

    table-row-group

    These elements behave like <tbody> HTML elements.

    table-header-group

    These elements behave like <thead> HTML elements.

    These elements behave like <tfoot> HTML elements.

    table-row

    These elements behave like <tr> HTML elements.

    table-cell

    These elements behave like <td> HTML elements.

    table-column-group

    These elements behave like <colgroup> HTML elements.

    table-column

    These elements behave like <col> HTML elements.

    table-caption

    These elements behave like <caption> HTML elements.

    ruby-base

    These elements behave like <rb> HTML elements.

    ruby-text

    These elements behave like <rt> HTML elements.

    ruby-base-container

    These elements are generated as anonymous boxes.

    ruby-text-container

    These elements behave like <rtc> HTML elements.

    Box

    <display-box>

    These values define whether an element generates display boxes at all.

    contents

    These elements don't produce a specific box by themselves. They are replaced by their pseudo-box and their child boxes. Please note that the CSS Display Level 3 spec defines how the contents value should affect "unusual elements" — elements that aren't rendered purely by CSS box concepts such as replaced elements. See Appendix B: Effects of display: contents on Unusual Elements for more details.

    none

    Turns off the display of an element so that it has no effect on layout (the document is rendered as though the element did not exist). All descendant elements also have their display turned off. To have an element take up the space that it would normally take, but without actually rendering anything, use the visibility property instead.

    Precomposed

    <display-legacy>

    CSS 2 used a single-keyword, precomposed syntax for the display property, requiring separate keywords for block-level and inline-level variants of the same layout mode.

    inline-block

    The element generates a block box that will be flowed with surrounding content as if it were a single inline box (behaving much like a replaced element would).

    It is equivalent to inline flow-root.

    inline-table

    The inline-table value does not have a direct mapping in HTML. It behaves like an HTML <table> element, but as an inline box, rather than a block-level box. Inside the table box is a block-level context.

    It is equivalent to inline table.

    inline-flex

    The element behaves like an inline-level element and lays out its content according to the flexbox model.

    It is equivalent to inline flex.

    inline-grid

    The element behaves like an inline-level element and lays out its content according to the grid model.

    It is equivalent to inline grid.

    Which syntax should you use?

    The CSS display module describes a multi-keyword syntax for values you can use with the display property to explicitly define outer and inner display. The single keyword values (precomposed <display-legacy> values) are supported for backward-compatibility.

    For example, using two values you can specify an inline flex container as follows:

    css
    .container {
      display: inline flex;
    }
    

    This can also be specified using the legacy single value:

    css
    .container {
      display: inline-flex;
    }
    

    For more information on these changes, see the Using the multi-keyword syntax with CSS display guide.

    Global

    css
    /* Global values */
    display: inherit;
    display: initial;
    display: unset;
    

    Description

    The individual pages for the different types of value that display can have set on it feature multiple examples of those values in action — see the Syntax section. In addition, see the following material, which covers the various values of display in depth.

    Multi-keyword values

    CSS Flow Layout (display: block, display: inline)

    display: flex

    display: grid

    Animating display

    Supporting browsers animate display with a discrete animation type. This generally means that the property will flip between two values 50% through animating between the two.

    There is one exception, which is when animating to or from display: none. In this case, the browser will flip between the two values so that the animated content is shown for the entire animation duration. So for example:

    • When animating display from none to block (or another visible display value), the value will flip to block at 0% of the animation duration so it is visible throughout.
    • When animating display from block (or another visible display value) to none, the value will flip to none at 100% of the animation duration so it is visible throughout.

    This behavior is useful for creating entry/exit animations where you want to for example remove a container from the DOM with display: none, but have it fade out with opacity rather than disappearing immediately.

    When animating display with Using CSS animations for an example.

    When animating display with CSS transitions, two additional features are needed:

    • @starting-style provides starting values for properties you want to transition from when the animated element is first shown. This is needed to avoid unexpected behavior. By default, CSS transitions are not triggered on an element's first style update or when the display type changes from none to another type.
    • transition shorthand) to enable display transitions.

    For examples of transitioning the display property, see the accessibility tree. This will cause the element and all its descendant elements to no longer be announced by screen reading technology.

    If you want to visually hide the element, a more accessible alternative is to use a combination of properties to remove it visually from the screen but still make it available to assistive technology such as screen readers.

    While display: none hides content from the accessibility tree, elements that are hidden but are referenced from visible elements' aria-describedby or aria-labelledby attributes are exposed to assistive technologies.

    display: contents

    Current implementations in some browsers will remove from the accessibility tree any element with a display value of contents (but descendants will remain). This will cause the element itself to no longer be announced by screen reading technology. This is incorrect behavior according to the CSS specification.

    Tables

    Formal definition

    Animation typeDiscrete behavior except when animating to or from none is visible for the entire duration

    Formal syntax

    display = 
    |
    <display-listitem> |
    <display-outside> ]

    <display-outside> =
    block |
    table ? ]|
    table-footer-group |
    table-column |
    ruby-base-container |
    inline-flex |
    inline-grid

    Examples

    display value comparison

    In this example we have two block-level container elements, each one with three inline children. Below that, we have a select menu that allows you to apply different display values to the containers, allowing you to compare and contrast how the different values affect the element's layout, and that of their children.

    We have included background-color on the containers and their children, so that it is easier to see the effect the display values are having.

    HTML

    html
    <article class="container">
      <span>First</span>
      <span>Second</span>
      <span>Third</span>
    </article>
    
    <article class="container">
      <span>First</span>
      <span>Second</span>
      <span>Third</span>
    </article>
    
    <div>
      <label for="display">Choose a display value:</label>
      <select id="display">
        <option selected>block</option>
        <option>block flow</option>
        <option>inline</option>
        <option>inline flow</option>
        <option>flow</option>
        <option>flow-root</option>
        <option>block flow-root</option>
        <option>table</option>
        <option>block table</option>
        <option>flex</option>
        <option>block flex</option>
        <option>grid</option>
        <option>block grid</option>
        <option>list-item</option>
        <option>block flow list-item</option>
        <option>inline flow list-item</option>
        <option>block flow-root list-item</option>
        <option>inline flow-root list-item</option>
        <option>contents</option>
        <option>none</option>
        <option>inline-block</option>
        <option>inline flow-root</option>
        <option>inline-table</option>
        <option>inline table</option>
        <option>inline-flex</option>
        <option>inline flex</option>
        <option>inline-grid</option>
        <option>inline grid</option>
      </select>
    </div>
    

    CSS

    css
    html {
      font-family: helvetica, arial, sans-serif;
      letter-spacing: 1px;
      padding-top: 10px;
    }
    
    article {
      background-color: red;
    }
    
    article span {
      background-color: black;
      color: white;
      margin: 1px;
    }
    
    article,
    span {
      padding: 10px;
      border-radius: 7px;
    }
    
    article,
    div {
      margin: 20px;
    }
    

    JavaScript

    js
    const articles = document.querySelectorAll(".container");
    const select = document.querySelector("select");
    
    function updateDisplay() {
      articles.forEach((article) => {
        article.style.display = select.value;
      });
    }
    
    select.addEventListener("change", updateDisplay);
    
    updateDisplay();
    

    Result

    Note that some multi-keyword values are added for illustration which have the following equivalents:

    • block = block flow
    • inline = inline flow
    • flow = block flow
    • flow-root = block flow-root
    • table = block table
    • flex = block flex
    • grid = block grid
    • list-item = block flow list-item
    • inline-block = inline flow-root
    • inline-table = inline table
    • inline-flex = inline flex
    • inline-grid = inline grid

    You can find more examples in the pages for each separate display type under Grouped values.

    Specifications

    Specification
    Scalable Vector Graphics (SVG) 2
    # VisibilityControl

    Browser compatibility

    BCD tables only load in the browser

    See also