How to make half-square background in css

允我心安 提交于 2019-11-28 00:42:59

问题


Is it possible to make in pure css background for half square like on the image. I know that it can be done with an image as background, but I need to use 5 different colours, and maybe in future the colours can be changed.


回答1:


If your divs have fixed sizes, you can use borders to make two triangles as described in How do CSS triangles work?:

div{
  display:inline-block;
  border-top:100px solid red;
  border-right:100px solid grey;
  }
<div></div>

To make other sizes, colors, you need to tweak the border properties :

div {
  display: inline-block;
}
div:nth-child(1) {
  border-right: 100px solid red;
  border-top: 100px solid grey;
}
div:nth-child(2) {
  border-left: 100px solid red;
  border-top: 100px solid grey;
}
div:nth-child(3) {
  border-right: 50px solid red;
  border-top: 100px solid grey;
}
div:nth-child(4) {
  border-right: 100px solid red;
  border-bottom: 50px solid grey;
}
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>



回答2:


You can also try linear-gradient

div {
  width: 200px;
  height: 200px;
  background: rgba(248, 80, 50, 1);
  background:linear-gradient(to bottom right,grey 50%,red 50%);
}
<div></div>



回答3:


You can use transform if you don't need to worry about old browser support.

.container {
  position: relative;
  overflow: hidden;
  width: 100px;
}
.square {
  width: 100px;
  height: 100px;
  background-color: #E30606;
}
.overlay {
  width: 171px;
  height: 72px;
  background-color: #D0CBCB;
  -webkit-transform: rotate(225deg);
  -moz-transform: rotate(225deg);
  -ms-transform: rotate(225deg);
  transform: rotate(225deg);
}
<div class="container">
  <div class="square">
    <div class="overlay"></div>
  </div>
</div>

http://jsfiddle.net/hT9vP/13/



来源:https://stackoverflow.com/questions/29819502/how-to-make-half-square-background-in-css

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!