Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Set color to parent's background-color

Tags:

css

From the image below:

enter image description here

How would I make the text and icons inside the white boxes be the same color as the background of the big box?

Here is how the markup looks

<div class="class-item blue-bg">
  <!--Heading stuff-->
  <div class="class-item-actions">
    <span class="pick-up-icon">
      <i class="fa fa-female fa-lg"></i>
      <i class="fa fa-child"></i>
    </span>
    <span class="full-day-icon blue">AM / PM</span>
    <span class="unapproved-projects-icon blue">
     2&nbsp;<i class="fa fa-th-large fa-lg fa-fw class-item-action"></i>
    </span>
    <i class="fa fa-unlock fa-lg></i>
  </div>
</div>

So essentially, how can I make it so somehow I don't need to add the blue class to full-day-icon and unapproved-projects-iconand instead it inherits the blue color from class-item's background-color?

EDIT: Here is the relevant CSS

  .class-item {
    padding: 10px;
    width: 90%;
    color: white;
    margin-bottom: 5px;
    cursor: pointer;
    opacity: 0.85;
  }

  .full-day-icon {
    font-weight: bold;
    background: white;
    padding: 5px;
    border-radius: 3px;
    font-size: 0.8em;
    position: relative;
    top: -1px;
  }

  .unapproved-projects-icon {
    background-color: white;
    padding: 5px;
    border-radius: 3px;
    font-size: 0.8em;
    position: relative;
    top: -1px;
  }
like image 376
Robert Avatar asked Aug 09 '17 01:08

Robert


People also ask

How do I get parent background color in CSS?

There is no way, in CSS, to get an element to take its background from some arbitrary other element (or even ancestor). You can either be specific or you can inherit from the parent.

Is background color inherited in CSS?

Background properties do not inherit, but the parent element's background will shine through by default because of the initial 'transparent' value on 'background-color'. In terms of the box model, "background" refers to the background of the content and the padding area.

Which CSS property sets a background color for an element?

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


1 Answers

If it's okay for you to use CSS Variables, here is a neat and simple way to do this:

  .class-item {
    /** Specify the BG color in one place. **/
    --class-item-bg: #0076A5;
    padding: 10px;
    width: 90%;
    background-color: var(--class-item-bg);
    color: white;
    margin-bottom: 5px;
    cursor: pointer;
    opacity: 0.85;
  }

  .full-day-icon {
    font-weight: bold;
    background: white;
    color: var(--class-item-bg);
    padding: 5px;
    border-radius: 3px;
    font-size: 0.8em;
    position: relative;
    top: -1px;
  }

  .unapproved-projects-icon {
    background-color: white;
    color: var(--class-item-bg);
    padding: 5px;
    border-radius: 3px;
    font-size: 0.8em;
    position: relative;
    top: -1px;
  }

You can check this fiddle to see how you can then use JavaScript to change the property so that it affects all the CSS rules where it is used.

like image 138
sangeeth96 Avatar answered Sep 24 '22 22:09

sangeeth96