Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background linear gradient not working in safari

Tags:

css

I am trying to set a linear gradient background over an image and my code works in Chrome but not in Safari. Here is a full example of my code:

HTML:

<div>
  <img src="./assets/51a-front-img.png" draggable="false"/>
</div>

CSS:

div:after{
  content: '\A';
  position: absolute;
  width: 100%;
  height: 100%;
  top:0;
  background: rgba(0,0,0,0.5);
  background: -moz-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.5)), color-stop(100%,rgba(0,0,0,0.7))); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* IE10+ */
  background: linear-gradient(to bottom, rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* W3C */
}

img {
  position: relative;
  width: 100%;
}
like image 650
Ruth Avatar asked Jul 20 '16 20:07

Ruth


People also ask

Does Safari support linear gradient?

Safari supports two types of CSS gradients: linear and radial.

How do you use a linear gradient for background color?

The linear-gradient() function sets a linear gradient as the background image. To create a linear gradient you must define at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.

What is Webkit gradient?

First, -webkit-gradient uses a two-point syntax that lets you explicitly state where a linear gradient starts and ends. linear-gradient does away with this in favor of convenient box-filling behavior. If you really want the gradient to stop before the edges of the box, you can do so via color stop placement.

Can I use background image gradient?

Gradients can be used anywhere you would use an <image> , such as in backgrounds.


1 Answers

The div:after needs to be positioned on the left edge (which chrome does by default). change your css to:

div:after{
  content: '\A';
  position: absolute;
  width: 100%;
  height: 100%;
  top:0;
  left:0;
  background: rgba(0,0,0,0.5);
  background: -moz-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.5)), color-stop(100%,rgba(0,0,0,0.7))); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* IE10+ */
  background: linear-gradient(to bottom, rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* W3C */
}

img {
  position: relative;
  width: 100%;
}
like image 132
Simon Kraus Avatar answered Oct 21 '22 22:10

Simon Kraus