148

Is there a difference between these two CSS properties:

background: none;
background: transparent;
  1. Are they both valid?
  2. Which one should be used and why?
web-tiki
  • 99,765
  • 32
  • 217
  • 249

3 Answers3

131

There is no difference between them.

If you don't specify a value for any of the half-dozen properties that background is a shorthand for, then it is set to its default value. none and transparent are the defaults.

One explicitly sets the background-image to none and implicitly sets the background-color to transparent. The other is the other way around.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 2
    @herman — No, they can't. This is the same as `background-color: transparent; background-image: none;`. A user stylesheet might override one or both of those values, but it will do so exactly as if `background-color: transparent; background-image: none;` had been written explicitly. – Quentin Mar 12 '15 at 12:34
  • So, you are saying the "initial" property value (which is used for unspecified properties) overrides even user agent stylesheets? Do you have any reference? – herman Mar 12 '15 at 12:43
  • User agent stylesheets implement the spec, which defines the initial property value of those properties as `transparent` and `none`. – Quentin Mar 12 '15 at 12:45
73

As aditional information on @Quentin answer, and as he rightly says, background CSS property itself, is a shorthand for:

background-color
background-image
background-repeat
background-attachment
background-position

That's mean, you can group all styles in one, like:

background: red url(../img.jpg) 0 0 no-repeat fixed;

This would be (in this example):

background-color: red;
background-image: url(../img.jpg);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 0 0;

So... when you set: background:none;
you are saying that all the background properties are set to none...
You are saying that background-image: none; and all the others to the initial state (as they are not being declared).
So, background:none; is:

background-color: initial;
background-image: none;
background-repeat: initial;
background-attachment: initial;
background-position: initial;

Now, when you define only the color (in your case transparent) then you are basically saying:

background-color: transparent;
background-image: initial;
background-repeat: initial;
background-attachment: initial;
background-position: initial;

I repeat, as @Quentin rightly says the default transparent and none values in this case are the same, so in your example and for your original question, No, there's no difference between them.

But!.. if you say background:none Vs background:red then yes... there's a big diference, as I say, the first would set all properties to none/default and the second one, will only change the color and remains the rest in his default state.

So in brief:

Short answer: No, there's no difference at all (in your example and orginal question)
Long answer: Yes, there's a big difference, but depends directly on the properties granted to attribute.


Upd1: Initial value (aka default)

Initial value the concatenation of the initial values of its longhand properties:

background-image: none
background-position: 0% 0%
background-size: auto auto
background-repeat: repeat
background-origin: padding-box
background-style: is itself a shorthand, its initial value is the concatenation of its own longhand properties
background-clip: border-box
background-color: transparent

See more background descriptions here


Upd2: Clarify better the background:none; specification.

Community
  • 1
  • 1
gmo
  • 8,860
  • 3
  • 40
  • 51
  • thx for answer what are the default values for background-image, background repeat...? do they depend on browser? If I understand your explanation, it's better to use background:none so none of the values are set to default? – web-tiki Dec 26 '13 at 12:31
  • Have you tried what you post?? Are all values setted to none? Check the `console` on this demo http://jsfiddle.net/dnzy2/6/ – DaniP Dec 26 '13 at 12:42
  • *when you set: background:none; you are saying that all the background properties are set to none...* Is not a way to say is wrong – DaniP Dec 26 '13 at 14:17
  • 2
    +1, This must be the accepted answer (if it's correct). – Pacerier Oct 08 '14 at 03:42
  • according to MDN, initial value of `background-image` is `none`, so `background-image: none` === `background-image: initial`, hence, `background: none` === `background: transparent`, I guess – Maxim Mazurok Feb 05 '21 at 03:16
12

To complement the other answers: if you want to reset all background properties to their initial value (which includes background-color: transparent and background-image: none) without explicitly specifying any value such as transparent or none, you can do so by writing:

background: initial;
herman
  • 11,740
  • 5
  • 47
  • 58
  • 6
    Be careful, IE does not support `initial`. see [Can I Use](http://caniuse.com/#search=initial) and [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/initial) – chharvey May 13 '16 at 04:14