Set background color in html body

background-color

CSS-свойство background-color CSS устанавливает цвет фона элемента.

Интерактивный пример

Синтаксис

/* Словесные значения */ background-color: red; /* Шестнадцатеричное значение */ background-color: #bbff00; /* Шестнадцатеричное значение с alpha-каналом */ background-color: #11ffee00; /* 00 - полностью прозрачный */ background-color: #11ffeeff; /* ff - непрозрачный */ /* RGB-значение */ background-color: rgb(255, 255, 128); /* RGBA-значение или RGB с alpha-каналом */ background-color: rgba(117, 190, 218, 0.0); /* 0.0 - полностью прозрачный */ background-color: rgba(117, 190, 218, 0.5); /* 0.5 - полупрозрачный */ background-color: rgba(117, 190, 218, 1.0); /* 1.0 - непрозрачный */ /* HSLA-значение */ background-color: hsla(50, 33%, 25%, 0.75); /* Специальные словесные значения */ background-color: currentColor; background-color: transparent; /* Общие значения */ background-color: inherit; background-color: initial; background-color: unset; 

Свойство background-color определяется единственным значением .

Читайте также:  Javascript document write hello

Значения

Формальный синтаксис

Примеры

HTML

div class="exampleone"> Lorem ipsum dolor sit amet, consectetuer div> div class="exampletwo"> Lorem ipsum dolor sit amet, consectetuer div> div class="examplethree"> Lorem ipsum dolor sit amet, consectetuer div> 

CSS

.exampleone  background-color: teal; color: white; > .exampletwo  background-color: rgb(153, 102, 153); color: rgb(255, 255, 204); > .examplethree  background-color: #777799; color: #FFFFFF; > 

Результат

Проблемы доступности

Важно обеспечить достаточный цветовой контраст между цветом текста и фоном, чтобы люди со слабым зрением могли его прочитать, но при этом должна быть достаточная разница между введённым текстом и текстом placeholder, чтобы пользователь не путал их.

Коэффициент цветового контраста определяется путём сравнения яркости текста placeholder и цветом фона формы ввода. Чтобы соответствовать рекомендациям Web Content Accessibility Guidelines (WCAG), требуется соотношение 4.5:1 для основного текста и 3:1 для более крупного текста, например, заголовков. Крупный текст определяется как 18.66px и больше с жирным начертанием или 24px и больше с обычным начертанием.

Спецификации

Совместимость с браузерами

BCD tables only load in the browser

Смотрите также

Found a content problem with this page?

This page was last modified on 10 окт. 2022 г. by MDN contributors.

Your blueprint for a better internet.

Источник

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).

Источник

Which is the HTML Tag for Background Color?

Javascript Course - Mastering the Fundamentals

The color combination of various elements on a webpage is really important. Companies keep it styled in a way to attract customers to their websites and keeps them hooked to it. And this is where the background color of elements came into the picture. Html provides us with a number of ways to modify the background color of any element of a webpage.

Introduction

The attribute that is used to set background color of an HTML element is bg color. Depending on the element whose background color is to be set, we use the appropriate tag. The bgcolor attribute can be used with the following tags- body, table, td, th, tr, marquee .

How to Set Background Color in HTML?

Let us see how to set the background color in HTML using the bgcolor attribute and other methods. The syntax for setting the background color using the bgcolor attribute is as follows:

Here value can be the color name, RGB number, or hex number of the color.

Let us see this with the help of an example:

set background color in HTML

The bgcolor property discussed above was used earlier but has been omitted from HTML 5 onwards. We now utilize the style attribute to set the background color of a page. In this section, we’ll go through the style attribute in detail.

The style attribute used with HTML tags specifies inline styles for an element. This attribute has a background-color property, which can be used to set the background color of the HTML element. If the style attribute is used, it will override all other global style sets. We can use an inline style attribute or use internal CSS for the same.

Inline CSS allows us to style specific HTML elements. Let us see this with the help of the following code:

style property using inline css

Internal CSS is used to include CSS inside the head section of the document.

Let us see this with the help of the following code:

style attribute using internal css

Output:

Let us now explore the various ways in which style attributes can be used to set the background color of the web page.

  1. By specifying the desired color — In this method, we mention the color that we want our background to be. Eg — red, blue, etc..
  1. By using hex color codes — The background color can be specified through hexadecimal color codes.
  1. By using RGB color values — The background color can be specified through RGB color values. These values refer to the amount of red, green and blue in color. Each RGB value is a number between 0 and 255 .
  1. By using HSL color values — HSL stands for hue, saturation, and lightness. The background color can be specified through HSL color values. Hue refers to the degree of intensity on the color wheel, where 0 is red, 240 is blue, and 120 is green. The saturation level is a percentage between 0 to 100 where 0% means grey and 100% means full color. Lightness is also a percentage between 0 to 100 , defining the intensity of the color, where 0% is black and, 100% is white, 50% is medium.
  1. By creating gradient background — We can set the background color of a webpage by creating a gradient background. It can be achieved by creating a background color gradient by mentioning the direction of the gradient along with the colors. Gradients can be linear, radial, or conic.

Let us see this with the help of the following example:

background linear gradient

Output:

In the above example, we define a linear gradient that starts from the left with the color red and transits to yellow. If more control over the direction of the gradient is desired, angles can be used instead of predefined directions.

Examples

We’ll now see various examples of code snippets demonstrating how to use the background-color property of the style attribute with different HTML elements. We shall build a short and sweet webpage step by step that contains a heading and some text. We shall use imported fonts to make the texts eye-appealing.

Example 1: Setting the Background Color of Body

background color property of the style attribute

We have created a heading and applied our imported font. We have set the background color of the body as pink by using the style attribute inside the body tag.

Example 2: Setting the Background Color of div Elements

Setting the background color of div elements

In this example, the style attribute has been used with the div tag, too, along with the body tag. The background color that we want the div element to be has been mentioned in the appropriate style attribute. We have set the background color of the second div element as light green and added some text to it.

Browser Support

Browser Support for

Browser Support for background-color

Learn more

Conclusion

  • bgcolor attribute was used to set the background color of a page till HTML 5.
  • Now, we use the style attribute for changing the background color.
  • In style attribute, we have background-color property, which is used to set the background color of tags.
  • Background color can be set by specifying the color, using RGB codes, hex color codes, HSL values, or by creating a color gradient.

Источник

Оцените статью