- How to Set Background Color with HTML and CSS
- Add the style attribute to the element
- Example of setting a background color with the style attribute:
- Result
- Add the CSS background-color property to the element
- Example of setting a background color with the CSS background-color property:
- Example of adding a background color to specific elements:
- Example of setting background colors with different color types:
- Create a background with gradients
- Example of setting a linear-gradient background:
- Example of setting a radial-gradient background:
- Create a changing background
- Example of creating a changing background:
- CSS background-color Property
- Browser Support
- CSS Syntax
- Property Values
- More Examples
- Example
- Example
- Example
- Example
- Example
- Example
- background-color
- Синтаксис
- Значения
- Объектная модель
- Браузеры
- CSS Backgrounds
- CSS background-color
- Example
- Other Elements
- Example
- Opacity / Transparency
- Example
- Transparency using RGBA
- Example
- The CSS Background Color Property
How to Set Background Color with HTML and CSS
First thing you should know is that there are different types of HTML colors, such as Hex color codes, HTML color names, RGB and RGBa values, HSL colors, etc. To choose your preferred color use our Color Tools.
In this snippet, you can find many different ways of adding a background color. Let’s start from the easiest one.
Add the style attribute to the element
You can set a background color for an HTML document by adding style=»background-color:» to the element.
Example of setting a background color with the style attribute:
html> html> head> title>Title of the document title> head> body style="background-color:#1c87c9;"> h1>Some heading h1> p>Some paragraph for example. p> body> html>
Result
Some paragraph for example.
Add the CSS background-color property to the element
The background-color property is used to change the background color. Inserting it to the element you will have a full colored cover of the page.
Example of setting a background color with the CSS background-color property:
html> html> head> title>Title of the document title> style> body < background-color: #1c87c9; > style> head> body> body> html>
You can also add a background color to specific elements in your document. For example, let’s see how to change the color of the heading and paragraphs.
Example of adding a background color to specific elements:
html> html> head> title>Title of the document title> style> body < background-color: #e6ebef; > h2 < background-color: #8ebf42; > p < background-color: #1c87c9; > style> head> body> h2>Some heading with green background. h2> p>Some paragraph with blue background. p> body> html>
Let’s see another example, where we add a background color with a color name value to the element. We specify a RGB value for the , HSL for the , and RGBa value for the element.
Example of setting background colors with different color types:
html> html> head> title>Title of the document title> style> h1 < background-color: lightblue; > h2 < background-color: rgb(142, 191, 66); > p < background-color: hsl(300, 100%, 25%); > span < background-color: rgba(255, 127, 80, 0.58); > style> head> body> h1>Example h1> h2>Some heading with green background. h2> p>Some paragraph with blue background. p> span>Some paragraph for span> body> html>
Create a background with gradients
Gradient backgrounds let you create smooth transitions between two or more specified colors.
There are two types of gradient backgrounds: linear-gradient and radial-gradient.
In linear-gradient backgrounds, you can set a starting point for the colors. If you don’t mention a starting point, it will automatically set «top to bottom» by default.
Example of setting a linear-gradient background:
html> html> head> title>Title of the document title> style> #grad < height: 500px; background-color: blue;/* For browsers that do not support gradients */ background-image: linear-gradient(to right, #1c87c9, #8ebf42); > style> head> body> h1>Right to Left Linear Gradient background h1> div id="grad"> div> body> html>
The given example shows a linear gradient that starts from the left. It starts from blue, transitioning to green. Change it according to your requirements.
In radial gradient backgrounds, the starting point is defined by its center.
Example of setting a radial-gradient background:
html> html> head> title>Title of the document title> style> #grad < height: 500px; background-color: grey;/* For browsers that do not support gradients */ background-image: radial-gradient(#e6ebef, #1c87c9, #8ebf42); > style> head> body> h1>Radial Gradient Background h1> div id="grad"> div> body> html>
Create a changing background
You can create a background which will change its colors in the mentioned time. For that, add the animation property to the element. Use the @keyframes rule to set the background colors through which you’ll flow, as well as the length of time each color will appear on the page.
Example of creating a changing background:
html> html> head> title>Title of the document title> style> body < -webkit-animation: colorchange 20s infinite; animation: colorchange 20s infinite; > @-webkit-keyframes colorchange < 0% < background: #8ebf42; > 25% < background: #e6ebef; > 50% < background: #1c87c9; > 75% < background: #095484; > 100% < background: #d0e2bc; > > @keyframes colorchange < 0% < background: #8ebf42; > 25% < background: #e6ebef; > 50% < background: #1c87c9; > 75% < background: #095484; > 100% < background: #d0e2bc; > > style> head> body> h1>Changing Background h1> body> html>
The percentages specify the animation length mentioned in «colorchange» (e.g. 20s).
CSS background-color Property
The background-color property sets the background color of an element.
The background of an element is the total size of the element, including padding and border (but not the margin).
Tip: Use a background color and a text color that makes the text easy to read.
Default value: | transparent |
---|---|
Inherited: | no |
Animatable: | yes. Read about animatable Try it |
Version: | CSS1 |
JavaScript syntax: | object.style.backgroundColor=»#00FF00″ Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
CSS Syntax
Property Values
Value | Description | Demo |
---|---|---|
color | Specifies the background color. Look at CSS Color Values for a complete list of possible color values. | Demo ❯ |
transparent | Specifies that the background color should be transparent. This is default | Demo ❯ |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
More Examples
Example
Specify the background color with a HEX value:
Example
Specify the background color with an RGB value:
Example
Specify the background color with an RGBA value:
Example
Specify the background color with a HSL value:
Example
Specify the background color with a HSLA value:
Example
Set background colors for different elements:
body <
background-color: #fefbd8;
>
h1 background-color: #80ced6;
>
div background-color: #d5f4e6;
>
span background-color: #f18973;
>
background-color
Определяет цвет фона элемента. Хотя это свойство не наследует свойства своего родителя, из-за того, что начальное значение устанавливается прозрачным, цвет фона дочерних элементов совпадает с цветом фона родительского элемента.
Синтаксис
background-color: | transparent | inherit
Значения
transparent Устанавливает прозрачный фон. inherit Наследует значение родителя.
HTML5 CSS2.1 IE Cr Op Sa Fx
Lorem ipsum dolor sit amet
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.
В данном примере для элементов веб-страницы применяется три различных способа задания фонового цвета. Результат примера показан на рис. 1.
Рис. 1. Применение background-color
Объектная модель
[window.]document.getElementById(» elementID «).style.backgroundColorБраузеры
Internet Explorer до версии 7.0 включительно не поддерживает значение inherit .
CSS Backgrounds
The CSS background properties are used to add background effects for elements.
In these chapters, you will learn about the following CSS background properties:
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
- background (shorthand property)
CSS background-color
The background-color property specifies the background color of an element.
Example
The background color of a page is set like this:
With CSS, a color is most often specified by:
- a valid color name — like «red»
- a HEX value — like «#ff0000»
- an RGB value — like «rgb(255,0,0)»
Look at CSS Color Values for a complete list of possible color values.
Other Elements
You can set the background color for any HTML elements:
Example
Here, the ,
, and elements will have different background colors:
div background-color: lightblue;
>
Opacity / Transparency
The opacity property specifies the opacity/transparency of an element. It can take a value from 0.0 — 1.0. The lower value, the more transparent:
Example
Note: When using the opacity property to add transparency to the background of an element, all of its child elements inherit the same transparency. This can make the text inside a fully transparent element hard to read.
Transparency using RGBA
If you do not want to apply opacity to child elements, like in our example above, use RGBA color values. The following example sets the opacity for the background color and not the text:
You learned from our CSS Colors Chapter, that you can use RGB as a color value. In addition to RGB, you can use an RGB color value with an alpha channel (RGBA) — which specifies the opacity for a color.
An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).
Tip: You will learn more about RGBA Colors in our CSS Colors Chapter.