• CSS
  • Beginner's tutorials
  • CSS getting started
  • Pseudo-classes and elements
  • Values and units
  • Images, media, forms
  • Challenge: Fancy letterhead
  • Styling lists
  • CSS layout
    1. Flexbox
    2. Challenge: Fundamental layout
  • CSS backgrounds and borders
  • CSS box sizing
  • CSS compositing and blending
  • CSS custom properties for cascading variables
  • CSS font loading
  • CSS grid layout
  • CSS logical properties and values
  • CSS multi-column layout
  • CSS overscroll behavior
  • CSS pseudo-elements
  • CSS scroll snap
  • CSS shadow parts
  • CSS text
  • CSS values and units
  • Properties
    1. -moz-*
      1. -moz-float-edge Non-standard Deprecated
      2. -moz-force-broken-image-icon Non-standard Deprecated
      3. -moz-image-region Non-standard
      4. -moz-orient Non-standard
      5. -moz-user-focus Non-standard Deprecated
      6. -moz-user-input Non-standard Deprecated
    2. -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
    3. align-items
    4. anchor-name
    5. animation-*
      1. animation-direction
      2. animation-name
      3. animation-range-start
      4. aspect-ratio
      5. background-attachment
      6. background-image
      7. background-position-y
      8. border
      9. border-block-end-color
      10. border-block-start-color
      11. border-block-width
      12. border-bottom-right-radius
      13. border-color
      14. border-image-outset
      15. border-image-width
      16. border-inline-end-color
      17. border-inline-start-color
      18. border-inline-width
      19. border-left-width
      20. border-right-style
      21. border-start-start-radius
      22. border-top-left-radius
      23. border-width
    6. box-align Non-standard Deprecated
    7. box-direction Non-standard Deprecated
    8. box-flex Non-standard Deprecated
    9. box-flex-group Non-standard Deprecated
    10. box-lines Non-standard Deprecated
    11. box-ordinal-group Non-standard Deprecated
    12. box-orient Non-standard Deprecated
    13. box-pack Non-standard Deprecated
    14. break-before
    15. clear
    16. 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-try-fallbacks
          12. position-visibility Experimental
        10. reading-flow Experimental
        11. reading-order Experimental
        12. row-gap
        13. ry
        14. scroll-margin-block
        15. scroll-margin-inline
        16. scroll-margin-right
        17. scroll-marker-group Experimental
        18. scroll-padding-block-start
        19. scroll-padding-inline-start
        20. scroll-snap-align
        21. scroll-timeline-axis
        22. scrollbar-width
      11. shape-*
        1. shape-rendering
      12. speak-as Experimental
      13. stroke-dasharray
      14. stroke-miterlimit
      15. table-layout
      16. 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. top
        11. transform-origin
        12. transition-delay
        13. translate
        14. user-modify Non-standard Deprecated
        15. view-timeline
        16. view-transition-class
        17. white-space-collapse
        18. word-break
        19. y
        20. Attribute selectors
        21. Universal selectors
      17. 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
      18. :blank Experimental
      19. :current Experimental
      20. :disabled
      21. :first-child
      22. :focus-within
    17. :has()
    18. :hover
    19. :is()
    20. :left
    21. :local-link Experimental
    22. :nth-child()
    23. :only-child
    24. :out-of-range
    25. :placeholder-shown
    26. :read-write
    27. :scope
    28. :target
    29. :target-current Experimental
    30. :target-within Experimental
  • :visited
  • ::-moz-color-swatch Non-standard
  • ::-moz-focus-inner Non-standard Deprecated
  • ::-moz-list-bullet Experimental Non-standard
  • ::-moz-list-number Experimental Non-standard
  • ::-moz-meter-bar Non-standard
  • ::-moz-progress-bar Experimental Non-standard
  • ::-moz-range-progress Non-standard
  • ::-moz-range-thumb Non-standard
  • ::-moz-range-track Non-standard
  • ::-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
  • ::checkmark Experimental
  • ::column Experimental
  • ::first-letter
  • ::marker
  • ::picker-icon Experimental
  • ::picker() Experimental
  • ::scroll-button() Experimental
  • ::scroll-marker Experimental
  • ::scroll-marker-group Experimental
  • ::target-text
  • ::view-*
    1. ::view-transition-new()
    2. @color-profile
    3. @document Non-standard Deprecated
    4. @import
    5. @namespace
    6. @position-try Experimental
    7. @supports
    8. -moz-image-rect Non-standard Deprecated
    9. anchor-size() Experimental
    10. anchor() Experimental
    11. attr()
    12. calc-size() Experimental
    13. color-mix()
    14. contrast-color() Experimental
    15. counters()
    16. drop-shadow()
    17. element() Experimental
    18. fit-content()
    19. hwb()
    20. inset()
    21. lch()
    22. log()
    23. min()
    24. oklch()
    25. palette-mix() Experimental
    26. scale3d()
    27. sepia()
    28. translate3d()
    29. var()
    30. <absolute-size>
    31. <baseline-position>
    32. <calc-keyword>
    33. <content-distribution>
    34. <dimension>
    35. <display-legacy>
    36. <filter-function>
    37. <generic-family>
    38. <hue>
    39. <length-percentage>
    40. <number>
    41. <position-area>
    42. <resolution>
    43. <shape> Deprecated
    44. <time-percentage>
    45. Guides
    46. Anchor positioning
      1. Using animations
    47. Backgrounds and borders
      1. Overview
      2. Box alignment in multi-column layout
    48. Box model
      1. Inheritance
      2. Using custom properties
    49. Colors
      1. Using color wisely
      2. Styling columns
      3. Handling content breaks
    50. Conditional rules
      1. Using containment
      2. Block and inline layout
      3. In flow and out of flow
      4. Inline formatting context
      5. Basic concepts
      6. Controlling flex item ratios
      7. Variable fonts
      8. Grid template areas
      9. Aligning items
      10. Grid layout and accessibility
      11. Masonry layout Experimental
    51. Images
      1. Using counters
      2. Logical properties for margins, borders, and padding
      3. Testing media queries programmatically
      4. Using nesting
    52. Overflow
      1. Understanding z-index
      2. Basic concepts
      3. Selectors and combinators
      4. Image-based shapes
      5. At-rule functions
      6. Using transitions
    53. Values and units
      1. Using math functions
    54. Center an element
    55. List group with badges
    56. Color picker tool
    57. CSS) language is to allow a browser engine to paint elements of the page with specific features, like colors, positioning, or decorations. The CSS syntax reflects this goal and its basic building blocks are:

      • The property which is an identifier, that is a human-readable name, that defines which feature is considered.
      • The value which describe how the feature must be handled by the engine. Each property has a set of valid values, defined by a formal grammar, as well as a semantic meaning, implemented by the browser engine.
  • CSS declarations

    Setting CSS properties to specific values is the core function of the CSS language. A property and value pair is called a declaration, and any CSS engine calculates which declarations apply to every single element of a page in order to appropriately lay it out, and to style it.

    Both properties and values are case-insensitive by default in CSS. The pair is separated by a colon, : (U+003A COLON), and white spaces before, between, and after properties and values, but not necessarily inside, are ignored.

    A css declaration is a property value pair, with a colon separating the two entities and a semi-colon closing the declaration.

    There are hundreds of different properties in CSS and a practically endless number of different values. Not all pairs of properties and values are allowed and each property defines what are the valid values. When a value is not valid for a given property, the declaration is deemed invalid and is wholly ignored by the CSS engine.

    CSS declaration blocks

    Declarations are grouped in blocks, that is in a structure delimited by an opening brace, { (U+007B LEFT CURLY BRACKET), and a closing one, } (U+007D RIGHT CURLY BRACKET). Blocks sometimes can be nested, so opening and closing braces must be matched.

    Two braces delimit the start and end of a CSS block, with css content or no content between the braces.

    Such blocks are naturally called declaration blocks and declarations inside them are separated by a semicolon, ; (U+003B SEMICOLON). A declaration block may be empty, that is containing null declaration. White spaces around declarations are ignored. The last declaration of a block doesn't need to be terminated by a semicolon, though it is often considered good style to do it as it prevents forgetting to add it when extending the block with another declaration.

    A CSS declaration block is visualized in the diagram below.

    Within a CSS block, encompassed in braces, semicolons separate declarations, with the final semicolon being optional but recommended as good practice.

    Note: The content of a CSS declaration block, that is a list of semicolon-separated declarations, without the initial and closing braces, can be put inside an HTML style attribute.

    CSS rulesets

    If style sheets could only apply a declaration to each element of a Web page, they would be pretty useless. The real goal is to apply different declarations to different parts of the document.

    CSS allows this by associating conditions with declarations blocks. Each (valid) declaration block is preceded by one or more comma-separated selector list and an associated declarations block, together, are called a ruleset, or often a rule.

    A CSS ruleset (or rule) is visualized in the diagram below.

    A group of comma-separated selectors precedes the brace-delimited declaration block, which contains multiple declarations ending in semicolons.

    As an element of the page may be matched by several selectors, and therefore by several rules potentially containing a given property several times, with different values, the CSS standard defines which one has precedence over the other and must be applied: this is called the cascade algorithm (see Handling conflicts).

    Note: It is important to note that even if a ruleset characterized by a group of selectors is a kind of shorthand replacing rulesets with a single selector each, this doesn't apply to the validity of the ruleset itself.

    This leads to an important consequence: if one single basic selector is invalid, like when using an unknown pseudo-element or pseudo-class, the whole selector is invalid and therefore the entire rule is ignored (as invalid too).

    CSS statements

    Rulesets are the main building blocks of a style sheet, which often consists of only a big list of them. But there is other information that a Web author wants to convey in the style sheet, like the character set, other external style sheets to import, font face or list counter descriptions and many more. It will use other and specific kinds of statements to do that.

    A statement is a building block that begins with any non-space characters and ends at the first closing brace or semicolon (outside a string, non-escaped and not included into another {}, () or [] pair).

    A Venn diagram of statements noting all rulesets are nested statements, while some at-rules are nested statements but most aren't. Anything that is not an at-rule or nested is invalid.

    There are two kinds of statements:

    • Rulesets (or rules) that, as seen, associate a collection of CSS declarations to a condition described by a selector.
    • At-rules that start with an at sign, @ (U+0040 COMMERCIAL AT), followed by an identifier and then continuing up to the end of the statement, that is up to the next semicolon (;) outside of a block, or the end of the next block. Each type of @media or @font-face).

    Any statement which isn't a ruleset or an at-rule is invalid and ignored.

    Nested statements

    There is another group of statements – the nested statements. These are statements that can be used in a specific subset of at-rules – the conditional group rules. These statements only apply if a specific condition is matched: the @media at-rule content is applied only if the device on which the browser runs matches the expressed condition; the @document at-rule content is applied only if the current page matches some conditions, and so on. In CSS1 and CSS2.1, only rulesets could be used inside conditional group rules. That was very restrictive and this restriction was lifted in CSS Conditionals Level 3. Now, though still experimental and not supported by every browser, conditional group rules can contain a wider range of content: rulesets but also some, but not all, at-rules.

    See also