I want to make my webpage with a background-color with opacity: 0.5 but the content inside the webpage will have an opacity: 1, as the default value.
The problem is that if I set opacity: 0.5 to the container, all the childs inside this container gets the same opacity value.
I have searched about opacity specifications and saw this:
Inherited No
but in my case it is being inherited so I have searched a bit more and found another transparency specification in which I saw this:
If the object is a container element, then the effect is as if the contents of the container element were blended against the current background using a mask where the value of each pixel of the mask is .
So, as it seems that it is impossible to set a parent with less opacity than its childs, is there some workaround to get it?
Note: I think that in this case is not very important to add code (because you can reproduce it easily) but here I have created a simple JSFiddle "to play" with it.
Thanks in advance!
No, it's not possible.
If you only want a semi-transparent background without affecting contents, you can use a rgba color.
The RGB color model is extended in this specification to include
“alpha” to allow specification of the opacity of a color.
For example,
body {
background: linear-gradient(to right, #fff, #ff0, #0ff);
}
p {
background-color: rgba(0, 0, 255, 0.3); /* semi-transparent solid blue */
padding: 70px;
}
<p>Semi-transparent background but fully opaque text</p>
Not possible. Opacity to a parent container will also apply to the children. If you want to have a background with an opacity effect you could use RGBA for the background color. This applies to solid colors and there is an option for working with gradients as well.
If you have an image you want to use, you could position absolute the image behind your content using a div/container. Give that container an opacity and a position.
My slider background shows up in Chrome, Firefox, and most versions of IE, but not in IE8. Its background stays the same color as the rest of the page.
.site-slider {
width: 100%;
background: url(images/alexandria/header_overlay.png) no-repeat center top,
url(images/alexandria/header_bg.jpg);
}
If I change background to background-image, every browser shows the same thing that IE8 does.
IE doesn’t support multiple backgrounds until version 9. You can get around this by adding a wrapper element and applying one background to the parent and one to the child.
.site-slider-wrapper {
background-image: url(images/alexandria/header_bg.jpg);
}
.site-slider {
background: url(images/alexandria/header_overlay.png) no-repeat center top;
}
The reason changing the property name to background-image breaks the CSS in every browser is that the first background sets background-repeat and background-position properties (no-repeat center top) as well, which aren’t valid as part of background-image.
(The whole point of background is to be a shorthand for the background-* properties.)
You're using a CSS3 "Multiple Backgrounds" feature, which isn't supported in IE8. See its support on this link
Please read this tutorial and find the heading where it says "Multiple Backgrounds". Here you'll find a way to make this property work on IE8 as well.
Secondly, background is a shorthand property where you can combine/define values for the following properties:
background-color
background-image
background-repeat
background-attachment
background-position
Eg: background: #00ff00 url("smiley.gif") no-repeat fixed center;
On the other hand, if you use any property from the list above, it will accept only values specific to them. They won't work if you'll try to combine any other property value with them. Because of the same reason, your background-image property isn't working. You can only define the urlof the image as a value of background-image
Eg: background-image: url("paper.gif");
Adding this line
src /*\**/: url('skins/fonts/titillium/TitilliumText22L003-webfont')\9
seemed to fix it...
I saw a css code where it was like
body { background: transparent url ('background.jpg') repeat scroll;}
What does the transparent value do? I tried google'ing about this, but no help. Wouldn't background.jpg just override it?
Thank you.
transparent is the color. An element can have both a background image and a background color.
The above is equivalent to:
body {
background-color: transparent;
background-image: url('background.jpg');
background-repeat: repeat;
background-attachment: scroll;
}
The color is important in general if e.g. the background image fails to load, or the image contains transparent regions, or the image does not repeat to fill the entire area (which is admittedly not the case in your example).
However, since transparent is the "initial value", it is never necessary when using the background shorthand, since the shorthand automatically sets all unspecified properties to their initial value.
Thus, the only use case where transparent makes sense as a background color involves:
not using the shorthand, but instead directly using the background-color property;
using it to override another selector applying directly to that element.
An example would be
body.foo { background-color: blue; }
body.foo.bar { background-color: transparent; }
Actually, it is not required.
http://www.w3.org/TR/CSS21/colors.html#background
Given a valid declaration, the 'background' property first sets all the individual background properties to their initial values, then assigns explicit values given in the declaration.
Since background-color's initial value is transparent, it is applied implicitly when setting background:url(...);
More precisely, your style rule is equivalent to
background-color: transparent;
background-image: url(...);
background-repeat: repeat;
background-attachment: scroll;
background-position: 0% 0%;
in both cases.
However, many authors (including myself) prefer to explicitly set the value
for readability
to prevent any browser bugs, or simply
because they don't know better
What's the difference between specifying a background color using background and background-color?
Snippet #1
body { background-color: blue; }
Snippet #2
body { background: blue; }
Premising that those are two distinct properties, in your specific example there's no difference in the result, since background actually is a shorthand for
background-color
background-image
background-position
background-repeat
background-attachment
background-clip
background-origin
background-size
Thus, besides the background-color, using the background shorthand you could also add one or more values without repeating any other background-* property more than once.
Which one to choose is essentially up to you, but it could also depend on specific conditions of your style declarations (e.g if you need to override just the background-color when inheriting other related background-* properties from a parent element, or if you need to remove all the values except the background-color).
background will supercede all previous background-color, background-image, etc. specifications. It's basically a shorthand, but a reset as well.
I will sometimes use it to overwrite previous background specifications in template customizations, where I would want the following:
background: white url(images/image1.jpg) top left repeat;
to be the following:
background: black;
So, all parameters (background-image, background-position, background-repeat) will reset to their default values.
About CSS performance :
background vs background-color :
Comparison of 18 color swatches rendered 100 times on a page as small
rectangles, once with background and once with background-color.
While these numbers are from a single page reload, with subsequent
refreshes the render times changed, but the percent difference was
basically the same every time.
That's a savings of almost 42.6ms, almost twice as fast, when using
background instead of background-color in Safari 7.0.1. Chrome 33
appears to be about the same.
This honestly blew me away because for the longest time for two reasons:
I usually always argue for explicitness in CSS properties, especially with backgrounds because it can adversely affect specificity down the road.
I thought that when a browser sees background: #000;, they really see background: #000 none no-repeat top center;. I don't have a link to a resource here, but I recall reading this somewhere.
Ref : https://github.com/mdo/css-perf#background-vs-background-color
With background you can set all background properties like:
background-color
background-image
background-repeat
background-position
etc.
With background-color you can just specify the color of the background
background: url(example.jpg) no-repeat center center #fff;
VS.
background-image: url(example.jpg);
background-position: center center;
background-repeat: no-repeat;
background-color: #fff;
More info
(See Caption: Background - Shorthand property)
One of the difference:
If you use a image as background in this way:
background: url('Image Path') no-repeat;
then you cannot override it with "background-color" property.
But if you are using background to apply a color, it is same as background-color and can be overriden.
eg: http://jsfiddle.net/Z57Za/11/ and http://jsfiddle.net/Z57Za/12/
I've found that you cannot set a gradient with background-color.
This works:
background:linear-gradient(to right, rgba(255,0,0,0), rgba(255,255,255,1));
This doesn't:
background-color:linear-gradient(to right, rgba(255,0,0,0), rgba(255,255,255,1));
There is no difference. Both will work in the same way.
CSS background properties are used to define the background effects of
an element.
CSS properties used for background effects:
background-color
background-image
background-repeat
background-attachment
background-position
Background property includes all of this properties and you can just write them in one line.
They're both the same. There are multiple background selectors (i.e. background-color, background-image, background-position) and you can access them either through the simpler background selector or the more specific one. For example:
background: blue url(/myImage.jpg) no-repeat;
or
background-color: blue;
background-image: url(/myImage.jpg);
background-repeat: no-repeat;
The difference is that the background shorthand property sets several background-related properties. It sets them all, even if you only specify e.g. a color value, since then the other properties are set to their initial values, e.g. background-image to none.
This does not mean that it would always override any other settings for those properties. This depends on the cascade according to the usual, generally misunderstood rules.
In practice, the shorthand tends to be somewhat safer. It is a precaution (not complete, but useful) against accidentally getting some unexpected background properties, such as a background image, from another style sheet. Besides, it’s shorter. But you need to remember that it really means “set all background properties”.
Comparison of 18 color swatches rendered 100 times on a page as small
rectangles, once with background and once with background-color.
I recreated the CSS performance experiment and the results are significantly different nowadays.
background
Chrome 54: 443 (µs/div)
Firefox 49: 162 (µs/div)
Edge 10: 56 (µs/div)
background-color
Chrome 54: 449 (µs/div)
Firefox 49: 171 (µs/div)
Edge 10: 58 (µs/div)
As you see - there's almost no difference.
background is the shortcut for background-color and few other background related stuffs as below:
background-color
background-image
background-repeat
background-attachment
background-position
Read the statement below from W3C:
Background - Shorthand property To shorten the code, it is
also possible to specify all the background properties in one single
property. This is called a shorthand property.
The shorthand property for background is background:
body {
background: white url("img_tree.png") no-repeat right top;
}
When using the shorthand property the order of the property values is:
background-color
background-image
background-repeat
background-attachment
background-position
It does not matter if one of the property values is missing, as long
as the other ones are in this order.
This is the best answer. Shorthand (background) is for reset and DRY (combine with longhand).
background is shorthand property for the following:
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
You can detailed info on every property here
Properties order
In most of browser implementation (i think maybe older browser could present issues) the order of the properties does not matter, except for:
background-origin and background-clip: when both of this properties are present, the first one refer to -origin and the second to -clip.
Example:
background: content-box green padding-box;
Is equivalent to:
background-origin: content-box;
background-color: green;
background-clip: padding-box;
background-size must always follow background-position and the properties must be separated by /
if background-position is composed by two numbers, the first one is the horizontal value and the second the vertical value.
I've noticed when generating emails for Outlook...
/*works*/
background: gray;
/*does not work*/
background-color: gray;
You can do some pretty neat stuff once you understand that you can play with inheritance with this. However first let's understand something from this doc on background:
With CSS3, you can apply multiple backgrounds to elements. These are
layered atop one another with the first background you provide on top
and the last background listed in the back. Only the last background
can include a background color.
So when one do:
background: red;
He is setting the background-color to red because red is the last value listed.
When one do:
background: linear-gradient(to right, grey 50%, yellow 2%) red;
Red is the background color once again BUT you will see a gradient.
.box{
border-radius: 50%;
width: 200px;
height: 200px;
background: linear-gradient(to right, grey 50%, yellow 2%) red;
}
.box::before{
content: "";
display: block;
margin-left: 50%;
height: 50%;
border-radius: 0 100% 100% 0 / 50%;
transform: translateX(70px) translateY(-26px) rotate(325deg);
background: inherit;
}
<div class="box">
</div>
Now the same thing with background-color:
.box{
border-radius: 50%;
width: 200px;
height: 200px;
background: linear-gradient(to right, grey 50%, yellow 2%) red;
}
.box::before{
content: "";
display: block;
margin-left: 50%;
height: 50%;
border-radius: 0 100% 100% 0 / 50%;
transform: translateX(70px) translateY(-26px) rotate(325deg);
background-color: inherit;
}
<div class="box">
</div>
The reason this happens is because when we are doing this :
background: linear-gradient(to right, grey 50%, yellow 2%) #red;
The last number sets the background-color.
Then in the before we are inheriting from background (then we get the gradient) or background color, then we get red.
One thing I've noticed that I don't see in the documentation is using
background: url("image.png")
short hand like above if the image is not found it sends a 302 code instead of being ignored like it is if you use
background-image: url("image.png")
There's a bug regarding with background and background-color
the difference of this,
when using background, sometimes when your creating a webpage
in CSS
background: #fff // can over ride a block of Mask image("top item, text or image"))
so its better to always use background-color
for safe use, in your design if its individual
Please see the following picture:
Why is padding-top and margin-top fully opaque, but padding-right etc. not?
I believe they are semi-transparent because they're not explicitly defined.
Consired following sheet:
selector1 {
margin: 20px;
}
selector2 {
margin: 20px;
margin-top: 10px;
}
selector3 {
margin: 10px 20px 30px;
}
In first example (selector1) all margin-* properties will be semi-transparent because non is explicitly defined - shortcut is being used.
In second example (selector2) only margin-top will be fully opaque, as it's defined in its own property.
In last example (selector3), margin-top and margin-bottom are defined explicitly, therefore the will be fully opaque. However margin-left and margin-right are defined by a single value, so they will be semi-transparent.
Semi-transparent color is also applied to default values, for instance:
background: red url(...) no-repeat;
This property defined background-color, background-image and background-repeat explicitly, however background-position, background-clip, background-size etc. are not defined (default values are used) so they will be seen as semi-transparent.
Grey colored entries are rules that are not defined but instead computed at runtime. Please refer this page