网页 综合 更新时间 2026-10-07 05:41:23

Applying color to HTML elements using CSS - CSS | MDN

自动蜘蛛 developer.mozilla.org 正文约 5999 字
HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
Learn web development
Get to know MDN better
With CSS, there are lots of ways to add color to your HTML elements to create the look you want. This guide is a primer introducing how CSS can be used to apply colors to HTML elements. This guide includes lists of the CSS properties that set color in their values and how to use colors both in stylesheets and in other ways.
Note: It is important to use colors wisely. Always select appropriate colors, ensuring the contrast between text and the background is sufficient to ensure legibility, and always keep the needs of people with differing visual capabilities in mind.
To learn more about CSS colors as a data type, see the CSS <color> data type reference and the CSS color values guide.
At the element level, everything in HTML can have color applied to it. Let's look at the different items rendered on the page — such as text, borders, etc. We'll provide lists of the CSS properties that apply color to each.
At a fundamental level, the color property defines the foreground color of an HTML element's content and the background-color property defines the element's background color. These can be used on just about any element.
Whenever an element is rendered, these properties are used to determine the color of the text, its background, and any decorations on the text.
The color to use when drawing the text and any text decorations (such as the addition of under- or overlines, strike-through lines, and so forth).
The text's background color.
Configures a shadow effect to apply to text. Among the options for the shadow is the shadow's base color (which is then blurred and blended with the background based on the other parameters). See Text drop shadows to learn more.
The default text decorations (such as underlines, strikethroughs, etc.) color is currentColor. This keyword represents the current value of the color property. However, you can override that value and use a different color for them with the text-decoration-color property.
The color to use when rendering emphasis symbols adjacent to each character in the text. This is used primarily when drawing text for East Asian languages.
The color to use when drawing the caret (sometimes referred to as the text input cursor) within the element. This is only useful in elements that are editable, such as <input> and <textarea> or elements whose HTML contenteditable attribute is set to true.
Every element is a box with some sort of content, and has a background and a border in addition to whatever contents the box may have.
See the Borders section for a list of the CSS properties you can use to set the colors of a box's borders.
The background color to use in areas of the element that have no foreground content.
Configures inset shadow and drop shadow effects on the box. Among the options for each shadow is the shadow's base color (which is then blurred and blended with any background based on the other parameters).
The color to use when drawing the line separating columns of text when using CSS multi-column layout.
The color to use when drawing an outline around the outside of the element. This outline is different from the border in that it doesn't get space set aside for it in the document. Outlines do not participate in the box model, overlapping other content. Outlines are generally used as focus indicators, indicating which element currently has focus and will receive keyboard input events.
Any element can have a border drawn around it. A basic element border is a line drawn around the edges of the element's content. See The box model to learn about the relationship between elements and their borders, and the article Styling borders using CSS to learn more about applying styles to borders.
You can use the border shorthand property, which lets you configure everything about the border in one shot (including non-color features of borders, such as its width, style (solid, dashed, etc.), and so forth.
Specifies a single color to use for every side of the element's border.
Lets you set the color of the corresponding side of the element's border.
With these, you can set the color used to draw the borders which are closest to the start and end of the block the border surrounds. In a left-to-right writing mode (such as the way English is written), the block start border is the top edge and the block end is the bottom. This differs from the inline start and end, which are the left and right edges (corresponding to where each line of text in the box begins and ends).
These let you color the edges of the border closest to the beginning and the end of the start of lines of text within the box. Which side this is will vary depending on the writing-mode, direction, and text-orientation properties, which are typically (but not always) used to adjust text directionality based on the language being displayed. For example, if the box's text is being rendered right-to-left, then the border-inline-start-color is applied to the right side of the border.
Now that you know which CSS properties let you apply color to elements, you can start adding colors to your websites. Let's look at some examples of using color within a style sheet. In this example, we use several previously mentioned properties, with the concept of applying colors in CSS being the same no matter the property.
Let's look at the result first, before going on to look at the code we need to create it:
The HTML responsible for creating the above example is shown here:
Here we have a wrapper <div> containing two child <div>s, each with a single child paragraph (<p>). Each content <div> is given a different look and feel.
Let's look at the CSS that creates the above result a piece at a time.
Note: We are using multiple different CSS color value types in this example to demonstrate their use. This is not recommended for production code. When writing CSS,
访问原文 返回搜索 https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Colors/Applying_color
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。