- CSS: Cascading Style Sheets - MDN Web Docs
Có thể bạn quan tâm
- References
- CSS
- <color>
- English (US)
- Remember language
- Deutsch
- Español
- Français
- 日本語
- 한국어
- Português (do Brasil)
- Русский
- 中文 (简体)
- Syntax
- Interpolation
- Accessibility
- Formal syntax
- Examples
- Specifications
- Browser compatibility
- See also
- CSS
- Beginner's tutorials
- Your first website: Styling the content
- CSS styling basics
- What is CSS?
- Getting started with CSS
- Challenge: Styling a biography page
- Basic CSS selectors
- Attribute selectors
- Pseudo-classes and pseudo-elements
- Combinators
- The box model
- Handling conflicts
- CSS values and units
- Sizing items in CSS
- Backgrounds and borders
- Overflowing content
- Images, media, and form elements
- Styling tables
- Debugging CSS
- Challenge: Fundamental CSS comprehension
- Challenge: Creating fancy letterheaded paper
- Challenge: A cool-looking box
- CSS text styling
- Fundamental text and font styling
- Styling lists
- Styling links
- Web fonts
- Challenge: Typesetting a community school homepage
- CSS layout
- Introduction to CSS layout
- Floats
- Positioning
- Flexbox
- CSS grid layout
- Responsive design
- Media query fundamentals
- Challenge: Fundamental layout comprehension
- CSS reference
- Modules
- CSS anchor positioning
- CSS animations
- CSS backgrounds and borders
- CSS basic user interface
- CSS box alignment
- CSS box model
- CSS box sizing
- CSS cascade and inheritance
- CSS color adjustment
- CSS colors
- CSS compositing and blending
- CSS conditional rules
- CSS containment
- CSS counter styles
- CSS custom properties for cascading variables
- CSS display
- CSS filter effects
- CSS flexible box layout
- CSS font loading
- CSS fonts
- CSS fragmentation
- CSS generated content
- CSS grid layout
- CSS images
- CSS inline layout
- CSS lists and counters
- CSS logical properties and values
- CSS masking
- CSS media queries
- CSS motion path
- CSS multi-column layout
- CSS namespaces
- CSS nesting
- CSS overflow
- CSS overscroll behavior
- CSS paged media
- CSS positioned layout
- CSS properties and values API
- CSS pseudo-elements
- CSS ruby layout
- CSS scoping
- CSS scroll snap
- CSS scroll-driven animations
- CSS scrollbars styling
- CSS selectors
- CSS shadow parts
- CSS shapes
- CSS syntax
- CSS table
- CSS text
- CSS text decoration
- CSS transforms
- CSS transitions
- CSS view transitions
- CSS writing modes
- CSSOM view
- Properties
- -moz-*
- -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-*
- -webkit-border-before Non-standard
- -webkit-box-reflect Non-standard
- -webkit-line-clamp
- -webkit-mask-box-image Non-standard
- -webkit-mask-composite Non-standard
- -webkit-mask-position-x Non-standard
- -webkit-mask-position-y Non-standard
- -webkit-mask-repeat-x Non-standard
- -webkit-mask-repeat-y Non-standard
- -webkit-tap-highlight-color Non-standard
- -webkit-text-fill-color
- -webkit-text-security Non-standard
- -webkit-text-stroke
- -webkit-text-stroke-color
- -webkit-text-stroke-width
- -webkit-touch-callout Non-standard
- accent-color
- align-*
- align-content
- align-items
- align-self
- all
- anchor-name Experimental
- animation-*
- animation
- animation-composition
- animation-delay
- animation-direction
- animation-duration
- animation-fill-mode
- animation-iteration-count
- animation-name
- animation-play-state
- animation-range Experimental
- animation-range-end Experimental
- animation-range-start Experimental
- animation-timeline Experimental
- animation-timing-function
- appearance
- aspect-ratio
- backdrop-filter
- backface-visibility
- background-*
- background
- background-attachment
- background-blend-mode
- background-clip
- background-color
- background-image
- background-origin
- background-position
- background-position-x
- background-position-y
- background-repeat
- background-size
- block-size
- border-*
- border
- border-block
- border-block-color
- border-block-end
- border-block-end-color
- border-block-end-style
- border-block-end-width
- border-block-start
- border-block-start-color
- border-block-start-style
- border-block-start-width
- border-block-style
- border-block-width
- border-bottom
- border-bottom-color
- border-bottom-left-radius
- border-bottom-right-radius
- border-bottom-style
- border-bottom-width
- border-collapse
- border-color
- border-end-end-radius
- border-end-start-radius
- border-image
- border-image-outset
- border-image-repeat
- border-image-slice
- border-image-source
- border-image-width
- border-inline
- border-inline-color
- border-inline-end
- border-inline-end-color
- border-inline-end-style
- border-inline-end-width
- border-inline-start
- border-inline-start-color
- border-inline-start-style
- border-inline-start-width
- border-inline-style
- border-inline-width
- border-left
- border-left-color
- border-left-style
- border-left-width
- border-radius
- border-right
- border-right-color
- border-right-style
- border-right-width
- border-spacing
- border-start-end-radius
- border-start-start-radius
- border-style
- border-top
- border-top-color
- border-top-left-radius
- border-top-right-radius
- border-top-style
- border-top-width
- border-width
- bottom
- box-*
- box-align Non-standard Deprecated
- box-decoration-break
- box-direction Non-standard Deprecated
- box-flex Non-standard Deprecated
- box-flex-group Non-standard Deprecated
- box-lines Non-standard Deprecated
- box-ordinal-group Non-standard Deprecated
- box-orient Non-standard Deprecated
- box-pack Non-standard Deprecated
- box-shadow
- box-sizing
- break-*
- break-after
- break-before
- break-inside
- caption-side
- caret-color
- clear
- clip-*
- clip Deprecated
- clip-path
- clip-rule
- color-*
- color
- color-interpolation
- color-interpolation-filters
- color-scheme
- column-*
- column-count
- column-fill
- column-gap
- column-rule
- column-rule-color
- column-rule-style
- column-rule-width
- column-span
- column-width
- columns
- contain-*
- contain
- contain-intrinsic-block-size
- contain-intrinsic-height
- contain-intrinsic-inline-size
- contain-intrinsic-size
- contain-intrinsic-width
- container-*
- container
- container-name
- container-type
- content
- content-visibility
- counter-*
- counter-increment
- counter-reset
- counter-set
- cursor
- cx
- cy
- d
- direction
- display
- dominant-baseline
- empty-cells
- field-sizing Experimental
- fill-*
- fill
- fill-opacity
- fill-rule
- filter
- flex-*
- flex
- flex-basis
- flex-direction
- flex-flow
- flex-grow
- flex-shrink
- flex-wrap
- float
- flood-color
- flood-opacity
- font-*
- font
- font-family
- font-feature-settings
- font-kerning
- font-language-override
- font-optical-sizing
- font-palette
- font-size
- font-size-adjust
- font-smooth Non-standard
- font-stretch
- font-style
- font-synthesis
- font-synthesis-position Experimental
- font-synthesis-small-caps
- font-synthesis-style
- font-synthesis-weight
- font-variant
- font-variant-alternates
- font-variant-caps
- font-variant-east-asian
- font-variant-emoji
- font-variant-ligatures
- font-variant-numeric
- font-variant-position
- font-variation-settings
- font-weight
- forced-color-adjust
- gap
- grid-*
- grid
- grid-area
- grid-auto-columns
- grid-auto-flow
- grid-auto-rows
- grid-column
- grid-column-end
- grid-column-start
- grid-row
- grid-row-end
- grid-row-start
- grid-template
- grid-template-areas
- grid-template-columns
- grid-template-rows
- hanging-punctuation
- height
- hyphenate-character
- hyphenate-limit-chars
- hyphens
- image-*
- image-orientation
- image-rendering
- image-resolution Experimental
- initial-letter
- inline-size
- inset-*
- inset
- inset-block
- inset-block-end
- inset-block-start
- inset-inline
- inset-inline-end
- inset-inline-start
- interpolate-size Experimental
- isolation
- justify-*
- justify-content
- justify-items
- justify-self
- left
- letter-spacing
- lighting-color
- line-*
- line-break
- line-height
- line-height-step Experimental
- list-*
- list-style
- list-style-image
- list-style-position
- list-style-type
- margin-*
- margin
- margin-block
- margin-block-end
- margin-block-start
- margin-bottom
- margin-inline
- margin-inline-end
- margin-inline-start
- margin-left
- margin-right
- margin-top
- margin-trim Experimental
- marker-*
- marker
- marker-end
- marker-mid
- marker-start
- mask-*
- mask
- mask-border
- mask-border-mode
- mask-border-outset
- mask-border-repeat
- mask-border-slice
- mask-border-source
- mask-border-width
- mask-clip
- mask-composite
- mask-image
- mask-mode
- mask-origin
- mask-position
- mask-repeat
- mask-size
- mask-type
- math-*
- math-depth
- math-shift Experimental
- math-style
- max-*
- max-block-size
- max-height
- max-inline-size
- max-width
- min-*
- min-block-size
- min-height
- min-inline-size
- min-width
- mix-blend-mode
- object-fit
- object-position
- offset-*
- offset
- offset-anchor
- offset-distance
- offset-path
- offset-position
- offset-rotate
- opacity
- order
- orphans
- outline-*
- outline
- outline-color
- outline-offset
- outline-style
- outline-width
- overflow-*
- overflow
- overflow-anchor
- overflow-block
- overflow-clip-margin
- overflow-inline
- overflow-wrap
- overflow-x
- overflow-y
- overlay Experimental
- overscroll-*
- overscroll-behavior
- overscroll-behavior-block
- overscroll-behavior-inline
- overscroll-behavior-x
- overscroll-behavior-y
- padding-*
- padding
- padding-block
- padding-block-end
- padding-block-start
- padding-bottom
- padding-inline
- padding-inline-end
- padding-inline-start
- padding-left
- padding-right
- padding-top
- page-*
- page
- page-break-after
- page-break-before
- page-break-inside
- paint-order
- perspective
- perspective-origin
- place-*
- place-content
- place-items
- place-self
- pointer-events
- position-*
- position
- position-anchor Experimental
- position-area Experimental
- position-try Experimental
- position-try-fallbacks Experimental
- position-try-order Experimental
- position-visibility Experimental
- print-color-adjust
- quotes
- r
- resize
- right
- rotate
- row-gap
- ruby-align
- ruby-position
- rx
- ry
- scale
- scroll-*
- scroll-behavior
- scroll-margin
- scroll-margin-block
- scroll-margin-block-end
- scroll-margin-block-start
- scroll-margin-bottom
- scroll-margin-inline
- scroll-margin-inline-end
- scroll-margin-inline-start
- scroll-margin-left
- scroll-margin-right
- scroll-margin-top
- scroll-padding
- scroll-padding-block
- scroll-padding-block-end
- scroll-padding-block-start
- scroll-padding-bottom
- scroll-padding-inline
- scroll-padding-inline-end
- scroll-padding-inline-start
- scroll-padding-left
- scroll-padding-right
- scroll-padding-top
- scroll-snap-align
- scroll-snap-stop
- scroll-snap-type
- scroll-timeline Experimental
- scroll-timeline-axis Experimental
- scroll-timeline-name Experimental
- scrollbar-*
- scrollbar-color
- scrollbar-gutter
- scrollbar-width
- shape-*
- shape-image-threshold
- shape-margin
- shape-outside
- shape-rendering
- stop-color
- stop-opacity
- stroke-*
- stroke
- stroke-dasharray
- stroke-dashoffset
- stroke-linecap
- stroke-linejoin
- stroke-miterlimit
- stroke-opacity
- stroke-width
- tab-size
- table-layout
- text-*
- text-align
- text-align-last
- text-anchor
- text-combine-upright
- text-decoration
- text-decoration-color
- text-decoration-line
- text-decoration-skip Experimental
- text-decoration-skip-ink
- text-decoration-style
- text-decoration-thickness
- text-emphasis
- text-emphasis-color
- text-emphasis-position
- text-emphasis-style
- text-indent
- text-justify
- text-orientation
- text-overflow
- text-rendering
- text-shadow
- text-size-adjust Experimental
- text-spacing-trim Experimental
- text-transform
- text-underline-offset
- text-underline-position
- text-wrap
- text-wrap-mode
- text-wrap-style
- timeline-scope Experimental
- top
- touch-action
- transform-*
- transform
- transform-box
- transform-origin
- transform-style
- transition-*
- transition
- transition-behavior
- transition-delay
- transition-duration
- transition-property
- transition-timing-function
- translate
- unicode-bidi
- user-modify Non-standard Deprecated
- user-select
- vector-effect
- vertical-align
- view-*
- view-timeline Experimental
- view-timeline-axis Experimental
- view-timeline-inset Experimental
- view-timeline-name Experimental
- view-transition-name
- visibility
- white-space
- white-space-collapse
- widows
- width
- will-change
- word-break
- word-spacing
- writing-mode
- x
- y
- z-index
- zoom
- -moz-*
- Selectors
- & nesting selector
- Attribute selectors
- Class selectors
- ID selectors
- Type selectors
- Universal selectors
- Combinators
- Child combinator
- Column combinator Experimental
- Descendant combinator
- Namespace separator
- Next-sibling combinator
- Selector list
- Subsequent-sibling combinator
- Pseudo-classes
- :-moz-broken Non-standard Deprecated
- :-moz-drag-over Non-standard
- :-moz-first-node Experimental Non-standard
- :-moz-handler-blocked Non-standard
- :-moz-handler-crashed Non-standard
- :-moz-handler-disabled Non-standard
- :-moz-last-node Experimental Non-standard
- :-moz-loading Non-standard
- :-moz-locale-dir(ltr) Non-standard
- :-moz-locale-dir(rtl) Non-standard
- :-moz-only-whitespace Non-standard
- :-moz-submit-invalid Non-standard
- :-moz-suppressed Non-standard
- :-moz-user-disabled Non-standard
- :-moz-window-inactive Non-standard
- :active
- :any-link
- :autofill
- :blank Experimental
- :buffering
- :checked
- :current Experimental
- :default
- :defined
- :dir()
- :disabled
- :empty
- :enabled
- :first
- :first-child
- :first-of-type
- :focus
- :focus-visible
- :focus-within
- :fullscreen
- :future
- :has-slotted Experimental
- :has()
- :host
- :host-context()
- :host()
- :hover
- :in-range
- :indeterminate
- :invalid
- :is()
- :lang()
- :last-child
- :last-of-type
- :left
- :link
- :local-link Experimental
- :modal
- :muted
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :only-child
- :only-of-type
- :optional
- :out-of-range
- :past
- :paused
- :picture-in-picture
- :placeholder-shown
- :playing
- :popover-open
- :read-only
- :read-write
- :required
- :right
- :root
- :scope
- :seeking
- :stalled
- :state()
- :target
- :target-within Experimental
- :user-invalid
- :user-valid
- :valid
- :visited
- :volume-locked
- :where()
- Pseudo-elements
- ::-moz-color-swatch Non-standard
- ::-moz-focus-inner Experimental Non-standard
- ::-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-inner-spin-button Non-standard
- ::-webkit-meter-bar Non-standard Deprecated
- ::-webkit-meter-even-less-good-value Non-standard
- ::-webkit-meter-inner-element Non-standard
- ::-webkit-meter-optimum-value Non-standard
- ::-webkit-meter-suboptimum-value Non-standard
- ::-webkit-progress-bar Non-standard
- ::-webkit-progress-inner-element Non-standard
- ::-webkit-progress-value Non-standard
- ::-webkit-scrollbar Non-standard
- ::-webkit-search-cancel-button Non-standard
- ::-webkit-search-results-button Non-standard
- ::-webkit-slider-runnable-track Experimental Non-standard
- ::-webkit-slider-thumb Experimental Non-standard
- ::after
- ::backdrop
- ::before
- ::cue
- ::file-selector-button
- ::first-letter
- ::first-line
- ::grammar-error
- ::highlight()
- ::marker
- ::part()
- ::placeholder
- ::selection
- ::slotted()
- ::spelling-error
- ::target-text
- ::view-transition
- ::view-transition-group
- ::view-transition-image-pair
- ::view-transition-new
- ::view-transition-old
- At-rules
- @charset
- @color-profile
- @container
- @counter-style
- @document Non-standard Deprecated
- @font-face
- @font-feature-values
- @font-palette-values
- @import
- @keyframes
- @layer
- @media
- @namespace
- @page
- @position-try Experimental
- @property
- @scope
- @starting-style
- @supports
- @view-transition
- Functions
- -moz-image-rect Non-standard Deprecated
- abs()
- acos()
- anchor-size() Experimental
- anchor() Experimental
- asin()
- atan()
- atan2()
- attr()
- calc-size() Experimental
- calc()
- clamp()
- cos()
- counter()
- counters()
- cross-fade()
- element() Experimental
- env()
- exp()
- fit-content()
- hypot()
- log()
- max()
- min()
- minmax()
- mod()
- pow()
- ray()
- rem()
- repeat()
- round()
- sign()
- sin()
- sqrt()
- symbols()
- tan()
- url()
- var()
- Types
- <absolute-size>
- <alpha-value>
- <angle-percentage>
- <angle>
- <basic-shape>
- <blend-mode>
- <box-edge>
- <calc-keyword>
- <calc-sum>
- <color-interpolation-method>
- <color>
- <custom-ident>
- <dashed-ident>
- <dimension>
- <display-box>
- <display-inside>
- <display-internal>
- <display-legacy>
- <display-listitem>
- <display-outside>
- <easing-function>
- <filter-function>
- <flex>
- <frequency-percentage>
- <frequency>
- <generic-family>
- <gradient>
- <hex-color>
- <hue-interpolation-method>
- <hue>
- <ident>
- <image>
- <integer>
- <length-percentage>
- <length>
- <line-style>
- <named-color>
- <number>
- <overflow>
- <percentage>
- <position-area> Experimental
- <position>
- <ratio>
- <relative-size>
- <resolution>
- <shape> Deprecated
- <string>
- <system-color>
- <time-percentage>
- <time>
- <transform-function>
- <url>
- Guides
- Animations
- Using CSS animations
- Backgrounds and Borders
- Using multiple backgrounds
- Resizing background images
- Box alignment
- Box alignment in block layout
- Box alignment in flexbox
- Box alignment in grid layout
- Box alignment in multi-column layout
- Box model
- Introduction to the CSS basic box model
- Mastering margin collapsing
- Colors
- Applying_color_to_HTML_elements
- Color values
- Using relative colors
- Using color wisely
- Accessibility_Understanding_colors_and_luminance"
- Accessibility: Color contrast
- Columns
- Basic concepts of multi-column layout
- Styling columns
- Spanning and balancing columns
- Handling overflow in multi-column layout
- Handling content breaks in multi-column layout
- Conditional rules
- Using feature queries
- Containment
- Using CSS containment
- CSS container queries
- Using container size and style queries
- CSSOM view
- Coordinate systems
- Flexbox
- Basic concepts of flexbox
- Relationship of flexbox to other layout methods
- Aligning items in a flex container
- Ordering flex items
- Controlling ratios of flex items along the main axis
- Mastering wrapping of flex items
- Typical use cases of flexbox
- Flow layout
- Block and inline layout in normal flow
- In flow and out of flow
- Introduction to formatting contexts
- Flow layout and writing modes
- Flow layout and overflow
- Fonts
- OpenType font features guide
- Variable fonts guide
- Grid
- Basic concepts of grid layout
- Relationship of grid layout to other layout methods
- Grid layout using line-based placement
- Grid template areas
- Layout using named grid lines
- Auto-placement in grid layout
- Box alignment in grid layout
- Grids, logical values, and writing modes
- Grid layout and accessibility
- CSS grid layout and progressive enhancement
- Realizing common layouts using grids
- Subgrid
- Masonry layout Experimental
- Images
- Using CSS gradients
- Lists and counters
- Using CSS counters
- Consistent list indentation
- Logical properties
- Basic concepts of logical properties and values
- Logical properties for floating and positioning
- Logical properties for margins, borders, and padding
- Logical properties for sizing
- Math functions
- Using CSS math functions
- Media queries
- Using media queries
- Using media queries for accessibility
- Testing media queries programmatically
- Printing
- Nesting style rules
- Using CSS nesting
- CSS nesting at-rules
- CSS nesting and specificity
- Positioning
- Understanding z-index
- Scroll snap
- Basic concepts of scroll snap
- Shapes
- Overview of shapes
- Shapes from box values
- Basic shapes with shape-outside
- Shapes from images
- Text
- Wrapping and breaking text
- Transforms
- Using CSS transforms
- Transitions
- Using CSS transitions
- CSS Layout cookbook
- Recipe: Media objects
- Column layouts
- Center an element
- Sticky footers
- Split navigation
- Breadcrumb navigation
- List group with badges
- Pagination
- Card
- Grid wrapper
- Tools
- Color picker tool
- Box-shadow generator
- Border-image generator
- Border-radius generator
- Syntax
- Interpolation
- Accessibility
- Formal syntax
- Examples
- Specifications
- Browser compatibility
- See also
The <color> CSS data type represents a color. A <color> may also include an alpha-channel transparency value, indicating how the color should composite with its background.
Note: Although <color> values are precisely defined, their actual appearance may vary (sometimes significantly) from device to device. This is because most devices are not calibrated, and some browsers do not support output devices' color profiles.
Syntax
css/* Named colors */ rebeccapurple aliceblue /* RGB Hexadecimal */ #f09 #ff0099 /* RGB (Red, Green, Blue) */ rgb(255 0 153) rgb(255 0 153 / 80%) /* HSL (Hue, Saturation, Lightness) */ hsl(150 30% 60%) hsl(150 30% 60% / 80%) /* HWB (Hue, Whiteness, Blackness) */ hwb(12 50% 0%) hwb(194 0% 0% / 0.5) /* LAB (Lightness, A-axis, B-axis) */ lab(50% 40 59.5) lab(50% 40 59.5 / 0.5) /* LCH (Lightness, Chroma, Hue) */ lch(52.2% 72.2 50) lch(52.2% 72.2 50 / 0.5) /* Oklab (Lightness, A-axis, B-axis) */ oklab(59% 0.1 0.1) oklab(59% 0.1 0.1 / 0.5) /* Oklch (Lightness, Chroma, Hue) */ oklch(60% 0.15 50) oklch(60% 0.15 50 / 0.5) /* Relative CSS colors */ /* HSL hue change */ hsl(from red 240deg s l) /* HWB alpha channel change */ hwb(from green h w b / 0.5) /* LCH lightness change */ lch(from blue calc(l + 20) c h) /* light-dark */ light-dark(white, black) light-dark(rgb(255 255 255), rgb(0 0 0))A <color> value can be specified using one of the methods listed below:
- By keywords: <named-color> (such as blue or pink), <system-color>, and currentcolor.
- By hexadecimal notations: <hex-color> (such as #ff0000).
- By <color-function>, with parameters in a color space using functional notations:
- sRGB color space: hsl(), hwb(), and rgb().
- CIELAB color space: lab() and lch().
- Oklab color space: oklab() and oklch().
- Other color spaces: color().
- By using relative color syntax to output a new color based on an existing color. Any of the above color functions can take an origin color preceded by the from keyword and followed by definitions of the channel values for the new output color.
- By mixing two colors: color-mix().
- By specifying two colors, using the first for light color-schemes and the second for dark color-schemes: light-dark().
currentcolor keyword
The currentcolor keyword represents the value of an element's color property. This lets you use the color value on properties that do not receive it by default.
If currentcolor is used as the value of the color property, it instead takes its value from the inherited value of the color property.
html<div style="color: blue; border: 1px dashed currentcolor;"> The color of this text is blue. <div style="background: currentcolor; height:9px;"></div> This block is surrounded by a blue border. </div>Missing color components
Each component of any CSS color functions - except for those using the legacy comma-separated syntax - can be specified as the keyword none to be a missing component.
Explicitly specifying missing components in color interpolation is useful for cases where you would like to interpolate some color components but not others. For all other purposes, a missing component will effectively have a zero value in an appropriate unit: 0, 0%, or 0deg. For example, the following colors are equivalent when used outside of interpolation:
css/* These are equivalent */ color: oklab(50% none -0.25); color: oklab(50% 0 -0.25); /* These are equivalent */ background-color: hsl(none 100% 50%); background-color: hsl(0deg 100% 50%);Interpolation
Color interpolation happens with gradients, transitions, and animations.
When interpolating <color> values, they are first converted to a given color space, and then each component of the computed values are interpolated linearly, with interpolation's speed being determined by the easing function in transitions and animations. The interpolation color space defaults to Oklab, but can be overridden through <color-interpolation-method> in some color-related functional notations.
Interpolation with missing components
Interpolating colors in the same space
When interpolating colors that are exactly in the interpolation color space, missing components from one color are replaced with existing values of the same components from the other color. For example, the following two expressions are equivalent:
csscolor-mix(in oklch, oklch(none 0.2 10), oklch(60% none 30)) color-mix(in oklch, oklch(60% 0.2 10), oklch(60% 0.2 30))Note: If a component is missing from both colors, this component will be missing after the interpolation.
Interpolating colors from different spaces: analogous components
If any color to be interpolated is not in the interpolation color space, its missing components are transferred into the converted color based on analogous components of the same category as described in the following table:
Category | Analogous components |
---|---|
Reds | R, X |
Greens | G, Y |
Blues | B, Z |
Lightness | L |
Colorfulness | C, S |
Hue | H |
a | a |
b | b |
For example:
- X (0.2) in color(xyz 0.2 0.1 0.6) is analogous to R (50%) in rgb(50% 70% 30%).
- H (0deg) in hsl(0deg 100% 80%) is analogous to H (140) in oklch(80% 0.1 140).
Using Oklch as the interpolation color space and the two colors below as an example:
csslch(80% 30 none) color(display-p3 0.7 0.5 none)The preprocessing procedure is:
-
Replace the missing components in both colors with a zero value:
csslch(80% 30 0) color(display-p3 0.7 0.5 0) -
Convert both colors into the interpolation color space:
cssoklch(83.915% 0.0902 0.28) oklch(63.612% 0.1522 78.748) -
If any component of the converted colors is analogous to a missing component in the corresponding original color, reset it as a missing component:
cssoklch(83.915% 0.0902 none) oklch(63.612% 0.1522 78.748) -
Replace any missing component with the same component from the other converted color:
cssoklch(83.915% 0.0902 78.748) oklch(63.612% 0.1522 78.748)
Accessibility
Some people have difficulty distinguishing colors. The WCAG 2.2 recommendation strongly advises against using color as the only means of conveying a specific message, action, or result. See color and color contrast for more information.
Formal syntax
<color> = <color-base> | currentColor | <system-color> <color-base> = <hex-color> | <color-function> | <named-color> | transparent <color-function> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hwb()> | <lab()> | <lch()> | <oklab()> | <oklch()> | <color()> <rgb()> = <legacy-rgb-syntax> | <modern-rgb-syntax> <rgba()> = <legacy-rgba-syntax> | <modern-rgba-syntax> <hsl()> = <legacy-hsl-syntax> | <modern-hsl-syntax> <hsla()> = <legacy-hsla-syntax> | <modern-hsla-syntax> <hwb()> = hwb( [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? ) <lab()> = lab( [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? ) <lch()> = lch( [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <hue> | none ] [ / [ <alpha-value> | none ] ]? ) <oklab()> = oklab( [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? ) <oklch()> = oklch( [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <hue> | none ] [ / [ <alpha-value> | none ] ]? ) <color()> = color( [ from <color> ]? <colorspace-params> [ / [ <alpha-value> | none ] ]? ) <legacy-rgb-syntax> = rgb( <percentage>#{3} , <alpha-value>? ) | rgb( <number>#{3} , <alpha-value>? ) <modern-rgb-syntax> = rgb( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? ) <legacy-rgba-syntax> = rgba( <percentage>#{3} , <alpha-value>? ) | rgba( <number>#{3} , <alpha-value>? ) <modern-rgba-syntax> = rgba( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? ) <legacy-hsl-syntax> = hsl( <hue> , <percentage> , <percentage> , <alpha-value>? ) <modern-hsl-syntax> = hsl( [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? ) <legacy-hsla-syntax> = hsla( <hue> , <percentage> , <percentage> , <alpha-value>? ) <modern-hsla-syntax> = hsla( [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? ) <hue> = <number> | <angle> <alpha-value> = <number> | <percentage> <colorspace-params> = <custom-params> | <predefined-rgb-params> | <predefined-polar-params> | <predefined-rectangular-params> | <xyz-params> <custom-params> = <dashed-ident> [ <number> | <percentage> | none ]+ <predefined-rgb-params> = <predefined-rgb> [ <number> | <percentage> | none ]{3} <predefined-polar-params> = jzczhz [ <number> | <percentage> | none ]{2} [ <hue> | none ] <predefined-rectangular-params> = <predefined-rectangular> [ <number> | <percentage> | none ]{3} <xyz-params> = <xyz> [ <number> | <percentage> | none ]{3} <predefined-rgb> = srgb | srgb-linear | display-p3 | a98-rgb | prophoto-rgb | rec2020 | rec2100-pq | rec2100-hlg | rec2100-linear <predefined-rectangular> = jzazbz | ictcp <xyz> = xyz | xyz-d50 | xyz-d65Examples
Exploring color values
In this example, we provide a <div> and a text input. Entering a valid color into the input causes the <div> to adopt that color, allowing you to test our color values.
HTML
html<div></div> <hr /> <label for="color">Enter a valid color value:</label> <input type="text" id="color" /> div { height: 200px; width: 200px; } const inputElem = document.querySelector("input"); const divElem = document.querySelector("div"); function validTextColor(stringToTest) { if (stringToTest === "inherit" || stringToTest === "transparent") { return false; } const div = document.createElement("div"); div.style.color = stringToTest; return !!div.style.color; } inputElem.addEventListener("input", () => { if (validTextColor(inputElem.value)) { divElem.style.backgroundColor = inputElem.value; divElem.textContent = ""; } else { divElem.removeAttribute("style"); divElem.textContent = "Invalid color value"; } });Result
Generating fully saturated sRGB colors
This example shows fully saturated sRGB colors in the sRGB color space.
HTML
html<div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div>CSS
body { display: flex; flex-wrap: wrap; } div { height: 80px; margin: 10px; width: 80px; } cssdiv:nth-child(1) { background-color: hsl(0 100% 50%); } div:nth-child(2) { background-color: hsl(30 100% 50%); } div:nth-child(3) { background-color: hsl(60 100% 50%); } div:nth-child(4) { background-color: hsl(90 100% 50%); } div:nth-child(5) { background-color: hsl(120 100% 50%); } div:nth-child(6) { background-color: hsl(150 100% 50%); } div:nth-child(7) { background-color: hsl(180 100% 50%); } div:nth-child(8) { background-color: hsl(210 100% 50%); } div:nth-child(9) { background-color: hsl(240 100% 50%); } div:nth-child(10) { background-color: hsl(270 100% 50%); } div:nth-child(11) { background-color: hsl(300 100% 50%); } div:nth-child(12) { background-color: hsl(330 100% 50%); }Result
Creating different shades of red
This example shows reds of different shades in the sRGB color space.
HTML
html<div></div> <div></div> <div></div> <div></div> <div></div> <div></div>CSS
body { display: flex; flex-wrap: wrap; } div { box-sizing: border-box; height: 80px; margin: 10px; width: 80px; } cssdiv:nth-child(1) { background-color: hsl(0 100% 0%); } div:nth-child(2) { background-color: hsl(0 100% 20%); } div:nth-child(3) { background-color: hsl(0 100% 40%); } div:nth-child(4) { background-color: hsl(0 100% 60%); } div:nth-child(5) { background-color: hsl(0 100% 80%); } div:nth-child(6) { background-color: hsl(0 100% 100%); border: solid; }Result
Creating reds of different saturation
This example shows reds of different saturations in the sRGB color space.
HTML
html<div></div> <div></div> <div></div> <div></div> <div></div> <div></div>CSS
body { display: flex; flex-wrap: wrap; } div { height: 80px; margin: 10px; width: 80px; } cssdiv:nth-child(1) { background-color: hsl(0 0% 50%); } div:nth-child(2) { background-color: hsl(0 20% 50%); } div:nth-child(3) { background-color: hsl(0 40% 50%); } div:nth-child(4) { background-color: hsl(0 60% 50%); } div:nth-child(5) { background-color: hsl(0 80% 50%); } div:nth-child(6) { background-color: hsl(0 100% 50%); }Result
Specifications
Specification |
---|
CSS Color Module Level 4 # color-syntax |
Browser compatibility
BCD tables only load in the browser
See also
- opacity: the property defining transparency at the element level
- <hue>: the data type representing the hue angle of a color
- color, background-color, border-color, box-shadow, outline-color, text-shadow: common properties that use <color>
- Applying color to HTML elements using CSS
- Using relative colors
- New functions, gradients, and hues in CSS colors (Level 4) on MDN blog (2023)
Help improve MDN
Was this page helpful to you?YesNoLearn how to contribute.This page was last modified on Nov 26, 2024 by MDN contributors.
View this page on GitHub • Report a problem with this contentTừ khóa » Html Color Codes Rgba
-
Decimal Red, Green, Blue, And Opacity Color Codes
-
CSS RGB And RGBA Colors - W3Schools
-
HTML RGB And RGBA Colors - W3Schools
-
HTML Color Codes - What's Your Color
-
RGBA Color - Html Colors
-
HTML Color Codes | HEX, RGB, RGBA, HSL And HSLA Values
-
RGB Color Codes Chart
-
How To Use CSS Hex Code Colors With Alpha Values - DigitalOcean
-
HTML Color Codes
-
HTML Color Codes - Dofactory
-
Defining Colors In CSS
-
Color Enumeration And ColorFade, ColorValue, And RGBA Functions ...
-
Convert RGBA Color To HTML Color Code - Copy Programming
-
Images: Color Codes - LibGuides At Texas Wesleyan University