Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

Is it possible, using CSS only, to make the background of an element semi-transparent but have the content (text & images) of the element opaque?

I'd like to accomplish this without having the text and the background as two separate elements.

When trying:

p {

  position: absolute;

  background-color: green;

  filter: alpha(opacity=60);

  opacity: 0.6;

}

span {

  color: white;

  filter: alpha(opacity=100);

  opacity: 1;

}

<p>

  <span>Hello world</span>

</p>

 Run code snippetExpand snippet

It looks like child elements are subjected to the opacity of their parents, so opacity:1 is relative to the opacity:0.6 of the parent.

1 Answer

0 votes
by (40.7k points)

You can either use a semi-transparent PNG image or CSS3:

background-color:rgba(255,0,0,0.5);

Have a look at this article from css3.info, Opacity, RGBA and compromise.

<p style="background-color:rgba(255,0,0,0.5);">

  <span>Hello world</span>

</p>

Related questions

0 votes
1 answer
asked Aug 26, 2019 in Web Technology by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Aug 28, 2019 in Web Technology by Tech4ever (20.3k points)
0 votes
1 answer
asked Aug 26, 2019 in Web Technology by Tech4ever (20.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...