1

If I have

.parent {
    background-color: #000;
}

.child {
    // the background from the parent with an alpha
    // background-color: inherit + alpha
}

can I apply an alpha to the inherited background color?

<div class="parent">
    <div class="child">this is the child</div>
</div>  

as far as I know the only way is to pass a color rgba(255, 0, 0, 0.3);} any ideas on how to achieve this? thanks!

handsome
  • 2,335
  • 7
  • 45
  • 73

3 Answers3

0

I added some lines to css so you can see the difference. I marked the important lines in css. I try the explain, but my English not so good :) If i did mistake my sentences, sorry for about that :)

.parent {
    --r: 0; /* set r variable */
    --g: 0; /* set g variable */
    --b: 0; /* set b variable */
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: rgb(var(--r), var(--g), var(--b)); /* use rgb color with variables */
    width: 100%;
    height: 100px;
}

.child {
    display: flex;
    background-color: rgba(var(--r), var(--g), var(--b), 0.5); /* use rgba color with parent variables */
    color: white;
    width: 25%;
    height: 50px;
    text-align: center;
    align-items: center;
    justify-content: center;
    margin-top: 100px;
}
<div class="parent">
    <div class="child">this is the child</div>
</div>
Osman Durdag
  • 955
  • 1
  • 7
  • 18
0

alpha has no effect as long as the same color is inherited :) - But I understand what you want - i hope example is useful

div {
  background-color: #000;
  width: 200px;
  height: 150px;
}
div p {
  background-color: inherit;
  position: relative;
  text-align: center;
}
span {
  position: relative;
  font-size: 25px;
  font-weight: bold;
  text-align: center;
  color: #f00;
  text-transform: uppercase;
  z-index: 1;
  padding: 10px;
  display: inline-block;
}
div p::after {
  content: '';
  opacity: 0.2; /* here alpha */
  background-color: #fff;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
}
<div>
  <p><span>test</span></p>
</div>
Emy
  • 639
  • 4
  • 13
0

Here is an idea using pseudo element and relying on inherit. The trick is to consider a gradient insitead of color:

.parent {
  height: 100px;
  background: linear-gradient(#45f206 0 0); /* we need a gradient and not a color for the trick to work*/
}

.child {
  padding: 30px;
  background: inherit;
  background-size: 0; /* this is important to avoid the child getting the color! */
  position: relative;
  z-index:0;
  transform:translateY(90px); /* to illustrate */
}

.child::before {
  content: "";
  position: absolute;
  z-index:-1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: inherit; /* we get the color from the child that got it from the parent */
  opacity: 0.5; /* your opacity here */
}
<div class="parent">
  <div class="child">this is the child</div>
</div>

With CSS variable for more flexibility:

.parent {
  --c:#45f206;

  height: 100px;
  display:inline-block;
  background: linear-gradient(var(--c) 0 0); /* we need a gradient and not a color for the trick to work*/
}

.child {
  padding: 30px;
  background: inherit;
  background-size: 0; /* this is important to avoid the child getting the color! */
  position: relative;
  z-index:0;
  transform:translateY(90px); /* to illustrate */
}

.child::before {
  content: "";
  position: absolute;
  z-index:-1;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: inherit; /* we get the color from the child that got it from the parent */
  opacity: 0.5; /* your opacity here */
}
<div class="parent">
  <div class="child">this is the child</div>
</div>

<div class="parent" style="--c:red">
  <div class="child">this is the child</div>
</div>

<div class="parent" style="--c:rgb(0,0,255)">
  <div class="child">this is the child</div>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415