Pure CSS folded-corner effect

Create a simple CSS folded-corner effect without images or extra markup. It works well in all modern browsers and is best suited to designs with simple colour backgrounds.

Demo: Pure CSS folded-corner effect

Known support: Firefox 3.5+, Chrome 4+, Safari 4+, Opera 10+, IE 8+

This post is going to expand on the technique used to create the folded-corner effect that is part of the demo page for Multiple Backgrounds and Borders with CSS 2.1. As a starting point it will look to recreate the appearance of the note style used on the Yiibu’s fantastic web site. Where Yiibu uses images, this will use pseudo-elements.

Nothing complicated. Any element will do and there’s no need for extra markup. It’s just a simple coloured box to start with. Browsers with no support for pseudo-elements, such as IE6 and IE7, will only be capable of displaying this.

Adding position:relative makes it possible to absolutely position the pseudo-element.

.note {
   position:relative;
   width:30%;
   padding:1em 1.5em;
   margin:2em auto;
   color:#fff;
   background:#97C02F;
}

The folded-corner

The folded-corner is created from a pseudo-element that is positioned in the top corner of the box. The pseudo-element has no width or height but is given a thick border. Varying the size of the border will vary the size of the folded-corner.

In this example, the top and right borders are set to colours that match the background colour of the box’s parent. The left and bottom border are then given a slightly darker or lighter shade of the box’s background colour.

.note:before {
   content:"";
   position:absolute;
   top:0;
   right:0;
   border-width:0 16px 16px 0;
   border-style:solid;
   border-color:#658E15 #fff;
}

This is all that’s needed to create a simple folded-corner effect like that found on Yiibu.

Firefox 3.0 doesn’t allow for the positioning of pseudo-elements. You can throw in a couple of extra styles to help tidy things up in that browser.

.note:before {
   ...
   display:block;
   width:0;
}

Adding a subtle shadow

The appearance of a fold can be slightly enhanced by adding a box-shadow (for browsers that support it) to the pseudo-element. Setting overflow:hidden on the note itself will help hide parts of the shadow that would disrupt the folded-corner effect.

.note:before {
   ...
   -webkit-box-shadow:0 1px 1px rgba(0,0,0,0.3), -1px 1px 1px rgba(0,0,0,0.2);
   -moz-box-shadow:0 1px 1px rgba(0,0,0,0.3), -1px 1px 1px rgba(0,0,0,0.2);
   box-shadow:0 1px 1px rgba(0,0,0,0.3), -1px 1px 1px rgba(0,0,0,0.2);
}

Rounded corners

It’s also relatively simple to make this work with rounded corners if desired. Unfortunately, every modern browser has some form of border-radius bug – including those using the non-prefixed property – which means a slight work around is needed.

Webkit browsers are the only browsers that can come anywhere close to rounding the corner of the pseudo-element if it only has 2 borders. Opera 11 and Firefox 3.6 make a mess of it. Opera 11 makes the biggest mess.

Using 4 borders avoids the problems in Opera 11 and Firefox 3.6. But it will trigger a bug in Safari 5 that leaves the diagonal looking a little jaggy. We can get around this problem by setting at least one border colour to be transparent.

When a background colour is applied to the pseudo-element it will show through the transparent border. Ideally, this approach would form the basis of the entire effect because we could reduce the amount of code needed. But Opera 11 will not show the background colour through the transparent borders unless a border-radius has been set.

.note-rounded:before {
   content:"";
   position:absolute;
   top:0;
   right:0;
   border-width:8px;
   border-color:#fff #fff transparent transparent;
   background:#658E15;
   -webkit-border-bottom-left-radius:5px;
   -moz-border-radius:0 0 0 5px;
   border-radius:0 0 0 5px;
   display:block; width:0;
}

The CSS file on the demo page has more comments on the work arounds. Every browser has its own peculiarities when it comes to using border-radius or borders on elements with no width or height. This is the merely simplest solution I’ve found to deal with those browser inconsistencies.

The final code

This is all the CSS needed to create a simple folded-corner effect, with a subtle shadow, from a single HTML element. To include a variant with rounded corners, the “note” object can be extended with the modifications described previously.

.note {
   position:relative;
   width:30%;
   padding:1em 1.5em;
   margin:2em auto;
   color:#fff;
   background:#97C02F;
   overflow:hidden;
}

.note:before {
   content:"";
   position:absolute;
   top:0;
   right:0;
   border-width:0 16px 16px 0;
   border-style:solid;
   border-color:#fff #fff #658E15 #658E15;
   background:#658E15;
   -webkit-box-shadow:0 1px 1px rgba(0,0,0,0.3), -1px 1px 1px rgba(0,0,0,0.2);
   -moz-box-shadow:0 1px 1px rgba(0,0,0,0.3), -1px 1px 1px rgba(0,0,0,0.2);
   box-shadow:0 1px 1px rgba(0,0,0,0.3), -1px 1px 1px rgba(0,0,0,0.2);
   display:block; width:0; /* Firefox 3.0 damage limitation */
}

.note.rounded {
   -webkit-border-radius:5px 0 5px 5px;
   -moz-border-radius:5px 0 5px 5px;
   border-radius:5px 0 5px 5px;
}

.note.rounded:before {
   border-width:8px;
   border-color:#fff #fff transparent transparent;
   -webkit-border-bottom-left-radius:5px;
   -moz-border-radius:0 0 0 5px;
   border-radius:0 0 0 5px;
}

The demo page shows the final effect, an example with rounded corners, and how different coloured notes are easy to create from this base.

This technique works less well when the element receiving the folded-corner effect is sitting on top of a background image rather than a simple background colour. However, the same limitation exists for image-based methods of creating this effect.

Reply on Twitter Retweet on Twitter Favorite on Twitter

18 comments

#

Hans Christian Reinl says…

Thanks for this post. Pretty cool effect with CSS. I have a short addition. Maybe it is possible to add a shadow for the folded corner also in IE 6+ with a filter as it is described here: http://maddesigns.de/box-shadow-schatten-css3-adventskalender-tag-5-413.html (German). Just add this for .note:before:

filter: progid:DXImageTransform.Microsoft.Shadow (color=#000000, Direction=135, Strength=1);

-ms-filter: "progid:DXImageTransform.Microsoft.Shadow (color=#000000, Direction=135, Strength=1)";

It is not the same as with box-shadow of CSS3 but maybe it’s better than nothing.

#

Nicolas says…

@Hans: Pseudo-elements are not supported by IE6 or IE7. My personal preference is not to use MS filters and they don’t work on pseudo-elements in IE8.

#

Graham says…

This looks really cool.
I can’t wait to give it a try.
Thanks for the tut :)

#

Marc says…

You can even add a transition effect to it and make it fold up or fold down more and changing the border size. Works great in a lot of browsers too.

#

Nicolas says…

@Marc: CSS transitions are not currently supported on pseudo-elements.

#

Greg Babula says…

Awesome stuff, thanks for sharing

#

Montana Flynn says…

@Marc & @Nicolas you could add the markup with some jQuery or do the whole thing if you wanted. I still feel more comfortable with animations coming from javascript then css, although I do see the huge benefits of having all the design aspect together the support is just not there.

#

Nicolas says…

@Montana: You could use jQuery but then it wouldn’t be a pure CSS solution. Adding a transition effect is unnecessary (what for?) and relying on jQuery seems like an unnecessarily heavy approach. The effect is a minor enhancement that all capable browsers can produce. It’s not important for every browser to render a web page in the same way, and I don’t think it’s desirable to complicate things for modern browsers just to include a minor effect in IE 6 and IE 7.

#

Walton says…

Awesome, looks pretty good!

#

p6ril says…

This is indeed very elegant. Using border width is so obvious in its simplicity I’ve never thought of using them in such a way :-). Very nice, bravo!

#

Anand says…

which values do I need to change to make the fold apper in the bottom right corner?
Thanks great effect.

#

Nicolas says…

@Torsten: Thanks for spotting that. I’ve updated the CSS to get around Firefox 4′s behaviour.

#

Nicolas says…

@Torsten: Are you using a Mac? It’s not there in Firefox 4 RC1 on Windows Vista.

#

Torsten says…

Yes, I’am using a Mac. (10.6.6)

#

Chris Brown says…

@Anand I have played around with the code and it is possible to have the fold on any corner of the box.

In the CSS rule .note:before make the following changes:

First select the corner of the box you want to be folded by changing the values top:0; and/or right:0; as appropriate. For example by changing it to bottom:0; and left:0;.

Next for a fold on the top left or bottom right corners of the box the turn slopes the other way. You can alter this by changing border-width:0 16px 16px 0; to border-width: 16px 16px 0 0;.

Finally, if necessary, swap the colours around by changing border-color:#658E15 #fff; to border-color:#fff #658E15; to complete the effect.

Thanks Nicolas for an really useful tutorial.

Comments are now closed.