Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different opacity element within a div

Tags:

html

css

opacity

I need to change opacity of an element within a div, to give it more opacity than the parent. How can I give the .solid a full opacity (="1"), without pre-processors?

.half-faded {
  opacity:0.3;
  border:1px solid black;
  width:200px;
  display:flex;
  justify-content:space-around;
}
.solid {
  opacity:1; /* doesn't help */
  opacity:2; /* twice doesn't help */
}
<div class="half-faded">
  <span class="one">One</span>
  <span class="two">Two</span>
  <span class="solid">Solid</span>
  <span class="four">Four</span>
</div>
like image 342
Aydin4ik Avatar asked Sep 29 '17 02:09

Aydin4ik


People also ask

How do I make part of a div transparent?

Example explained First, we create a <div> element (class="background") with a background image, and a border. Then we create another <div> (class="transbox") inside the first <div>. The <div class="transbox"> have a background color, and a border - the div is transparent.

How do you change the opacity of an element?

To set the opacity of a background, image, text, or other element, you can use the CSS opacity property. Values for this property range from 0 to 1. If you set the property to 0, the styled element will be completely transparent (ie. invisible).

How do you inherit opacity?

Opacity is not inherited, but because the parent has opacity that applies to everything within it. You cannot make a child element less transparent than the parent, without some trickery. Values are a number from 0 to 1 representing the opacity of the channel (the “alpha” channel).

How do you apply opacity without affecting a child element?

Answer: Use the CSS RGBA colors There is no CSS property like "background-opacity" that you can use only for changing the opacity or transparency of an element's background without affecting its child elements.


2 Answers

If .half-faded has multiple elements (not just spans), you could do:

.half-faded > * {
  opacity: .3;
}

.half-faded > .solid {
  opacity: 1;
}

[Edit]

The only way to do what you asked in the comment is to wrap the "Solid" text inside a <span> for example, as shown in the snippet below. The reason is that the .half-faded > .solid rule turns the opacity of the .solid elements back to 1, so we would have to turn down the opacity of the text outside the .biz element back to 0.3, however there's no CSS rule to match text nodes, therefore you need to have the text inside an element.

If you're able to change how the markup is built, I think it'd be easier to change the way it's being built.

.half-faded > * {
  opacity: .3;
  display: block;
}

.half-faded > .solid {
  opacity: 1;
}

.half-faded > .solid > span {
  opacity: .3;
}
<div class="half-faded">
  <div>Not solid</div>
  <a class="solid">Solid</a>
  <span class="solid">
    <span>Inside solid but not solid</span>
    <a href="#" class="biz">Solid business</a>  
  </span>
</div>
like image 120
Parziphal Avatar answered Nov 17 '22 06:11

Parziphal


You could do:

.half-faded {
  border: 1px solid rgba(0, 0, 0, 0.3);
  width: 200px;
  display: flex;
  justify-content: space-around;
}

.half-faded * {
  opacity: 0.3;
}

.half-faded .solid {
  opacity: 1;
}
<div class="half-faded">
  <span class="one">One</span>
  <span class="two">Two</span>
  <span class="solid">Solid</span>
  <span class="four">Four</span>
  <div>other</div>
</div>
like image 2
Syden Avatar answered Nov 17 '22 06:11

Syden