Combining background color and gradient in one background CSS attribute

前端 未结 3 657
感情败类
感情败类 2021-01-25 01:23

I\'d like to combine a solid color and a gradient in a single background CSS attribute. Then, I want these two backgrounds to have separate size and position parame

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-25 02:17

    setting a background gradient will be overlayed over the background color. so if you want only 70% of the screen to be red, you'll need to include it in the gradient as you did.

    the reason why your property was not rendered in your js fiddle is because you had a comma separating the different parts of the backround shorthand property. so if you did want for overlay your gradient over the background you need to remove the comma:

    div {
    
    /* other properties of the div here */
    
    background: red linear-gradient(to right, transparent 0%, transparent 70%, rgb(0,0,0) 70%,rgb(255,255,255) 100%);
    
    }
    

    https://jsfiddle.net/0orbjebm/

    if you wanted the gradient tinted by the overlay, just add transparency. this is nice if you just want to great to have a gradient that you can overlay with transparency (i altered the sequence just for aesthetic sense

    div {
    
      /* other properties of the div here */
    
      background: red linear-gradient(to right, transparent 0%, transparent 70%, rgba(0,0,0,1) 100%);
    
    }
    

    https://jsfiddle.net/tootz4w8/

    in that above example i added a div and added an inline style property for the background color in the div itself and you can see we get nice effects

提交回复
热议问题