Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring all CSS background properties in one line — especially background-size

Tags:

html

css

safari

So here's my code:

background: url(images/my-image.png) no-repeat center center / cover;

This works fine on Chrome and Firefox but not on Safari for some reason?

I used to declare my background-size on it's own line but as I understand it it should be possible to declare all properties in one line using a forward slash?

Any help would be much appreciated. Thanks.

like image 438
Ben Clarke Avatar asked Jul 22 '15 08:07

Ben Clarke


People also ask

Can you set the background properties in one declaration?

To reduce the length of the CSS code we can declare background properties in one line through “Shorthand Property”. Through this property we can define different properties of background in a single line instead of using background-color, background-image and other properties in multiple lines.

What is CSS background-size?

The background-size CSS property sets the size of the element's background image. The image can be left to its natural size, stretched, or constrained to fit the available space.

Which CSS property can be used to spread an image all over the background of a Web page?

The Modern Way When you work with background images, you may want an image to stretch to fit the page despite the wide range of devices and screen sizes. 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.

Can we specify the background-size in percentage True or false?

As per the W3C Specs: A percentage is relative to the background positioning area. and background positioning area is one of either border-box or padding-box or content-box based on the background-origin property. Here you haven't specified any value explicitly for this and so its default value of padding-box is used.


2 Answers

As one line short hand code seems do unknown for safari browsers meaning of cover:

background: url(images/my-image.png) no-repeat center center / cover;

I faced the same issue before. And the following worked for all browsers:

background: url(images/my-image.png) no-repeat;
background-position: center;
background-size: cover;/*now this is known for the safari*/
like image 92
Bhojendra Rauniyar Avatar answered Sep 22 '22 13:09

Bhojendra Rauniyar


background property has following attributes.

  • url("image path")
  • bg-color e.g transparent or any color
  • bg-img-repeat e.g repeat or no=repeat
  • bg-image-postion vertical and horizontal e.g center center
  • bg-img-size e.g cover, contain, 100%, 800px, or 200px 100px (width and height) etc

We will write like this

background: url("images/my-image.png") transparent no-repeat bottom center  / cover;
background-size: cover; /*for safari we can add this sperately*/
like image 30
Muhammad Bilawal Avatar answered Sep 22 '22 13:09

Muhammad Bilawal