Html change background-color on condition

I am using following style to show div in my page as even odd combination: css:

.task-container:nth-child(even) {
    background: none;
}

.task-container:nth-child(odd) {
    background: rgba(2, 104, 144, .05);
}

.timeout {
    background-color: #ffc;
}

In HTML. I am using div in ng-repeat:

... other divs

With some conditions i want to change the background color like yellow but it is not working as nth-child(even)/odd overriding the yellow color. I tried something like that:

Can someone help me to find a correct way?

Html change background-color on condition

asked Aug 22, 2016 at 12:43

Assuming the .timeout class is being added properly on your div element, you just need to add the !important in your CSS:

.timeout {
    background-color: #ffc !important;
}

This is because, the .task-container:nth-child(even) or .task-container:nth-child(odd) is more specific so it will get higher priority.

Also, it is not recommended to use !important so you can write more specific selector like this (and you don't need !important flag):

.task-container.timeout {
    background-color: #ffc;
}

See a simplified example below:

.task-container:nth-child(even) {
  background: none;
}
.task-container:nth-child(odd) {
  background: rgba(2, 104, 144, .05);
}
.task-container.timeout {
  background-color: #ffc;
}
1
2
3
4
5
6
7

answered Aug 22, 2016 at 12:48

Html change background-color on condition

Shashank AgrawalShashank Agrawal

23.9k11 gold badges80 silver badges110 bronze badges

3

Below is the code written once I came across similar kind of problem. Hope this will help you. You may write it in footer and this will change the color of target cells of result page based on conditions:

The background-color CSS property sets the background color of an element.

Try it

Syntax

/* Keyword values */
background-color: red;
background-color: indigo;

/* Hexadecimal value */
background-color: #bbff00; /* Fully opaque */
background-color: #bf0; /* Fully opaque shorthand */
background-color: #11ffee00; /* Fully transparent */
background-color: #1fe0; /* Fully transparent shorthand */
background-color: #11ffeeff; /* Fully opaque */
background-color: #1fef; /* Fully opaque shorthand */

/* RGB value */
background-color: rgb(255, 255, 128); /* Fully opaque */
background-color: rgba(117, 190, 218, 0.5); /* 50% transparent */

/* HSL value */
background-color: hsl(50, 33%, 25%); /* Fully opaque */
background-color: hsla(
  50,
  33%,
  25%,
  0.75
); /* 75% opaque, i.e. 25% transparent */

/* Special keyword values */
background-color: currentcolor;
background-color: transparent;

/* Global values */
background-color: inherit;
background-color: initial;
background-color: revert;
background-color: revert-layer;
background-color: unset;

The background-color property is specified as a single value.

Values

The uniform color of the background. It is rendered behind any background-image that is specified, although the color will still be visible through any transparency in the image.

Accessibility concerns

It is important to ensure that the contrast ratio between the background color and the color of the text placed over it is high enough that people experiencing low vision conditions will be able to read the content of the page.

Color contrast ratio is determined by comparing the luminance of the text and background color values. In order to meet current Web Content Accessibility Guidelines (WCAG), a ratio of 4.5:1 is required for text content and 3:1 for larger text such as headings. Large text is defined as 18.66px and bold or larger, or 24px or larger.

  • WebAIM: Color Contrast Checker
  • MDN Understanding WCAG, Guideline 1.4 explanations
  • Understanding Success Criterion 1.4.3 | W3C Understanding WCAG 2.0

Formal definition

Formal syntax

background-color = 

Examples

HTML

<div class="exampleone">Lorem ipsum dolor sit amet, consectetuerdiv>

<div class="exampletwo">Lorem ipsum dolor sit amet, consectetuerdiv>

<div class="examplethree">Lorem ipsum dolor sit amet, consectetuerdiv>

CSS

.exampleone {
  background-color: transparent;
}

.exampletwo {
  background-color: rgb(153, 102, 153);
  color: rgb(255, 255, 204);
}

.examplethree {
  background-color: #777799;
  color: #ffffff;
}

Result

Specifications

Specification
Unknown specification
# background-color

Browser compatibility

BCD tables only load in the browser

See also

How do you change the conditional background color?

Set conditional formatting rules In Google Sheets, highlight the cell or range of cells that you want to apply formatting rules to. Conditional formatting. In the window that appears, define the conditions for specific text colors and/or cell background colors. Click Done.

How do I change the background color automatically in HTML?

You can set a background color for an HTML document by adding style="background-color:" to the element.

How can change background color of cell value in HTML?

Note: The Conditional Formatting is a dynamic feature, the cell color will be changed as the data changes. ... .

How do I change the background color of a div?

You can use the bgColor attribute, like bgColor="#6B6B6B" , in the body element to change the background-color of . The HTML bgcolor attribute is used to set the background color of an HTML element.