如何制作一个超过4个角的div

时间:2016-10-06 06:35:10

标签: html css html5 css3 css-shapes

我必须仅使用HTML和CSS制作div,但不要使用任何超过4个角的背景图片。

i want t make exactly this thing using html and css

我该怎么做?

8 个答案:

答案 0 :(得分:17)

你可以使用pseudo-element和一些css形状技巧来实现这一目标。



.folder {
  width: 190px;
  height: 110px;
  background: #888;
  position: relative;
  overflow: hidden;
}

.folder:after {
  content: "";
  width: 100px;
  border: 15px solid transparent;
  position: absolute;
  right: -15px;
  border-top-color: #fff;
  top:0;
}

<div class="folder"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:10)

有两个代码示例: CSS (+动画)和 SVG

动画

&#13;
&#13;
* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #2196f3;
}

.page {
  height: 100%;
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: center;
  align-items: center;
  -moz-box-align: center;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -moz-box-pack: center;
  -ms-flex-pack: center;
}

.folder {
  background-color: #d3eafd;
  position: relative;
  width: 92px;
  height: 64px;
  display: block;
  border-top-right-radius: 8px;
  border-bottom-right-radius: 8px;
  border-bottom-left-radius: 8px;
}

.folder-tab {
  position: absolute;
  height: 10px;
  left: 0;
  bottom: 100%;
  display: block;
  width: 40%;
  border-top-left-radius: 8px;
  background-color: inherit;
}
.folder-tab:after {
  content: '';
  position: absolute;
  display: block;
  top: 0;
  left: calc(100% - 10px);
  border-bottom: 10px solid #d3eafd;
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;
}

.folder-icn {
  padding-top: 12px;
  width: 100%;
  height: 100%;
  display: block;
}

.downloading {
  width: 30px;
  height: 32px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}

.custom-arrow {
  width: 14px;
  height: 14px;
  position: absolute;
  top: 0;
  left: 50%;
  margin-left: -7px;
  background-color: #fff;
  -webkit-animation-name: downloading;
  -webkit-animation-duration: 1.5s;
  -webkit-animation-iteration-count: infinite;
  animation-name: downloading;
  animation-duration: 1.5s;
  animation-iteration-count: infinite;
}
.custom-arrow:after {
  content: '';
  position: absolute;
  display: block;
  top: 100%;
  left: -9px;
  border-top: 15px solid #fff;
  border-left: 16px solid transparent;
  border-right: 16px solid transparent;
}

.bar {
  width: 30px;
  height: 4px;
  background-color: #fff;
  margin: 0 auto;
}

@-webkit-keyframes downloading {
  0% {
    top: 0;
    opacity: 1;
  }
  50% {
    top: 110%;
    opacity: 0;
  }
  52% {
    top: -110%;
    opacity: 0;
  }
  100% {
    top: 0;
    opacity: 1;
  }
}
@keyframes downloading {
  0% {
    top: 0;
    opacity: 1;
  }
  50% {
    top: 110%;
    opacity: 0;
  }
  52% {
    top: -110%;
    opacity: 0;
  }
  100% {
    top: 0;
    opacity: 1;
  }
}
&#13;
<div class="page">
  
  <div class="folder">
    <span class="folder-tab"></span>
    <div class="folder-icn">
      <div class="downloading">
        <span class="custom-arrow"></span>
      </div>
      <div class="bar"></div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

<强> SVG

&#13;
&#13;
<!DOCTYPE html>
<html>
<body>

<svg height="32px" version="1.1" viewBox="0 0 32 32" width="32px" xmlns="http://www.w3.org/2000/svg" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns" xmlns:xlink="http://www.w3.org/1999/xlink"><title/><desc/><defs/><g fill="none" fill-rule="evenodd" id="Page-1" stroke="none" stroke-width="1"><g fill="#157EFB" id="icon-94-folder"><path d="M17,11 L15,7 L4.00276013,7 C2.89666625,7 2,7.88967395 2,8.991155 L2,27.008845 C2,28.1085295 2.89971268,29 3.99328744,29 L29.0067126,29 C30.1075748,29 31,28.1073772 31,27.0049107 L31,12.9950893 C31,11.8932319 30.1029399,11 28.9941413,11 L17,11 Z" id="folder"/></g></g>
  </svg>

</body>
</html>
&#13;
&#13;
&#13;

有用的链接:

答案 2 :(得分:5)

div {
	width: 280px;
	height: 280px;
	background: #1e90ff;
	-webkit-clip-path: polygon(48% 13%, 100% 13%, 100% 60%, 100% 100%, 0 100%, 0 0, 29% 0);
clip-path: polygon(48% 13%, 100% 13%, 100% 60%, 100% 100%, 0 100%, 0 0, 29% 0);
}

/* Center the demo */
html, body { height: 100%; }
body {
	display: flex;
	justify-content: center;
	align-items: center;
}
<div></div>

答案 3 :(得分:3)

如果只有一个块级元素,您可以设置:before伪元素的样式,以在包含<div>的上方创建倾斜选项卡。

div {
  margin: 40px;
  width: 150px;
  height: 80px;
  background: red;
  position: relative;
  padding: 10px;
  color: #fff;
}

div:before {
  content:"";
  position: absolute;
  left: 0;
  top: -20px;
  width: 70px;
  height: 0;
  border-bottom: 20px solid red;
  border-right: 20px solid transparent;
}
<div>content</div>

N.b。:与使用clip-path解决方案相比,这应该对旧浏览器(和IE)有更好的支持。

答案 4 :(得分:1)

使用HTML5的“画布”来做另一种方式:

<div>
  <canvas id="cnv" height="200" width="400"></canvas>
  <script>
    var canvas = document.getElementById('cnv');
    if (canvas.getContext) {
      var ctx = canvas.getContext('2d');
      ctx.beginPath();
      ctx.moveTo(0, 0);
      ctx.lineTo(100, 0);
      ctx.lineTo(130, 25);
      ctx.lineTo(200, 25);
      ctx.lineTo(200, 125);
      ctx.lineTo(0, 125);
      ctx.closePath();
      ctx.fillStyle = "gray";
      ctx.fill();
    }
  </script>
</div>

答案 5 :(得分:0)

如果您可以插入代码,则可以使用SVG图形。

如果没有,您可以绘制矢量图形css clip-path作为上面的答案。 有一些生成器,here是我发现的

另一种选择是使用至少3个div,在其中一个中使用css transform倾斜一个,并使用相对os绝对定位来定位每个div。

答案 6 :(得分:0)

您可以使用单个元素和两个渐变(一个渐变为矩形,另一个渐变为制表符)来实现此目的:

div {
  width: 280px;
  height: 200px;
  background: linear-gradient(to bottom, transparent 31px, #656d78 31px),
    linear-gradient(-135deg, transparent 32%, #656d78 32%);
}
<div></div>

这也可以通过使用伪元素的单个渐变(对于制表符)来实现:

div {
  width: 280px;
  height: 169px;
  background-color: #656d78;
  margin-top: 39px;
  position: relative;
}

div:after {
  content: "";
  position: absolute;
  top: -31px;
  left: 0;
  width: 100%;
  height: 31px;
  background: linear-gradient(-135deg, transparent 50%, #656d78 50%);
}
<div></div>

答案 7 :(得分:0)

您可以使用CSS制作多边形的div。

.myDiv {
  -webkit-clip-path: polygon(48% 16%, 100% 16%, 100% 100%, 0% 100%, 0 0, 32% 0);
  clip-path: polygon(48% 16%, 100% 16%, 100% 100%, 0% 100%, 0 0, 32% 0);
}

或者您可以使用此网站创建任何类型的多边形(在线)

https://www.cssportal.com/css-clip-path-generator/