【问题标题】:How to make a triangular top/bottom of section?如何制作截面的三角形顶部/底部?
【发布时间】:2015-11-17 19:52:37
【问题描述】:

我想在部分底部制作一个三角形。 但它不是响应方式...当我调整窗口大小时,: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;
}

【问题讨论】:

标签: css triangular


【解决方案1】:

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

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 值以将其调整为更高

【讨论】:

  • 您的问题很好,但我有两个问题:如何使边界更清晰? prntscr.com/93x2lu 2) 如何为这张图片制作三角形? prntscr.com/93x3dc
  • @user3313798 1) 将 2 种渐变颜色设置为不同但接近的值以模糊边缘,但先做 2 :) 2) 增加 padding-tops 以达到所写的不太平坦的三角形。代码笔站在那里实时使用它并帮助您了解它是如何工作的
  • 嗯,但是这个紫色部分必须是紫色的。我包括在这个白色渐变的顶部。我应该改变什么?
  • @user3313798 这里是codepen.io/anon/pen/EVMxrK 的变体,对于颜色,在代码中搜索它们并根据您的需要进行更新。如果您喜欢,请花点时间熟悉这段代码,让它成为您的;),如果您认为答案有用,您也可以投票...只是说
  • 谢谢,但这并不理想 :( prntscr.com/93xe58 但我知道这是一种解决方案.. :(
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    相关资源
    最近更新 更多