如何制作三角形顶部/底部?

时间:2015-11-17 19:52:37

标签: css triangular

我想在截面的底部做一​​个三角形的形状。 但它没有响应方式...当我调整窗口大小时,之间的空间:之前和之后:越来越大。

如何以另一种方式制作?

JSFIDDLE DEMO:http://jsfiddle.net/0y4L7hxh

<section id="s1">
     <h1>Hello World !</h1>
</section>
<section id="s2">
     <h1>Hello Dominik !</h1>
</section>

#s1 {
    background-color: green;
    padding: 160px 0px;
}
#s2 {
    background-color: #330099;
    padding: 160px 0px;
}
#s1:before {
    position: absolute;
    content:"";
    bottom: 40px;
    width: 51%;
    height: 150px;
    left: 0;
    background-color: green;
    transform: rotate(8deg);
    z-index: 9999;
}
#s1:after {
    position: absolute;
    content:"";
    bottom: 40px;
    width: 51%;
    height: 150px;
    right: 0px;
    background-color: green;
    transform: rotate(-8deg);
    z-index: 9999;
}

2 个答案:

答案 0 :(得分:0)

你可以使用渐变背景和背景大小:

&#13;
&#13;
section {
  background-color: yellow;
  margin: 0;
  padding: 2.5% 1em 0.01em;/* do not forget to give some top padding to free room for text/triangle */
}

section:after {
  content: '';
  padding-top: 5%;/* this means 5% of parent's width, tune this to your needs */
  position: absolute;
  left: 0;
  right: 0;
  background: linear-gradient(to bottom left, yellow 49%, transparent 51%) 0 0 no-repeat, linear-gradient(to bottom right, yellow 49%, transparent 51%) 100% 0 no-repeat;
  background-size: 50% 50%;
}

section:nth-child(even) {
  background: purple;
}

section:nth-child(even):after {
  background: linear-gradient(to bottom left, purple 49%, transparent 51%) 0 0 no-repeat, linear-gradient(to bottom right, purple 49%, transparent 51%) 100% 0 no-repeat;
  background-size: 50% 50%;
}
&#13;
<section
  <h1>Hello World !</h1>
  <h1>Hello World !</h1>
  <h1>Hello World !</h1>
  <h1>Hello World !</h1>
</section>

<section>
  <h1>Hello Dominik !</h1>
  <h1>Hello Dominik !</h1>
  <h1>Hello Dominik !</h1>
  <h1>Hello Dominik !</h1>
  <h1>Hello Dominik !</h1>
</section>
<section>
  <h1>Hello World !</h1>
</section>

<section>
  <h1>Hello Dominik !</h1>
</section>
&#13;
&#13;
&#13;

这里有一个代码笔,背景颜色不同,平面三角形http://codepen.io/anon/pen/yYwLeR与它一起播放并调整padding-top值以调整它的高度

答案 1 :(得分:0)

也许不使用两个矩形,你可以使用两个三角形(jsfiddle demo:http://jsfiddle.net/atLuqy7f/

#s1{
  background-color: green;
  padding: 160px 0px;
  position:relative;
}

#s1:before{
  position: absolute;
  bottom:-40px;
  left:50%;
  transform:translateX(-50%);
  content: "";
  width: 0; 
  height: 0; 
  border-left: 150px solid transparent;
  border-right: 150px solid transparent;
  border-top: 40px solid green;
  z-index: 9999;
}

#s1:after{
   position: absolute;
  bottom:-40px;
  left:50%;
  transform:translateX(-50%);
  content: "";
  width: 0; 
  height: 0; 
  border-left: 20px solid transparent;
  border-right: 20px solid transparent;
  border-bottom: 40px solid #330099;
  z-index: 9999;
}

请记住使用position属性作为绝对定位元素的容器。