Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Only Folded Corner Div

Tags:

css

So I have been slamming my head against the wall trying several different methods online and can't get anything to work.

I have a div that needs to be fluid width, and its height needs to be variable as well.

The div sits on top of a tile-able background image. It has a 1px border around it.

I need the bottom right of the div to fold up, like a piece of paper.

I tried using an image, in a div anchored to the bottom. But that requires a fixed width or height as far as I can tell.

I tried this method but it requires a solid background color. I have a repeating image.

I tried this method, which uses gradients to control the opacity at the corner, this almost works, but my div requires a border. Applying the border ruins the effect.

background:
    linear-gradient(45deg,  rgba(255,0,0,0.4), rgba(255,0,0,0.4)),
    linear-gradient(135deg, rgba(255,0,0,0.4), rgba(255,0,0,0.4)),
    linear-gradient(225deg, transparent 10px,  rgba(255,0,0,0.4)
background-size: 14px 14px, 50% 100%, 50% 50%, 50% 50%;
background-position: 100% 0, 0 0, 100% 100%, 100% 0;
background-repeat: no-repeat;

//then an :after pseudo class to create the corner fold

Any help would be greatly appreciated. Thanks.

like image 290
Michael Botelho Avatar asked Jun 24 '13 02:06

Michael Botelho


1 Answers

This question got me bussy for some time, this seems to be a really hard thing to do with CSS only. I managed to achieve the effect(the paper flip with a border around the element) you wanted, but it requires alot of CSS and I don't know how far you want to go. I applied border-radius to the top right corner and used a triangle to overlap the border-radius. This did not cover the entire border radius, so I used a span to form 2 shapes to overlay the remaining gap.

Look at this fiddle for the result, any improvements are welcome

http://jsfiddle.net/EnVnW/

CODE:

body {
    background: #444 url('http://leaverou.me/ft2010/img/darker_wood.jpg') bottom;
}

.arrow_box {
    color:red;
    position: relative;
    background: #88b7d5;
    border: 4px solid #c2e1f5;
    height:400px;
    border-radius:0 300px 0 0; /* here we give the box a round corner on the top right side */
}

.arrow_box:after, .arrow_box:before { /* we create 2 triangles in the top right corner, one for the border and one for the filling */
    -ms-transform:rotate(-135deg); /* rotate to make the triangle have the right angle */
    -webkit-transform:rotate(-135deg); 
     transform:rotate(-135deg);
    bottom: 100%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    top:42px;
    right:-20px;
}

/* here we position the triangles in the top right corner */
.arrow_box:after {
    border-color: rgba(200, 265, 0, 0);
    border-bottom-color: #00b7d5;
    border-width: 100px;
    left: 100%;
    margin-left: -240px;
}
.arrow_box:before {
    border-color: rgba(194, 225, 245, 0);
    border-bottom-color: #c2e1f5;
    border-width: 105px;
    left: 100%;
    top:39px;
    margin-left: -245px;
}

/* we still have a massive gap next to the triangle, so we fill it up with 2 rectangles. One on the left side of the triangle and one on the bottom side of the triangle, we also will give them a border to finish the line around the element */
.arrow_box span {
    border-top: 4px solid #c2e1f5;
    position:absolute;
    width:150px;
    height:140px;
    background-color:black;
    right:140px;
    top:-4px;
    background: #88b7d5;
}

.arrow_box span:after {
    border-right: 4px solid #c2e1f5;  
    content: "";
    position:absolute;
    width:150px;
    height:150px;
    background: #88b7d5;
    left:140px;
    top:140px;
}

With CSS4 this will be alot easier to do, here you can read about it;

http://dev.w3.org/csswg/css-backgrounds-4/#border-corner-shape

like image 160
koningdavid Avatar answered Sep 20 '22 12:09

koningdavid