One cool thing about CSS custom properties is that they can be a part of a value. Let’s say you’re using multiple backgrounds to pull off a a design. Each background will have its own color, image, repeat, position, etc. It can be verbose!
body { background-position: top 10px left 10px, top 10px right 10px, bottom 10px right 10px, bottom 10px left 10px; background-repeat: no-repeat; background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/angles-top-left.svg), url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/angles-top-right.svg), url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/angles-bottom-right.svg), url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/angles-bottom-left.svg); }
You want to add a fifth in a media query:
您想在媒体查询中添加第五个:
1 2 3 4 5
@media (min-width:1500px) { body { /* REPEAT all existing backgrounds, then add a fifth. */ } }
That’s going to be super verbose! You’ll have to repeat each of those four images again, then add the fifth. Lots of duplication there.
这将是超级冗长的!你将不得不再次重复这四个图像中的每一个,然后添加第五个。那里有很多重复。
One possibility is to create a variable for the base set, then add the fifth much more cleanly:
body { --baseBackgrounds: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/angles-top-left.svg), url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/angles-top-right.svg), url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/angles-bottom-right.svg), url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/angles-bottom-left.svg);
background-position: top 10px left 10px, top 10px right 10px, bottom 10px right 10px, bottom 10px left 10px; background-repeat: no-repeat; background-image: var(--baseBackgrounds); } @media (min-width:1500px) { body { background-image: var(--baseBackgrounds), url(added-fifth-background.svg); } }
But, it’s really up to you. It might make more sense and be easier manage if you made each background image into a variable, and then pieced them together as needed.
Dynamically changing just the part of a value is a huge strength of CSS custom properties!
动态更改值的一部分是CSS自定义属性的巨大优势!
Note, too, that with backgrounds, it might be best to include the entire shorthand as the variable. That way, it’s much easier to piece everything together at once, rather than needing something like…