![Css Background Image Opacity Css Background Image Opacity](/uploads/1/2/4/7/124787312/748274584.jpg)
Cross Browser Opacity. Opacity is now a part of the CSS3 specifications, but it was present for a long time. However, older browsers have different ways of controlling the opacity or transparency. CSS Opacity in Firefox, Safari, Chrome, Opera and IE9. Here is the most up to date syntax for CSS opacity in all current browsers. Hello, everyone, Myself and a co-worker are (basically) n00bs when it comes to CSS, so please forgive if this question seems elementary. What is the best way to give opacity to the background of a.
Example
Set the opacity level for a <div> element:
Try it Yourself »More 'Try it Yourself' examples below.
Definition and Usage
The
opacity
property sets the opacity level for an element.The opacity-level describes the transparency-level, where 1 is not transparent at all, 0.5 is 50% see-through, and 0 is completely transparent.
opacity 0.5
Note: When using the
opacity
property to add transparency to the background of an element, all of its child elements become transparent as well. This can make the text inside a fully transparent element hard to read. If you do not want to apply opacity to child elements, use RGBA color values instead (See 'More Examples' below).Default value: | 1 |
---|---|
Inherited: | no |
Animatable: | yes, see individual properties. Read about animatableTry it |
Version: | CSS3 |
JavaScript syntax: | object.style.opacity='0.5' Try it |
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Note: IE8 and earlier versions supports an alternative, the filter property. Like: filter:Alpha(opacity=50).
CSS Syntax
Property Values
Value | Description | Play it |
---|---|---|
number | Specifies the opacity. From 0.0 (fully transparent) to 1.0 (fully opaque) | Play it » |
initial | Sets this property to its default value. Read about initial | Play it » |
inherit | Inherits this property from its parent element. Read about inherit |
More Examples
Example
The opacity property adds transparency to the background of an element, and to all of its child elements as well. This makes the text inside a transparent element hard to read:
div.first {
opacity: 0.1;
}
div.second {
opacity: 0.3;
}
div.third {
opacity: 0.6;
}
Try it Yourself »opacity: 0.1;
}
div.second {
opacity: 0.3;
}
div.third {
opacity: 0.6;
}
Example
To not apply opacity to child elements (like in the example above) use RGBA color values instead. The following example sets the opacity for the background color, but not for the text:
div.first {
background: rgba(76, 175, 80, 0.1);
}
div.second {
background: rgba(76, 175, 80, 0.3);
}
div.third {
background: rgba(76, 175, 80, 0.6);
}
Try it Yourself »background: rgba(76, 175, 80, 0.1);
}
div.second {
background: rgba(76, 175, 80, 0.3);
}
div.third {
background: rgba(76, 175, 80, 0.6);
}
Tip: Learn more about RGBA Colors in CSS RGBA Colors.
Example
How to use JavaScript to change the opacity for an element:
function myFunction(x) {
// Return the text of the selected option
var opacity = x.options[x.selectedIndex].text;
var el = document.getElementById('p1');
if (el.style.opacity ! undefined) {
el.style.opacity = opacity;
} else {
alert('Your browser doesn't support this example!');
}
}
Try it Yourself »// Return the text of the selected option
var opacity = x.options[x.selectedIndex].text;
var el = document.getElementById('p1');
if (el.style.opacity ! undefined) {
el.style.opacity = opacity;
} else {
alert('Your browser doesn't support this example!');
}
}
Related Pages
CSS Tutorial: CSS Opacity / Transparency
CSS Tutorial: CSS RGBA Colors
HTML DOM Reference: opacity property
Images are an important part of attractive website designs. This includes the use of background images. These are the images and graphics that are used behind areas of a page as opposed to images that are presented as part of the content pages. These background images can add visual interest to a page and help you achieve the visual design that you may be looking for on a page.
If you begin working with background images, you will undoubtedly run into the scenario where you want an image to stretch to fit the page. This is especially true for responsive websites that are being delivered to a wide range of devices and screen sizes.
This desire to stretch a background image is a very common desire for web designers because not every image fits in the space of a website. Instead of setting a fixed size, stretching the image allows it to flex to fit the page no matter how wide or narrow the browser window is.
The Modern Way
The best way to stretch an image to fit the background of an element is to use the CSS3 property, for background-size, and set it equal to cover.
Take a look at this example of it in action. Here's the HTML in the image below.
Now, take a look at the CSS. It's not much different than the code above. There are a few additions to make it clearer.
Now, this is the result in full screen.
By setting background-size to cover, you'll guarantee that browsers will automatically scale the background image, however large, to cover the entire area of the HTML element that it's being applied to. Take a look at a narrower window.
According to caniuse.com, this one is supported by over ninety percent of browsers, making it an obvious choice in most situations. It does create some problems with Microsoft browsers, so a fallback might be necessary.
The Fallback Way
Here is an example that uses a background image for the body of a page and which sets the size to 100% so that it will always stretch to fit the screen. This method isn't perfect, and it might cause some uncovered space, but by using the background-position property, you should be able to eliminate the problem and still accommodate older browsers.
Using the example from above with the background-size set to 100% instead, you can see that the CSS looks mostly the same.
The result on a full screen browser, or one with similar dimensions to the image is nearly identical. However, with a narrower screen, the flaws start to show.
Clearly, it isn't ideal, but it will work as a fallback.
According to caniuse.com, this property works in IE 9+, Firefox 4+, Opera 10.5+, Safari 5+, Chrome 10.5+, and on all the major mobile browsers. This covers you for all the modern browsers available today, which means you should use this property without fear that it will not work on someone's screen.
Between these two methods, you shouldn't have any difficulty supporting nearly all browsers. As background-size: cover gains even more acceptance among browsers, even this fallback will become unnecessary. Clearly, CSS3 and more responsive design practices have simplified and streamlined using images as adaptive backgrounds within HTML elements.