Making text background transparent but not text itself
Don't use opacity
for this, set the background to an RGBA-value instead to only make the background semi-transparent. In your case it would be like this.
.content {
padding:20px;
width:710px;
position:relative;
background: rgb(204, 204, 204); /* Fallback for older browsers without RGBA-support */
background: rgba(204, 204, 204, 0.5);
}
See http://css-tricks.com/rgba-browser-support/ for more info and samples of rgba-values in css.
CSS opacity only to background color, not the text on it?
It sounds like you want to use a transparent background, in which case you could try using the rgba()
function:
rgba(R, G, B, A)
R (red), G (green), and B (blue) can be either
<integer>
s or<percentage>
s, where the number 255 corresponds to 100%. A (alpha) can be a<number>
between 0 and 1, or a<percentage>
, where the number 1 corresponds to 100% (full opacity).RGBa example
background: rgba(51, 170, 51, .1) /* 10% opaque green */
background: rgba(51, 170, 51, .4) /* 40% opaque green */
background: rgba(51, 170, 51, .7) /* 70% opaque green */
background: rgba(51, 170, 51, 1) /* full opaque green */
A small example showing how rgba
can be used.
As of 2018, practically every browser supports the rgba
syntax.
Background Transparent but not Text
There are multiple things you are missing out.
First the structure itself is wrong. Using so many br tags is incorrect. Considering you are new to this portal, you should have researched bit more about background opacity.
.container{ position: relative; /* The image used */ background-image: url("https://www.elephantsdeli.com/wp-content/uploads/fly-images/1673/elephants-delicatessen-sack-lunch-order-form-hero-image.jpg-1920x1080.jpg");
/* Full height */ height:100vh; /* Center and scale the image nicely */ background-position: center; background-repeat: no-repeat; background-size: cover;}.background { height: 100vh; background: rgba(255 , 255, 255, 0.5)}.text-center { padding:50px 0;}
<div class="container" ><div class="background"> <h1 style="font-size: 350%;" class="text-center"><b>It's Lunch-Time!</b></h1> </div></div>
Change one of the bootstrap's background transparency without affecting text
Yes, use css rgba(red, green, blue, opacity)
to set opacity exclusively on the parent element's background property.
But note that Bootstrap v4 sets css:
.bg-secondary {
background-color: #6c757d!important;
}
Also, because hexadecimal rgba() is still lacking comprehensive browser support, it's advisable to convert hexadecimal #6c757d into it's decimal equivalent. In your case the Bootstrap override would be:
.bg-secondary {
background-color: rgba(108, 117, 125, 0.6) !important;
}
For your style to override bootstrap's style definition, you must define it after the bootstrap stylesheet and use the !important
post-fix as well.
Here's a working fiddle.
Can I set an opacity only to the background image of a div?
Nope, this cannot be done since opacity
affects the whole element including its content and there's no way to alter this behavior. You can work around this with the two following methods.
Secondary div
Add another div
element to the container to hold the background. This is the most cross-browser friendly method and will work even on IE6.
HTML
<div class="myDiv">
<div class="bg"></div>
Hi there
</div>
CSS
.myDiv {
position: relative;
z-index: 1;
}
.myDiv .bg {
position: absolute;
z-index: -1;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: url(test.jpg) center center;
opacity: .4;
width: 100%;
height: 100%;
}
See test case on jsFiddle
:before and ::before pseudo-element
Another trick is to use the CSS 2.1 :before
or CSS 3 ::before
pseudo-elements. :before
pseudo-element is supported in IE from version 8, while the ::before
pseudo-element is not supported at all. This will hopefully be rectified in version 10.
HTML
<div class="myDiv">
Hi there
</div>
CSS
.myDiv {
position: relative;
z-index: 1;
}
.myDiv:before {
content: "";
position: absolute;
z-index: -1;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: url(test.jpg) center center;
opacity: .4;
}
Additional notes
Due to the behavior of z-index
you will have to set a z-index for the container as well as a negative z-index
for the background image.
Test cases
See test case on jsFiddle:
- Using CSS 2.1 :before
- Using CSS 3 ::before
Opacity of background-color, but not the text
Use rgba!
.alpha60 {
/* Fallback for web browsers that don't support RGBa */
background-color: rgb(0, 0, 0);
/* RGBa with 0.6 opacity */
background-color: rgba(0, 0, 0, 0.6);
/* For IE 5.5 - 7*/
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000);
/* For IE 8*/
-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000)";
}
In addition to this, you have to declare
background: transparent
for IE web browsers, preferably served via conditional comments or similar!
via http://robertnyman.com/2010/01/11/css-background-transparency-without-affecting-child-elements-through-rgba-and-filters/
CSS : Background opacity is showing on text
Have a look at the following example. Instead of opacity
use rgba
.
According to docs:
opacity applies to the element as a whole, including its contents, even though the value is not inherited by child elements. Thus, the element and its children all have the same opacity relative to the element's background, even if they have different opacities relative to one another.
#backgroundimage { background: url("https://www.solidbackgrounds.com/images/640x480/640x480-aero-solid-color-background.jpg"); height: 500px}
#container { background-color: rgba(76,76,76,.5);}
#text { color: #ffffff;}
<div id="backgroundimage"> <div id="container"> <p id="text"> Some text here </p> </div></div>
How do I give text or an image a transparent background using CSS?
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).
Beware that the text still needs sufficient contrast with the background, once the underlying background shines through.
<p style="background-color: rgba(255, 0, 0, 0.5);">
<span>Hello, World!</span>
</p>
Related Topics
Most Common Way of Writing a HTML Table with Vertical Headers
How to Apply a Color to a Svg Text Element
How to Increase a Scrollbar's Width Using CSS
Make Bootstrap's Carousel Both Center and Responsive
Set Height 100% on Absolute Div
How to Create a Polygon Shape Div
Css3 Scale - Prevent from Scalling Text
CSS Custom Cursor Doesn't Work in Ff/Chrome
Pure CSS Chessboard with Div & No Classes or Ids, Is It Possible
Remove Blur Effect on Child Element
Fallback for CSS Attributes Without Unit
Restrict File Upload Selection to Specific Types
Hover Effects Not Working with IE8
Why Does Flexbox Stretch My Image Rather Than Retaining Aspect Ratio
How to Properly Use H1 in HTML5