• CSS
  • Beginner's tutorials
  • Getting started with CSS
  • Pseudo-classes and pseudo-elements
  • CSS values and units
  • Images, media, and form elements
  • Challenge: Creating fancy letterheaded paper
  • Styling lists
  • CSS layout
    1. Flexbox
    2. Challenge: Fundamental layout comprehension
  • 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
  • -moz-float-edge Non-standard Deprecated
  • -moz-force-broken-image-icon Non-standard Deprecated
  • -moz-image-region Non-standard
  • -moz-orient Non-standard
  • -moz-user-focus Non-standard Deprecated
  • -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. aspect ratio, browser viewport width or height, user preferences such as preferring reduced motion, data usage, or transparency.

        Media queries are used for the following:

        Note: The examples on this page use CSS's @media for illustrative purposes, but the basic syntax remains the same for all types of media queries.

  • Syntax

    A media query is composed of an optional media type and any number of media feature expressions, which may optionally be combined in various ways using logical operators. Media queries are case-insensitive.

    A media query computes to true when the media type (if specified) matches the device on which a document is being displayed and all media feature expressions compute as true. Queries involving unknown media types are always false.

    Note: A style sheet with a media query attached to its <link> tag will still download even if the query returns false, the download will happen but the priority of downloading will be much lower. Nevertheless, its contents will not apply unless and until the result of the query changes to true. You can read why this happens in Tomayac's blog Why Browser Download Stylesheet with Non-Matching Media Queries.

    Targeting media types

    Media types describe the general category of a given device. Although websites are commonly designed with screens in mind, you may want to create styles that target special devices such as printers or audio-based screen readers. For example, this CSS targets printers:

    css
    @media print {
      /* … */
    }
    

    You can also target multiple devices. For instance, this @media rule uses two media queries to target both screen and print devices:

    css
    @media screen, print {
      /* … */
    }
    

    See media types for the list of available media types. Because media types describe devices in very broad terms, most of the originally-defined media types were deprecated, with just screen, print, and all remaining. To target more specific attributes, use media features instead.

    Targeting media features

    Media features describe the specific characteristics of a given user agent, output device, or environment. For instance, you can apply specific styles to widescreen monitors, computers that use mice, or devices that are being used in low-light conditions. This example applies styles when the user's primary input mechanism (such as a mouse) can hover over elements:

    css
    @media (hover: hover) {
      /* … */
    }
    

    Media features are either range or discrete.

    Discrete features take their value from an enumerated set of possible keyword values. For example, the discrete orientation feature accepts either landscape or portrait.

    css
    @media print and (orientation: portrait) {
      /* … */
    }
    

    Many range features can be prefixed with "min-" or "max-" to express "minimum condition" or "maximum condition" constraints. For example, this CSS will apply styles only if your browser's viewport width is equal to or narrower than 1250px:

    css
    @media (max-width: 1250px) {
      /* … */
    }
    

    The following media queries are equivalent to the above example:

    css
    @media (width <= 1250px) {
      /* … */
    }
    
    @media (1250px >= width) {
      /* … */
    }
    

    With media query range features, you can either use the inclusive min- and max- prefixes or the more concise range syntax operators <= and >=.

    The following media queries are equivalent:

    css
    @media (min-width: 30em) and (max-width: 50em) {
      /* … */
    }
    
    @media (30em <= width <= 50em) {
      /* … */
    }
    
    @media (50em >= width >= 30em) {
      /* … */
    }
    

    The range comparisons above are inclusive. To exclude the comparison value, use < and/or >.

    css
    @media (30em < width < 50em) {
      /* … */
    }
    
    @media (50em > width > 30em) {
      /* … */
    }
    

    If you create a media feature query without specifying a value, the nested styles will be used as long as the feature's value is not 0 or none. For example, this CSS will apply to any device with a color screen:

    css
    @media (color) {
      /* … */
    }
    

    If a feature doesn't apply to the device on which the browser is running, expressions involving that media feature are always false.

    For more Media feature examples, please see the reference page for each specific feature.

    Creating complex media queries

    Sometimes you may want to create a media query that depends on multiple conditions. This is where the logical operators come in: not, and, and only. Furthermore, you can combine multiple media queries into a comma-separated list; this allows you to apply the same styles in different situations, with the contained media queries evaluated as a logical or composition: interpreted as if each media query were within parentheses with an or between them.

    In the previous example, we saw the and operator used to group a media type with a media feature. The and operator can also combine multiple media features within a single media query. The not operator negates a media query, or a media feature when used with brackets, basically reversing their normal meanings. The or operator can, under certain conditions, be used to combine multiple media features within a single media query. Lastly, the only operator was used to prevent older browsers from applying the styles without evaluating the media feature expressions but it has no effect in modern browsers.

    Note: In most cases, the all media type is used by default when no other type is specified. However, if you use the only operator, you must explicitly specify a media type. You can see only screen or only print as a whole.

    Combining multiple types or features

    The and keyword combines a media feature with a media type or other media features. This example combines two media features to restrict styles to landscape-oriented devices with a width of at least 30 ems:

    css
    @media (min-width: 30em) and (orientation: landscape) {
      /* … */
    }
    

    To limit the styles to devices with a screen, you can chain the media features to the screen media type:

    css
    @media screen and (min-width: 30em) and (orientation: landscape) {
      /* … */
    }
    

    Testing for multiple queries

    You can use a comma-separated list of media queries to apply styles when the user's device matches any one of various media types, features, or states.

    The following rule contains two media queries. The block's styles will apply if either the user's device has a height of 680px or more or if the browser viewport is in portrait mode (the viewport height is greater than the viewport width):

    css
    @media (min-height: 680px), screen and (orientation: portrait) {
      /* … */
    }
    

    In this example, if the user is printing to a PDF and the page height is 800px, the media query returns true because the first query component — which tests whether the viewport has a height of 680px or more — is true. Likewise, if a user is on a smartphone in portrait mode with a viewport height of 480px, the media query returns true because the second query component is true.

    In a comma-separated list of media queries, the individual media queries end at the comma or, in the case of the last media query in the list, at the opening bracket ({).

    Inverting a query's meaning

    The not keyword inverts the meaning of a single media query. For example, the CSS styles in this media query will apply to everything except printed media:

    css
    @media not print {
      /* … */
    }
    

    The not negates only the media query it is applied to. The not, without parenthesis, negates all the features within the media query in which it is contained. This means, in a comma-separated list of media queries, each not applies to the single query it is contained within, applying to all the features within that single query. In this example, the not applies to the first media query, which concludes at the first comma:

    css
    @media not screen and (color), print and (color) {
      /* … */
    }
    

    The above query is evaluated like this:

    css
    @media (not (screen and (color))), print and (color) {
      /* … */
    }
    

    Both examples are valid. Media conditions can be grouped by wrapping them in parentheses (()). These groups can then be nested within a condition the same as a single media query.

    The not is evaluated last in a media query, meaning it applies to the entire media query, not to a single feature within a query, as if an open parenthesis was added immediately after the not and closed at the end of the media query.

    The following query:

    css
    @media not all and (monochrome) {
      /* … */
    }
    

    is evaluated like this:

    css
    @media not (all and (monochrome)) {
      /* … */
    }
    

    It is not evaluated like this:

    css
    @media (not all) and (monochrome) {
      /* … */
    }
    

    To negate a single feature within a media query, use parenthesis. Encompassing a not and a media feature in parentheses limits the components of the query that get negated.

    In this example, we negate the hover media feature but not the all media type:

    css
    @media all and (not(hover)) {
      /* … */
    }
    

    The not(hover) matches if the device has no hover capability. In this case, because of the parentheses, the not applies to hover but not to all.

    Improving compatibility with older browsers

    The only keyword prevents older browsers that do not support media queries with media features from applying the given styles. It has no effect on modern browsers.

    css
    @media only screen and (color) {
      /* … */
    }
    

    Testing for multiple features with or

    You can use or to test for a match among more than one feature, resolving to true if any of the features are true. For example, the following query tests for devices that have a monochrome display or hover capability:

    css
    @media (not (color)) or (hover) {
      /* … */
    }
    

    Note that you cannot use the or operator on the same level as the and and not operators. You can either separate the media features with a comma or use parenthesis to group sub-expressions of media features to clarify the order of evaluation.

    For example, the following queries are both valid:

    css
    @media ((color) and (hover)) or (monochrome) {
      /* … */
    }
    
    /* or */
    @media (color) and (hover), (monochrome) {
      /* … */
    }
    

    See also