0

I have a container element that I am trying to figure out if I can shape it to have two 45 degree angles. Like this:

enter image description here

It is simply a rectangle now:

enter image description here

<div id="home-img-text">
        <div id="home-img-text-container">
            <div id="home-img-text-description">WRECKING <br>& DEMOLITION <br> DONE RIGHT.</div>
        </div>
    </div>
#home-img-text-container {
    background: rgba(0,0,0,.8);
    padding: 30px 20px;
}
#home-img-text-description {
    color: #FFF;
    font-size: 2.5em;
    line-height: 1.4em;
}

enter image description here

How would I go about doing this?

UPDATE:

.home-img-text {
    position: absolute;
    left: 10%;
    top: 25%;
}
#home-img-text-container1, #home-img-text-container2 {
    position: relative;
    margin-bottom: 20px;
    background: rgba(0,0,0,.8);
    opacity: 0;
    transition:1s; -webkit-transition:1s;
    overflow: hidden;
}
#home-img-text-container1.fadeDisplay {
    opacity: 1;
    transform: translateX(30px); -webkit-transform: translateX(30px);
}
#home-img-text-container2.fadeDisplay {
    opacity: 1;
    transform: translateX(30px); -webkit-transform: translateX(30px);
}

#home-img-text-description {
    position: relative;
    margin: 40px 0px;  
    padding: 30px 20px;
    color: #FFF;
    font-size: 2.5em;
    line-height: 1.4em;
}
#home-img-text-description2 {
    color: #FFF;
    font-size: 2em;
    line-height: 1.4em;
    padding: 30px 20px;
}
/*----Adding this ---*/

#home-img-text-description:before {
  position: absolute;
  content: '';
  height: 40px;
  width: 100%;
  left: 0px;
  background: inherit;
}

#home-img-text-description:before {
  top: -40px;
  transform: skewX(45deg);
  transform-origin: right bottom;
}
Becky
  • 2,283
  • 2
  • 23
  • 50

4 Answers4

5

Using two pseudo-elements that are skewed in opposite directions (by 45 deg) would also be a good option for producing this shape. The advantage of using this option is that it produces a transparent cut on the right-top and right-bottom unlike the shapes created using the border method.

I wouldn't recommend clip-path (with or without SVG) because of the lack of support in IE.

#home-img-text{
  overflow: hidden;
}  
#home-img-text-container {
  position: relative;
  background: rgba(0, 0, 0, .8);
  padding: 30px 20px;
  margin: 40px 0px;
}
#home-img-text-container:after,
#home-img-text-container:before {
  position: absolute;
  content: '';
  height: 40px;
  width: 100%;
  left: 0px;
  background: inherit;
}
#home-img-text-container:before {
  top: -40px;
  transform: skewX(45deg);
  transform-origin: right bottom;
}
#home-img-text-container:after {
  bottom: -40px;
  transform: skewX(-45deg);
  transform-origin: right top;
}
#home-img-text-description {
  color: #FFF;
  font-size: 2.5em;
  line-height: 1.4em;
}
body {
  background-image: radial-gradient(circle at center, chocolate, sandybrown);
  min-height: 100vh;
}
<div id="home-img-text">
  <div id="home-img-text-container">
    <div id="home-img-text-description">WRECKING
      <br>& DEMOLITION
      <br>DONE RIGHT.</div>
  </div>
</div>

Here is a version with borders on all the sides of the shape:

#home-img-text{
  border-left: 2px solid;
  overflow: hidden;
}  
#home-img-text-container {
  position: relative;
  padding: 30px 20px;
  margin: 40px 0px;
  background: rgba(0, 0, 0, .8);
  border-right: 2px solid;
}
#home-img-text-container:after,
#home-img-text-container:before {
  position: absolute;
  content: '';
  height: 38px;
  width: 100%;
  left: 0px;
  background: inherit;
  border: 2px solid;
}
#home-img-text-container:before {
  top: -40px; /* -(height + border-top) */
  border-width: 2px 3px 0px 0px;
  transform: skewX(45deg);
  transform-origin: right bottom;
}
#home-img-text-container:after {
  bottom: -40px; /* -(height + border-bottom) */
  border-width: 0px 3px 2px 0px;
  transform: skewX(-45deg);
  transform-origin: right top;
}
#home-img-text-description {
  color: #FFF;
  font-size: 2.5em;
  line-height: 1.4em;
}

body {
  background-image: radial-gradient(circle at center, chocolate, sandybrown);
  min-height: 100vh;
}
<div id="home-img-text">
  <div id="home-img-text-container">
    <div id="home-img-text-description">WRECKING
      <br>& DEMOLITION
      <br>DONE RIGHT.</div>
  </div>
</div>

If you need to add an image (either as background or not), it will need an extra couple of elements and the element to have a fixed height. but as mentioned earlier this approach has its advantages even if it needs extra elements.

#home-img-text {
  position: relative;
  padding: 40px 0px; /* top/bottom padding equal to height of the two skewed elements, don't set left/right padding  */
  overflow: hidden;
}
#home-img-text-container {
  height: 170px; /* height of image - 2 * height of skewed element */
  padding: 0px 20px; /* don't set any top/bottom padding */
  background: linear-gradient(rgba(0, 0, 0, .8), rgba(0, 0, 0, .8)),url(http://lorempixel.com/800/250/nature/1);
  background-position: 0px 0px, 0px -40px; /* 0px 0px, 0px -(height of skewed element) */
}
#home-img-top,
#home-img-bottom {
  position: absolute;
  content: '';
  height: 40px;
  width: 100%;
  left: 0px;
  overflow: hidden;  
}
#home-img-top {
  top: 0px;
  transform: skewX(45deg);
  transform-origin: right bottom;
}
#home-img-bottom {
  bottom: 0px;
  transform: skewX(-45deg);
  transform-origin: right top;
}
#home-img-top:before,
#home-img-bottom:before {
  position: absolute;
  content: '';
  height: 100%;
  width: 100%;
  left: 0px;
  background: linear-gradient(rgba(0, 0, 0, .8), rgba(0, 0, 0, .8)), url(http://lorempixel.com/800/250/nature/1);
}
#home-img-top:before {
  top: 0px;
  background-position: 0px 0px,0px 0px;
  transform: skewX(-45deg);
  transform-origin: right bottom;
}
#home-img-bottom:before {
  bottom: 0px;
  background-position: 0px 0px, 0px -210px; /* 0px -(height of image - height of skewed element) */
  transform: skewX(45deg);
  transform-origin: right top;
}
#home-img-text-description {
  color: #FFF;
  font-size: 2.5em;
  line-height: 1.4em;
}
body {
  background-image: radial-gradient(circle at center, chocolate, sandybrown);
  min-height: 100vh;
}
<div id="home-img-text">
  <div id="home-img-top"></div>
  <div id="home-img-text-container">
    <div id="home-img-text-description">WRECKING
      <br>& DEMOLITION
      <br>DONE RIGHT.</div>
  </div>
  <div id="home-img-bottom"></div>
</div>
Harry
  • 87,580
  • 25
  • 202
  • 214
  • Sorry I just got to this now. I haven't been on. I am now trying to do only the top right corner and I cannot figure this out. Here is what I have: https://jsfiddle.net/2gq71f10/ – Becky May 12 '16 at 04:55
  • @Becky: The `overflow: hidden` on parent shouldn't be commented. That is the one of the most critical parts of this approach :) – Harry May 12 '16 at 05:00
  • When I take out the comment on my actual page, I get something completely different. I added a pic to my question. – Becky May 12 '16 at 05:03
  • Interesting @Becky. Can you update the fiddle with your actual code so that I can see the issue and fix it? Right now, it is tough to see what could be the cause. [This](https://jsfiddle.net/2gq71f10/2/) is what I see. – Harry May 12 '16 at 05:04
  • Code added to my question. – Becky May 12 '16 at 05:07
  • @Becky: That is because of the `translate` that you have within `.fadeDisplay`. Can you change your CSS to be like in [this fiddle](https://jsfiddle.net/2gq71f10/3/)? – Harry May 12 '16 at 05:22
  • I tried that and I am not even getting any kind of angle anymore, it is just a box like before. I updated my code again. – Becky May 12 '16 at 05:35
  • 1
    @Becky: Don't put the `background: rgba(0,0,0,.8)` on the `#home-img-text-container1`. Put it on the `#home-img-text-description` like in the Fiddle :) – Harry May 12 '16 at 05:38
  • 1
    Thanks for the help Harry! – Becky May 14 '16 at 02:03
  • @Becky: I cannot access your website as it is blocked at office. It would be better if you can create a fiddle (and also a new question as others can also help). – Harry Jun 02 '16 at 04:26
  • Thanks. Here it is.. http://stackoverflow.com/questions/37583981/containers-have-same-undesired-width – Becky Jun 02 '16 at 06:14
1

#home-img-text-container {
  background: rgba(0,0,0,.8);
  padding: 30px 20px;
  position: relative;
}

#home-img-text-container:before,
#home-img-text-container:after {
  position: absolute;
  content: '';
  display: block;  
  right: -40px;
  border: 40px solid transparent;
  border-right: 40px solid transparent;  
}

#home-img-text-container:before {
  top: 0;
  border-top: 40px solid white;  
}

#home-img-text-container:after {
  bottom: 0;
  border-bottom: 40px solid white;
}

#home-img-text-description {
  color: #FFF;
  font-size: 2.5em;
  line-height: 1.4em;
}
<div id="home-img-text">
  <div id="home-img-text-container">
    <div id="home-img-text-description">WRECKING <br>& DEMOLITION <br> DONE RIGHT.</div>
  </div>
</div>
Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61
1

you can also use gradients here combined with background-size and calc() :

only borders:

#home-img-text-container {
  margin:1em;
  display:inline-block;
    background:
      linear-gradient(0deg, rgba(0,0,0,0.9), rgba(0,0,0,0.9)) top left no-repeat ,
      linear-gradient(0deg, rgba(0,0,0,0.9), rgba(0,0,0,0.9)) bottom left no-repeat ,
      linear-gradient(90deg, rgba(0,0,0,0.9), rgba(0,0,0,0.9)) top left no-repeat,
      linear-gradient(0deg, rgba(0,0,0,0.9), rgba(0,0,0,0.9)) center right no-repeat,
      linear-gradient(-45deg, transparent 1.5em, rgba(0,0,0,0.8) 1.5em, rgba(0,0,0,0.9) calc(1.5em + 3px), transparent  calc(1.5em + 3px)) bottom left no-repeat,
      linear-gradient(-135deg, transparent 1.5em, rgba(0,0,0,0.8) 1.5em, rgba(0,0,0,0.9) calc(1.5em + 3px), transparent  calc(1.5em + 3px)) top left no-repeat
      ;
  background-size: 
    calc(100% - 2.25em) 3px ,
    calc(100% - 2.25em)  3px , 
    3px 100%, 
    3px calc(100% - 4.25em), 
    100% 50%, 
    100% 50%
    ;
    padding: 30px 20px;
}
#home-img-text-description {
    font-size: 2.5em;
    line-height: 1.4em;
}

html {
  min-height:100%;
  background:linear-gradient(to bottom right, tomato, gold);
  }
<div id="home-img-text">
  <div id="home-img-text-container">
    <div id="home-img-text-description">WRECKING
      <br>& DEMOLITION
      <br>DONE RIGHT.</div>
  </div>
</div>

only background: (closest to question)

#home-img-text-container {
  margin: 1em;
  display: inline-block;
  background: 
    linear-gradient(-45deg, transparent 1.5em, rgba(0, 0, 0, 0.8) 1.5em) bottom left no-repeat, 
    linear-gradient(-135deg, transparent 1.5em, rgba(0, 0, 0, 0.8) 1.5em) top left no-repeat;
  background-size: 100% 50%;
  /* rgba(0,0,0,.8);*/
  padding: 30px 20px;
}
#home-img-text-description {
  color: #FFF;
  font-size: 2.5em;
  line-height: 1.4em;
}


html {
  background:linear-gradient(to bottom right, tomato 30%, white 30%);
  }
<div id="home-img-text">
  <div id="home-img-text-container">
    <div id="home-img-text-description">WRECKING
      <br>& DEMOLITION
      <br>DONE RIGHT.</div>
  </div>
</div>

border and background :

#home-img-text-container {
  margin:1em;
  display:inline-block;
    background:
      linear-gradient(0deg, rgba(0,0,0,0.9), rgba(0,0,0,0.9)) top left no-repeat ,
      linear-gradient(0deg, rgba(0,0,0,0.9), rgba(0,0,0,0.9)) bottom left no-repeat ,
      linear-gradient(90deg, rgba(0,0,0,0.9), rgba(0,0,0,0.9)) top left no-repeat,
      linear-gradient(0deg, rgba(0,0,0,0.9), rgba(0,0,0,0.9)) center right no-repeat,
      linear-gradient(-45deg, transparent 1.5em, rgba(0,0,0,0.8) 1.5em, rgba(0,0,0,0.9) calc(1.5em + 3px), transparent  calc(1.5em + 3px)) bottom left no-repeat,
      linear-gradient(-135deg, transparent 1.5em, rgba(0,0,0,0.8) 1.5em, rgba(0,0,0,0.9) calc(1.5em + 3px), transparent  calc(1.5em + 3px)) top left no-repeat,
      linear-gradient(-45deg, transparent 1.5em, rgba(0,0,0,0.8) 1.5em) bottom left no-repeat,
      linear-gradient(-135deg, transparent 1.5em, rgba(0,0,0,0.8) 1.5em) top left no-repeat;
  
  background-size: 
    calc(100% - 2.25em) 3px ,
    calc(100% - 2.25em)  3px , 
    3px 100%, 
    3px calc(100% - 4.25em), 
    100% 50%, 100% 50%, 
    100% 50%, 100% 50%
    ;
      /* rgba(0,0,0,.8);*/
    padding: 30px 20px;
}
#home-img-text-description {
    color: #FFF;
    font-size: 2.5em;
    line-height: 1.4em;
}


html {
  background:linear-gradient(to bottom right, tomato 25%, gold 25%);
  }
<div id="home-img-text">
  <div id="home-img-text-container">
    <div id="home-img-text-description">WRECKING
      <br>& DEMOLITION
      <br>DONE RIGHT.</div>
  </div>
</div>

demo & codepen to play with

G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
0

You are achieve this using css3 selector before and after.

<style type="text/css">
   #home-img-text-container {
      background: rgba(0,0,0,.8);
      padding: 30px 20px;
      width: 300px;
      position: relative;
   }
   #home-img-text-container:before{
     content: "";
     position: absolute;
     top: 0;
     right: 0;
     border-bottom: 45px solid rgba(0,0,0,0);
     border-right: 45px solid #fff;
     width: 45px;
     height: 0;
  }
  #home-img-text-container:after{
     content: "";
     position: absolute;
     bottom: 0;
     right: 0;
     border-top: 45px solid rgba(0,0,0,0);
     border-right: 45px solid #fff;
     width: 45px;
     height: 0;
  }
  #home-img-text-description {
     color: #FFF;
     font-size: 2.5em;
     line-height: 1.4em;
  }

</style>
<div id="home-img-text">
    <div id="home-img-text-container">
       <div id="home-img-text-description">WRECKING <br>& DEMOLITION <br> DONE RIGHT.</div>
   </div>
</div>
Mukesh Ram
  • 6,248
  • 4
  • 19
  • 37