1

How can I create an angled div like the one in the below pic with CSS?

enter image description here

I have tried some transforms but didn't get the exact result that I am looking for.

Below is a snippet of the code that I had tried:

.angledwrapper {
  width: 105%;
  float: left;
  margin-left: -13px;
  margin-top: 65px;
  -webkit-transform: rotate(-5deg);
  -moz-transform: rotate(-5deg);
  -o-transform: rotate(-5deg);
  -ms-transform: rotate(-5deg);
  transform: rotate(-5deg);
}
.mindycontentwrap {
  width: 100%;
  float: left;
  background: #777;
}
.angledcontent {
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  -ms-transform: rotate(0deg);
  transform: rotate(0deg);
  float: left;
  min-height: 200px;
  width: 67%;
}
.angledwrapbottom {
  width: 100%;
  float: left;
  background: #777;
  -webkit-transform: rotate(-5deg);
  -moz-transform: rotate(-5deg);
  -o-transform: rotate(-5deg);
  -ms-transform: rotate(-5deg);
  transform: rotate(-5deg);
}
.angledtop {
  width: 105%;
  float: left;
  background: #777;
  -webkit-transform: rotate(-5deg);
  -moz-transform: rotate(-5deg);
  -o-transform: rotate(-5deg);
  -ms-transform: rotate(-5deg);
  transform: rotate(-5deg);
}
.Jane {
  position: relative;
  max-width: 30%;
  float: left;
}
<div class="musicwrap_container">
  <div class="angledwrapper">
    <div class="angledtop"></div>
    <div class="mindycontentwrap">
      <img src="xxxxx/images/Jane.png" title="Jane" alt="Jane" class="Jane">
      <div class="angledcontent"></div>
    </div>
    <div class="angledwrapbottom"></div>
  </div>
</div>

Fiddle Demo

Any idea?

Harry
  • 87,580
  • 25
  • 202
  • 214
Ana DEV
  • 1,018
  • 2
  • 17
  • 39
  • 1
    There are a few ways. More info on how the `div` would be put to use will help in providing the right solution. Is it just a background (or) will text be present within the `div` and be restricted to shape's boundaries? Will it require hover/click effects? – Harry Apr 06 '16 at 11:31
  • 1
    "*I have tried some transforms but exactly result i did not get.*" Where it is? – ketan Apr 06 '16 at 11:32
  • I will have a content inside and depending on that content height this angled wrapper must be streched @Harry – Ana DEV Apr 06 '16 at 11:32
  • So I need to have two divs combined with different angles @Harry? – Ana DEV Apr 06 '16 at 11:40
  • hey am trying to do that with two divs but did not get the proper look. Could you give me some sample please @Harry? – Ana DEV Apr 06 '16 at 12:27
  • 1
    See my updated question @Harry please – Ana DEV Apr 06 '16 at 12:46
  • @AnahitDEV: Is [this](https://jsfiddle.net/8y8nj9L2/4/) ok? – Harry Apr 06 '16 at 13:02

1 Answers1

2

This sort of a shape can be created in multiple ways and this answer covers a few of them. The pros and cons of each approach would be very similar to those described in this answer.

Using CSS Gradients:

This can also be done using linear-gradient background images. The approach would be same as the transforms except that here background-image of required size is placed on top and bottom rather than extra (real/pseudo) elements.

div {
  position: relative;
  padding: 55px 4px 125px;  /* important for space at top and bottom */
  background: linear-gradient(to top left, firebrick 49.5%, transparent 50.5%), linear-gradient(firebrick, firebrick), linear-gradient(to bottom right, firebrick 49.5%, transparent 50.5%);
  background-size: 100% 50px, 100% calc(100% - 175px), 100% 125px;  /* Y axis value controls height of angled parts */
  background-repeat: no-repeat;
  background-position: 0px 0px, 0% 50px, 0% 100%;
  min-height: 250px;
}
<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
  software like Aldus PageMaker including versions of Lorem Ipsum.</div>

Using SVG:

This can also created by using SVG path element to create the shape and place it absolutely behind the container.

div {
  position: relative;
  padding: 50px 4px 125px;  /* important for space at top and bottom */
  min-height: 250px;
}
div svg {
  position: absolute;
  content: '';
  height: 100%;
  width: 100%;
  top: 0px;
  left: 0px;
  z-index: -1;
}
path {
  fill: firebrick;
}
<div>
  <svg viewBox='0 0 800 800' preserveAspectRatio='none'>
    <path d='M0,40 L800,0 800,690 0,800z' />
  </svg>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
  software like Aldus PageMaker including versions of Lorem Ipsum.</div>

Note: The below snippet was part of the original answer but it is not advisable for containers that have dynamic width. As width increases, the skew creates trouble. Open the below snippet in full screen mode to see the problem.

Using transform: skewY() two pseudo-elements that are skewed at different angles can be placed on top + bottom of a normal div to create the required appearance.

div {
  position: relative;
  margin: 60px 4px 120px;  /* important to push element and leave space for angled parts */
  padding: 4px;
  background: firebrick;
  min-height: 150px;
}
div:after,
div:before {
  position: absolute;
  content: '';
  width: 100%;
  background: firebrick;
  backface-visibility: hidden;
  z-index: -1;
}
div:before {
  top: 0px;
  left: 0px;
  height: 100%;
  transform-origin: left top;
  transform: skewY(-4deg);
}
div:after {
  bottom: 0px;
  left: 0px;
  height: 100%;
  transform-origin: right bottom;
  transform: skewY(-10deg);
}
<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
  software like Aldus PageMaker including versions of Lorem Ipsum.</div>

Here is another approach using perspective transforms but this also won't suit you because adding text within it is very tough. There is another approach using CSS clip-path but that will work only in WebKit browsers.

Community
  • 1
  • 1
Harry
  • 87,580
  • 25
  • 202
  • 214
  • Hey when am trying to adjust per my design i don't see that angles. Changing margins, padding but no luck. Any idea @Harry? – Ana DEV Apr 06 '16 at 14:06
  • @AnahitDEV Sorry, I didn't understand. Can you show a demo and indicate what the problem is? (Also, note that the `skew` approach is probably not good for your case. It doesn't work when the width increases a lot.) – Harry Apr 06 '16 at 14:08
  • Hmm i guess that is the issue. I have a structure here https://jsfiddle.net/8y8nj9L2/3/ – Ana DEV Apr 06 '16 at 14:09
  • Ahh yes i have the new one just like yours just fullwidth and i don't see any angles. – Ana DEV Apr 06 '16 at 14:25
  • @AnahitDEV: Can you not use the gradients or SVG approach? They are much simpler for your case. I am also trying out something different but I don't think that'll be much better than these two. – Harry Apr 06 '16 at 14:26
  • [This](https://jsfiddle.net/n7uwr4km/) is another approach but adding text within this is very tough because the padding/margin top need to be adjusted as width increases. This is because the top-left edge keeps going down as width increases :( – Harry Apr 06 '16 at 14:30
  • How we can do it dynamically? So it will responsive? – Ana DEV Apr 06 '16 at 15:20
  • @AnahitDEV For gradients and SVG you can use the code in answer as-is. It is responsive. – Harry Apr 06 '16 at 15:22