17

I want to create two divs which are floated left to each other, however with a slanted angled border separating them. I've attached a picture to demonstrate what I mean.

Does anyone know if something like this is possible with CSS (cutting off the content with an overflow:hidden I guess)

adjacent div with slanted side

These divs need to contain images that get cut off by the border, here is an example :

divs with images and slanted adjacent sides

web-tiki
  • 99,765
  • 32
  • 217
  • 249
Mark
  • 3,653
  • 10
  • 30
  • 62

3 Answers3

32

Try this

.left, .right {
  position: relative;
  height: 100px;
  width: 200px;
  background: #000;
  float: left;
}

.left:after {
  content: '';
  line-height: 0;
  font-size: 0;
  width: 0;
  height: 0;
  border-top: 100px solid #000;
  border-bottom: 50px solid transparent;
  border-left: 0px solid transparent;
  border-right: 50px solid transparent;
  position: absolute;
  top: 0;
  right: -50px;
}

.right {
  margin-left: 60px;
  width: 100px;
}

.right:before {
  content: '';
  line-height: 0;
  font-size: 0;
  width: 0;
  height: 0;
  border-top: 50px solid transparent;
  border-bottom: 100px solid #000;
  border-left: 50px solid transparent;
  border-right: 0px solid #000;
  position: absolute;
  top: -50px;
  left: -50px;
}
<div class="left"> </div>
<div class="right"> </div>

UPDATE with images

.left, .right {
    background: #000 url('http://lorempixel.com/300/100');
    position: relative;
    height: 100px;
    width: 250px;
    float: left;
}

.left:after {
    content: '';
    line-height: 0;
    font-size: 0;
    width: 0;
    height: 0;
    border-top: 50px solid transparent;
    border-bottom: 100px solid #fff;
    border-left: 30px solid transparent;
    border-right: 0 solid #fff;
    position: absolute;
    top: -50px;
    right: 0;
}

.right {
    background: #000 url('http://lorempixel.com/200/100');
    width: 150px;
}

.right:before {
    content: '';
    line-height: 0;
    font-size: 0;
    width: 0;
    height: 0;
    border-top: 100px solid #fff;
    border-bottom: 50px solid transparent;
    border-left: 0px solid transparent;
    border-right: 30px solid transparent;
    position: absolute;
    top: 0;
    left: 0;
}
<div class="left"> </div>
<div class="right"> </div>
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
  • looks good, but one caveat. I actually want the div to have an image in it which will get cut off by the angled margin. When I add a photo to your code it doesnt maintain the angle :( – Mark May 13 '12 at 00:17
  • that wasn't in the requirements :) – Zoltan Toth May 13 '12 at 00:21
  • you are correct, I've updated the original post to better describe what I was looking for. Thanks for the effort! – Mark May 13 '12 at 00:27
  • This is dope man. Well done – Blaine Kasten Aug 26 '14 at 03:39
  • 2
    This is great but how do you edit this code to work with fluid heights? So if the left hand div has 2 paragraphs of text (length of which could change) how do you ensure the angle stays 100% height? Currently it doesn't. – egr103 Feb 23 '16 at 15:00
16

All of the solutions so far depend on having a really thick angled border to divide the photos.

To avoid this you'd make a container and skew it. Then counter skew the image in the opposite direction.

Here's a CodePen http://cdpn.io/azvsA, but the gist of it is as follows:

.container {
  border-right: 10px solid white;
  overflow: hidden;
  transform (skewX(-20deg));
}

.image {
  transform (skewX(20deg));
}
Jeff Lupinski
  • 226
  • 2
  • 5
  • 1
    this has the downside that the left side and the right outer side are angled aswell see https://imgur.com/a/3gYE3s5 – Snackaholic Feb 20 '20 at 15:53
4

You can write like this:

.left, .right {
    background: #000 url('http://lorempixel.com/300/100');
    position: relative;
    height: 100px;
    width: 250px;
    float: left;
}
.left{
    z-index:1;
}
.parent{
    overflow:hidden;
}

.right {
    background: #000 url('http://lorempixel.com/200/100');
    width: 150px;
}
.left:after{
    content:'';
    position:absolute;
    border-right:20px solid #fff;
    top:-25px;
    bottom:-10px;
    left:0;
    right:-10px;
    -moz-transform:rotate(10deg);
    -webkit-transform:rotate(10deg);
}

Check this http://jsfiddle.net/EJxFg/4/

sandeep
  • 91,313
  • 23
  • 137
  • 155