0

I want to diagonally split the background into two colors where one is white color. And I want the other one to be a gradient that goes from red to blue.

Here we have red split with white background (source):

body {
  height:100vh;
  width:100vw;
  background:linear-gradient(160deg, red, red 60%, white 60%, white);
}

Is it possible to make the red part a gradient, where it goes from red to blue, something like:

enter image description here

So basically, a gradient within a gradient.

1
  • 1
    Given the complexity of this question, I will bounty it 50 points when eligible. Commented Jul 28, 2017 at 10:14

4 Answers 4

3

Yes you can.Use the given css:

  height:100vh;
  width:100vw;
  background:linear-gradient(90deg, red , purple 60%, blue 100%);
  clip-path: polygon(0% 0%,100% 0%,100% 30%,0% 100%);

At first fill it with the gradient and then clip it.

Sign up to request clarification or add additional context in comments.

4 Comments

Limited support for clip-path unfortunately, but I like this one a lot.
You can upvote and also accept it if you liked it :)
You will get an upvote ;) but can't accept it because it isn't supported on all modern browsers. Ping me in the future when it is!
chrome 55 fully supports the property.
2

I have managed to do this with a pseudo element on the body.

See it in action here: https://jsfiddle.net/q8az3u0g/

body:before {
    content: '';
    display: block;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 0 100vh 100vw;
    border-color: transparent transparent #fff transparent;
}

Basically the body has a horizontal gradient and then the pseudo element becomes a white triangle overlay.

Comments

2

-- Update --

You can do this with a single element by adjusting the linear-gradient.

.shape {
  width: 500px;
  height: 300px;
  overflow: hidden;
  position: relative;
}

.shape:after {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  background: red;
  background: linear-gradient(110deg, red, violet, blue);
  transform-origin: 0 100%;
  transform: rotate(-20deg) scale(1.2,1.2);
  bottom: 0;
  left: 0;
}
<div class="shape"></div>

Previous Answer

First a slanted shape is created by rotating a div and then rotating its child element in opposite direction. (overflow:hidden will cut off the remaining parts) On the child element gradient is applied.

.shape {
  width: 500px;
  height: 300px;
  overflow: hidden;
}

.rotated {
  width: 100%;
  height: 100%;
  transform-origin: 0 100%;
  transform: scale(1.5, 1.5) rotate(-20deg);
  position: relative;
  overflow: hidden;
}

.coloured {
  position: absolute;
  width: 100%;
  height: 100%;
  background: red;
  background: linear-gradient(to right, red, violet, blue);
  transform-origin: 0 0;
  transform: rotate(20deg);
  bottom: 0;
  left: 0;
}
<div class="shape">
  <div class="rotated">
    <div class="coloured"></div>
  </div>
</div>

You can do it with two elements if required ( One if you can use clip-path,it has low browser support)

.shape {
  width: 500px;
  height: 300px;
  overflow: hidden;
}

.child {
  width: 100%;
  height: 100%;
  transform-origin: 0 100%;
  transform: scale(1.5, 1.5) rotate(-20deg);
  position: relative;
  overflow: hidden;
}

.child:after {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  background: red;
  background: linear-gradient(to right, red, violet, blue);
  transform-origin: 0 0;
  transform: rotate(20deg);
  bottom: 0;
  left: 0;
}
<div class="shape">
  <div class="child"></div>
</div>

This is how it works :

enter image description here

Comments

1

You can also lay 2 gradients on top of each others (keywords i react on :gradient within gradient)

body {
margin:0;
  min-height: 100vh;
  background: 
  linear-gradient(to bottom right, transparent 50%, white 50.25%), /* update direction and start/stop value to your needs */
  linear-gradient(90deg, red, blue) white;
  background-size:100% auto;
  transition:0.5s
  }
  
  /* extra test*/
  body:hover {
  background-size: 125% auto;/* add some extra tunning to deg direction and size ?*/
}

5 Comments

This is pretty next level. I didn't realise this was possible. Is double gradients compatible with all modern browsers?
@HenrikPetterson It is at least compatible with any browser that supports multiple backgrounds. each gradient is treated as an image . developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient
This is exactly my question. Do you by any chance know if all modern browsers support multiple background?
If gradient is supported, then multiple background is, it was supported before gradient was finalized :) caniuse.com/#search=linear-gradient caniuse.com/#feat=multibackgrounds
This is awesome. I will put this code to test this week. Thank you for posting or even finding this question. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.