What is the difference between background and back

2019-01-04 16:29发布

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; }

16条回答
仙女界的扛把子
2楼-- · 2019-01-04 16:37

I've noticed when generating emails for Outlook...

/*works*/
background: gray;

/*does not work*/
background-color: gray;
查看更多
相关推荐>>
3楼-- · 2019-01-04 16:39

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.

查看更多
Animai°情兽
4楼-- · 2019-01-04 16:39

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") 
查看更多
贼婆χ
5楼-- · 2019-01-04 16:40

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.

查看更多
Evening l夕情丶
6楼-- · 2019-01-04 16:43

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 shortcut 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).

查看更多
Viruses.
7楼-- · 2019-01-04 16:43

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.

查看更多
登录 后发表回答