Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I give text or an image a transparent background using CSS?

Tags:

html

css

opacity

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>

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.

like image 518
Stijn Sanders Avatar asked Apr 30 '09 09:04

Stijn Sanders


People also ask

How do I make text transparent with an image in CSS?

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 I make an image have a transparent background CSS?

opacity is a CSS property that allows you to change the opaqueness of an element. By default, all elements have a value of 1 . By changing this value closer to 0 , the element will appear more and more transparent. A common use case is using an image as part of the background.


1 Answers

Either use a semi-transparent PNG or SVG image or use CSS:

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

Here's an article from css3.info, Opacity, RGBA and compromise (2007-06-03).


<p style="background-color: rgba(255, 0, 0, 0.5);">   <span>Hello, World!</span> </p>
like image 122
9 revs, 8 users 29% Avatar answered Oct 06 '22 06:10

9 revs, 8 users 29%