1

我想在截面底部做一个三角形。但它不是响应方式......当我调整窗口大小时,:before 和 :after 之间的空间越来越大。

如何用另一种方式制作它?

JSFIDDLE 演示: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;
}
4

2 回答 2

0

也许您可以使用两个三角形而不是使用两个矩形(jsfiddle 演示: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;
}

并且记住对绝对定位元素的容器使用位置属性。

于 2015-11-17T20:32:36.430 回答
0

你可以使用渐变背景和背景尺寸:

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%;
}
<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>

这里有一个与背景和平面三角形颜色不同的代码笔http://codepen.io/anon/pen/yYwLeR使用它并调整 padding-top 值以将其调整得更高

于 2015-11-17T20:26:11.663 回答