What is the best way to combine colors using CSS?

I am trying to customize my code with the following elements:

.featured {
  background-color: rgba(100, 150, 200, 1);
}
.hidden {
  background-color: black;
}
<div>
    <p class="featured hidden">Lorem ipsum dolor sit amet</p>
    <p class="hidden">Consectetur adipiscing elit.</p>
</div>

Is there a way to combine classes in such a way that if both .hidden and .featured are applied to an element, the resulting background color will be a combination of the individual colors specified for these classes?

My goal is to create a darker shade when both classes are used together, rather than just using the .hidden class alone.

Answer №1

Using `background-blend-mode` for blending backgrounds

If you use background-blend-mode: multiply;, you can achieve the effect of darkening the background-color of the element with the class .deleted.

To make this work, you need to make the following changes:

  • Change
    background-color:rgba(228, 225, 219, 1);
    to
    background-image: linear-gradient(0deg, rgba(228, 225, 219, 1), rgba(228, 225, 219, 1));
    in the .reviewed class. This will give .reviewed the same background color and allow background-blend-mode to interact with the background-color of .deleted
  • Add background-blend-mode: multiply; to the .deleted class to blend its background color with the "background image" set on .reviewed

.reviewed {
  background-image: linear-gradient(0deg, rgba(228, 225, 219, 1), rgba(228, 225, 219, 1));
}
.deleted {
  background-blend-mode: multiply;
  background-color: red;
}
<table>
  <tr>
    <td>Number</td>
    <td>Name</td>
  </tr>
  <tr class="reviewed">
    <td>1</td>
    <td>Reviewed</td>
  </tr>
  <tr class="reviewed deleted">
    <td>2</td>
    <td>Reviewed and deleted</td>
  </tr>
  <tr class="deleted">
    <td>3</td>
    <td>Deleted</td>
  </tr>
</table>

The benefit of using this method is that you don't need to specify a third color as CSS handles the calculation. However, it's important to note that IE and Edge do not currently support this feature.

background-blend-mode is compatible with Firefox, Chrome, and has partial support in Safari. Check out this link for more details on browser support.

Answer №2

.reviewed {
    background-color:rgba(228, 225, 219, 1);
}

.deleted {
    background-color:red;
}
.reviewed.deleted {
    background-color:/*use color code for something dark red*/
}

.reviewed.deleted signifies an element with both the reviewed and deleted classes.

Answer №3

If you want to achieve a similar effect as in Photoshop, you can set the CSS background to a gradient.

Here is an example of a gradient:

background-image: -webkit-linear-gradient(to bottom right, red, rgba(255,0,0,0));
background-image: -ms-linear-gradient(to bottom right, red, rgba(255,0,0,0));
background-image: -o-linear-gradient(to bottom right, red, rgba(255,0,0,0));
background-image: -moz-linear-gradient(to bottom right, red, rgba(255,0,0,0));

Answer №4

If you're working with a CSS preprocessor like SASS or LESS, one trick is to define two color variables and use mixing functions to create a third color.

Take a look at this SASS example:

$color1: rgba(228, 225, 219, 1);
$color2: red;

.reviewed {
  background-color: $color1;
}
.deleted {
  background-color: $color2;
}
.reviewed.deleted {
  background-color: mix($color1, $color2);
}

For a similar approach using LESS, check out this LESS Example.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using focusout and clicking buttons do not effectively interact with child elements

I have a series of div elements, each containing a button. When I click on a button, text is displayed, and when I click away, the information hides with a focus-out function. If I have one button open and then want to click on another parent button, it wo ...

What is the process for transferring a folder to public storage in Laravel?

I've been attempting to upload and save a folder (which contains several files) into my local directory, but I'm encountering difficulties. Below is the input form I have: <input type="file" name="folder" id="folder&q ...

Functionality Issue: Submit Button Not Working on Designed Form Select

Through dedication and hard work, I managed to create a customized form with images that display correctly in Firefox, Chrome, and Internet Explorer's compatibility mode. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

What is preventing me from updating one dropdown list based on the selection made in another dropdown list?

I have a situation on a classic asp page where there are two dropdown lists: <select id="ddlState" name="ddlState" runat="server"> <option value="KY">Kentucky</option> <option value="IN" ...

The SVG image exceeds the boundaries of the parent column div in bootstrap

I am struggling to make a SVG fit inside a column in a bootstrap div with col-lg-4. I tried using img-fluid, but it did not work as expected. The SVG should automatically adjust its size to fit the parent div. However, that is not happening. .star-ratin ...

Creating a floating border effect.Would you like to know how

Currently working on a new component, I'm looking to create a unique border for certain cards. However, I'm struggling to find the perfect solution. Check out the specific border style needed in the image provided below. https://i.sstatic.net/4vA ...

uniformly distribute the list items on the horizontal navigation bar

Does anyone know how to properly space out a ul in a navigation bar, with 5px padding on the right and left and the text evenly spaced in between? body { background: #ffffff; text-align: center; font-family: helvetica, arial, san ...

Maintaining a fixed header that remains visible while scrolling through a dropdown menu in Angular

In my application, I have a mat-select widget that displays a list of options. When scrolling within the list, I find it difficult to keep track of all the options. I am looking to enhance the user experience by adding a fixed header at the top of the opt ...

Ways to programmatically include superscripts to numbers in ASP.NET/C#?

Is there a way to dynamically add superscript to numbers in C# within an ASP.NET application? For example: 1st, 2nd ...... If you have any suggestions or know of the best possible solution, please share. Thank you! ...

JavaScript cannot validate all things; it only validates a singular function

I need help with validating two functions at once. Currently, I am able to navigate to 'success.html' after entering the correct information for the form (name, subject, and number), but the radio buttons remain unchecked. I believe this issue ...

What steps should be taken to validate an HTML5 form without actually submitting it?

I am working on a form that includes various HTML attributes such as: <form> <label>Full Name</label> <input type="text" name="firstname" class="span3" required="required"> <label>Email Address</label&g ...

Creating a for loop using jQuery to append the value of [i] to the string "id" for the element

As a newcomer to programming, my code may not be the best. I am attempting to automate game results (1 / X / 2) based on the input of home and away goals in a form. To achieve this, I need assistance with my jQuery for loop. Here is the current code: for ...

There is excessive space beneath the text

I am struggling with aligning text vertically to match the chevrons. I have attempted various CSS properties like removing paddings/margins, setting line-height to 1, using text-decoration: none, text-align: center, and vertical-align: middle on my <p&g ...

What is the best way to ensure that the text on the sub menus in the recipes navigation bar is displayed in vibrant colors?

Can anyone help me figure out how to make the blue hover effect fit perfectly with the subheadings in my recipes menu? Right now, the color doesn't cover the text fully. Feel free to read and run my code for a better understanding. This is the CSS I ...

What is the best way to create a dot animation using CSS or JavaScript?

https://i.sstatic.net/60Hym.gif This particular GIF is sourced from Dribbble. I attempted to create a demo using pure CSS. Below are snippets of my code: @keyframes circles{ 0%{ transform: scale(0) rotate(150deg); } 100%{ transform: scale( ...

Enable scrolling feature for the table

I have integrated a bootstrap table into my React application, but I am facing an issue where the table is not scrollable. Whenever new content is loaded, it increases the height of the table instead of making it scrollable. Below is the code for my table ...

Background image fixed with scrolling effect

I've been struggling with a parallax effect on my website. After seeing it work smoothly on other websites, I tried to implement it myself but couldn't quite get it right. The background image keeps moving when I scroll the page and I want it to ...

I'm looking to split the Arabic characters "lam alef" (لا) into two separate spans in HTML

Yesterday, I dedicated numerous hours trying to figure out how to present the lam alef letter in Arabic using two separate spans with different colors. Unfortunately, my attempts only displayed it as ل‎‍ ‍‎ا ...

Styling child elements in Angular using css from its parent element

I have a question: Regarding the structure below <component-parent> <first-child> <second-child> <third-child></third-child> </second-child> </first-child> </component-parent> Now I want t ...

HTML5 Local Storage allows web developers to store data locally within

After saving a value in the local storage (HTML5) on page 1, I noticed that when I navigate to page 2, the values stored in the local storage disappear. Any insights or suggestions on what might be causing this issue? Upon reviewing my code, I am using th ...