lx4r
lx4r

Reputation: 133

Cascading divs with triangle bottom borders

This is a follow-up to his question: Center triangle at bottom of div full width responsively

Again I'm stuck with my CSS for a project involving divs with triangle borders at the bottom:

I want a row of cascading divs to look like this (lower tringle colored red for demonstration purposes):

enter image description here

My code now looks like this:

html, body {
    padding: 0; margin: 0;
    color: white;
}
.top {
    background-color: #282C34;
    height: 500px;
    width: 100%;
    position: relative;
}
.bottom {
    background-color: #3B3E48;
    height: 500px;
    width: 100%;
}
.triangle {
    border-left: 50vw solid transparent;
    border-right: 50vw solid transparent;
    width: 0;
    height: 0;
    bottom: -40px;
    content: "";
    display: block;
    position: absolute;
    overflow: hidden;
    left:0;right:0;
    margin:auto;
}
.upperTriangle {
    border-top: 40px solid #282C34;
}
.lowerTriangle {
    border-top: 40px solid red;
}
<div class="top">
    <div class="triangle upperTriangle"></div>
</div>
<div class="bottom">
    <div class="triangle lowerTriangle"></div>
</div>
<div class="top">
    <div class="triangle"></div>
</div>

Code on JSFiddle: http://jsfiddle.net/rndwz681/

My problems:

Thanks a lot in advance for the help.

Upvotes: 3

Views: 571

Answers (2)

Bmd
Bmd

Reputation: 1318

By adding position:relative; to your .bottom class and adding z-index:100; to your .triangle class I was able to get your triangles to appear the way you want them to.

See my fiddle: http://jsfiddle.net/rndwz681/1/

z-index sets the "layer" that an object appears on (higher number = closer to the user). It can only be applied to 'positioned' elements, but your absolute-positioned triangles qualify.

Upvotes: 1

halfzebra
halfzebra

Reputation: 6797

Powered by CSS triangle generator

.container {
  width: 100%;
  overflow: hidden;
}
.block {
  width: 100%;
  height: 200px;
}
.block--arrow {
  position: relative;
}
.block--arrow:before {
  display: block;
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  margin-left: -350px;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 100px 350px 0 350px;
}
.grey {
  background: #626262;
}
.light-grey {
  background: #999999;
}
.light-grey:before {
  border-color: #626262 transparent transparent transparent;
}
.black {
  background: #000000;
}
.black:before {
  border-color: #999999 transparent transparent transparent;
}
<div class="container">
  <div class="grey block"></div>
  <div class="light-grey block block--arrow"></div>
  <div class="black block block--arrow"></div>
</div>

Upvotes: 2

Related Questions