将CSS背景从中心旋转到角落

4
我希望在CSS中实现这个效果,适用于所有屏幕尺寸: example 从屏幕左侧到中间的背景应该是蓝色的,从上中部到右下角的背景应该是白色的。 这是我已经得到的:
<style>
.wrapper {
    position: fixed;
    z-index: 1;
    width: 100%;
    height: 100%;
    background: #297fca;
}
.right {
    position: fixed;
    z-index: 2;
    top: -70%;
    right: -50%;
    background: #fff;
    width: 100%;
    height: 100%;
    transform: translateY(50%) rotate(45deg);
  }
</style>
...
 <div class="wrapper">
    <div class="right">
    </div>

  </div>

这在某些屏幕尺寸上有效,但不是通用解决方案。 我正在寻找仅使用CSS的解决方案。如果这不可能,SVG也可以。 提前感谢您。
2个回答

6
你可以使用linear-gradient轻松实现此目的。你需要两个,一个将创建一个正方形填充前50%,另一个将创建三角形形状填充剩余的50%。

body {
  margin:0;
  height:100vh;
  background-image:
   linear-gradient(#297fca,#297fca),
   linear-gradient(to bottom left,transparent 49.8%,#297fca 50%);
  background-repeat:no-repeat;
  background-size:50.1% 100%; /* both gradient will fill 50% width and 100% height*/
  background-position:
    left, /* The first one placed on the left*/
    right /* The second one placed on the right*/
}

如果您不想要透明度,可以按以下方式操作:

body {
  margin:0;
  height:100vh;
  background:
   linear-gradient(to top right,transparent 49.8%,#fff 50%) 
    right -1px top 0
    /50% 100% 
    no-repeat,
   #297fca;
}


2
您可以使用 CSS 的 clip-path 属性并给它必要的填充规则。尝试下面的代码片段:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  height: 100vh;
  background: #297fca;
  clip-path: polygon(0 0, 50% 0, 100% 100%, 0 100%);
}
<div class="container"></div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接