Modern CSS3 techniques to embellish your website

by Jean. 12 Comments -

The CSS3 specification allows front-end developers to create sophisticated visual effects to make websites look better. I have compiled over 10 new CSS3 techniques to embellish your website and give it a more professional look and feel.

Black and white images using CSS3

The following CSS class will display any color image in black and white. The vendor prefix allows the trick to work on any browser.

img.desaturate { 
    filter: grayscale(100%);
    -webkit-filter: grayscale(100%);
    -moz-filter: grayscale(100%);
    -ms-filter: grayscale(100%);
    -o-filter: grayscale(100%);
}

Source/Demo: http://demosthenes.info/blog/532/Convert-Images-To-Black–White-With-CSS

Page top shadow in CSS3

Here is a simple snippet to give your website a nice page top shadow. Easy to apply and visually pleasant!

body:before {
          content: "";
          position: fixed;
          top: -10px;
          left: 0;
          width: 100%;
          height: 10px;

          -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
          -moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
          box-shadow: 0px 0px 10px rgba(0,0,0,.8);

          z-index: 100;
}

Source/Demo: http://playground.genelocklin.com/depth/

Detecting double-clicks in CSS3

Believe it or not, it’s possible to detect when an element has been double-clicked by using just CSS, as demonstrated in the following code:

<div class="test3">
  <span><input type="text" value="&nbsp;" readonly="true" />
  <a href="http://google.com">Double click me</a></span>
</div>

<style type="text/css">
.test3 span {
	position: relative;
}
.test3 span a { 
	position: relative;
	z-index: 2; 
}
.test3 span a:hover, .test3 span a:active { 
	z-index: 4; 
}
.test3 span input { 
	background: transparent; 
	border: 0; 
	cursor: pointer; 
	position: absolute; 
	top: -1px; 
	left: 0; 
	width: 101%;  /* Hacky */
	height: 301%; /* Hacky */
	z-index: 3; 
}
.test3 span input:focus { 
	background: transparent; 
	border: 0; 
	z-index: 1; 
}
</style>

Source/Demo: http://css-tricks.com/examples/CSSDoubleClick/

Triangles in CSS3

Yes, it’s actually possible to draw triangles using only CSS. Although it’s probably not the best way of doing it, I still find this technique pretty useful and interesting.

/* create an arrow that points up */
div.arrow-up {
  width:0px; 
  height:0px; 
  border-left:5px solid transparent;  /* left arrow slant */
  border-right:5px solid transparent; /* right arrow slant */
  border-bottom:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}

/* create an arrow that points down */
div.arrow-down {
  width:0px; 
  height:0px; 
  border-left:5px solid transparent;
  border-right:5px solid transparent;
  border-top:5px solid #2f2f2f;
  font-size:0px;
  line-height:0px;
}

/* create an arrow that points left */
div.arrow-left {
  width:0px; 
  height:0px; 
  border-bottom:5px solid transparent;  /* left arrow slant */
  border-top:5px solid transparent; /* right arrow slant */
  border-right:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}

/* create an arrow that points right */
div.arrow-right {
  width:0px; 
  height:0px; 
  border-bottom:5px solid transparent;  /* left arrow slant */
  border-top:5px solid transparent; /* right arrow slant */
  border-left:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}

Source/Demo: http://davidwalsh.name/css-triangles

Using CSS calc()

calc() works like a function and allow you to perform calculations to determine the size and shape of objects. It can be used anywhere a length is required.

/* basic calc */
.simpleBlock {
  width: calc(100% - 100px);
}

/* calc in calc */
.complexBlock {
  width: calc(100% - 50% / 3);
  padding: 5px calc(3% - 2px);
  margin-left: calc(10% + 10px);
}

Source/Demo: http://davidwalsh.name/css-calc

Pure CSS text gradients

Text gradients have always been popular on the internet. Now with CSS3, it’s a lot easier to create beautiful gradients in a matter of minutes.

h2[data-text] {
	position: relative;
}
h2[data-text]::after {
	content: attr(data-text);
	z-index: 10;
	color: #e3e3e3;
	position: absolute;
	top: 0;
	left: 0;
	-webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,1)), to(rgba(0,0,0,0)));

Source: http://snipplr.com/view/49911/pure-css-text-gradients/

Disabling pointer events with CSS

The newly introduced pointer-events property allow you to deactivate pointer events on an element. For example, a link with the following class will not be clickable anymore.

.disabled { pointer-events: none; }

Source/Demo: http://davidwalsh.name/pointer-events

Stiched elements in CSS3

The following code snippet shows how to create a nice stitched look around any element. Nice!

p {
	padding: 5px 10px;
	margin: 10px;
	background: #ff0030;
	color: #fff;
	font-size: 21px;
	line-height: 1.3em;
	border: 2px dashed #fff;
	border-radius: 3px;
	-moz-border-radius: 3px;
	-webkit-border-radius: 3px;
	-moz-box-shadow: 0 0 0 4px #ff0030, 2px 1px 4px 4px rgba(10,10,0,.5);
	-webkit-box-shadow: 0 0 0 4px #ff0030, 2px 1px 4px 4px rgba(10,10,0,.5);
	box-shadow: 0 0 0 4px #ff0030, 2px 1px 6px 4px rgba(10,10,0,.5);
	text-shadow: -1px -1px #aa3030;
}

Source: http://www.catswhocode.com/blog/snippets/stitched-elements-in-css3

Custom scrollbars with CSS3 and WebKit

Remember 10 years ago where almost anyone used Microsoft exclusive properties to customize the look of scrollbars? Well, now you can do the same with Webkit.

::-webkit-scrollbar {
  width: 12px;
}

::-webkit-scrollbar-track {
  background: none;
}

::-webkit-scrollbar-thumb {
  background: -webkit-linear-gradient(left, #547c90, #002640);
  border: 1px solid #333;
  box-shadow: inset 1px 0 0 rgba(255, 255, 255, 0.4);
}

Source/Demo: http://davidwalsh.name/custom-scrollbars

Blurry text with CSS3

A simple but very nice text blur effect. Easy and good-looking!

.blur {
   color: transparent;
   text-shadow: 0 0 5px rgba(0,0,0,0.5);
}

Source/Demo: http://css-tricks.com/snippets/css/blurry-text/

Pure CSS corner ribbon

This code is a bit long, but it creates a fancy corner ribbon in pure CSS.

<div class="wrapper">
       <div class="ribbon-wrapper-green"><div class="ribbon-green">NEWS</div></div>
</div>

And the CSS:

.wrapper {
  margin: 50px auto;
  width: 280px;
  height: 370px;
  background: white;
  border-radius: 10px;
  -webkit-box-shadow: 0px 0px 8px rgba(0,0,0,0.3);
  -moz-box-shadow:    0px 0px 8px rgba(0,0,0,0.3);
  box-shadow:         0px 0px 8px rgba(0,0,0,0.3);
  position: relative;
  z-index: 90;
}

.ribbon-wrapper-green {
  width: 85px;
  height: 88px;
  overflow: hidden;
  position: absolute;
  top: -3px;
  right: -3px;
}

.ribbon-green {
  font: bold 15px Sans-Serif;
  color: #333;
  text-align: center;
  text-shadow: rgba(255,255,255,0.5) 0px 1px 0px;
  -webkit-transform: rotate(45deg);
  -moz-transform:    rotate(45deg);
  -ms-transform:     rotate(45deg);
  -o-transform:      rotate(45deg);
  position: relative;
  padding: 7px 0;
  left: -5px;
  top: 15px;
  width: 120px;
  background-color: #BFDC7A;
  background-image: -webkit-gradient(linear, left top, left bottom, from(#BFDC7A), to(#8EBF45)); 
  background-image: -webkit-linear-gradient(top, #BFDC7A, #8EBF45); 
  background-image:    -moz-linear-gradient(top, #BFDC7A, #8EBF45); 
  background-image:     -ms-linear-gradient(top, #BFDC7A, #8EBF45); 
  background-image:      -o-linear-gradient(top, #BFDC7A, #8EBF45); 
  color: #6a6340;
  -webkit-box-shadow: 0px 0px 3px rgba(0,0,0,0.3);
  -moz-box-shadow:    0px 0px 3px rgba(0,0,0,0.3);
  box-shadow:         0px 0px 3px rgba(0,0,0,0.3);
}

.ribbon-green:before, .ribbon-green:after {
  content: "";
  border-top:   3px solid #6e8900;   
  border-left:  3px solid transparent;
  border-right: 3px solid transparent;
  position:absolute;
  bottom: -3px;
}

.ribbon-green:before {
  left: 0;
}
.ribbon-green:after {
  right: 0;
}

Source/Demo: http://jsfiddle.net/chriscoyier/H6rQ6/1/

  • http://wpchannel.com/ Aurélien Denis

    Awesome list!

    I never heard about many of them. Thanks!

  • http://livingaudio.co Saïd

    Great set! Looking forward to using some of these in the near future.

  • http://www.codeconquest.com/ Charles @ CodeConquest.com

    There are so many hidden secrets in CSS! I am finding out about new ones all the time: nth-child, attribute selectors, boolean selectors, filters, now the calc function. I think the calc function will be very useful.

  • http://dev.webpulser.com Garry

    Awesome the css black & white image tip. thanks !

  • http://goodchee.com Lars Faye

    So interesting how CSS is starting to rival many techniques and processes that javascript would normally take care of, especially things like calc() and controlling the single/double click. I suppose it all relates to the layout and display of a site, but we’re teetering close to functionality. Add in SASS/Compass, and we’re getting into fuzzy territory! Interesting times.

  • http://www.apttdubai.com/safari_trips.htm Angela Lea

    O thanks for sharing such a great informative codes, I was about to start my project and i think all you points will help me out for making the best website, CSS gives a great look

  • http://css-live.ru/ SelenIT

    What a pity to see the misnomer that kills kittens (http://alistapart.com/article/every-time-you-call-a-proprietary-feature-css3-a-kitten-dies) on a blog that positions itself as “100% animal friendly”. And why there are properties like “-moz-border-radius” (discontinued since Firefox 13) and “-ms-linear-gradient” (that doesn’t work in IE9 and isn’t needed in IE10 which supports standard syntax) in many examples, but no standard unprefixed syntax? It’s only the standard syntax that can be called CSS3, not experimental implementations or vendor-specific features.

    And CSS Triangles are CSS2.1 (and even CSS1) stuff, not CSS3.

  • Casey Dennison

    Thanks you for this informative list! It’s quite amazing how CSS has evolved since it’s first iteration. I really like the green ribbon..

  • http://www.irctc-pnrstatus.in/ Gautham

    Wow awesome, Never knew there was a grayscale function in CSS3

  • http://www.greensprocket.com David Tym

    There is a great little tool out there for generating CSS triangles which I find to be a lot faster than manually coding the CSS.

    CSS Triangle Generator: http://apps.eky.hk/css-triangle-generator/

    Hope it helps someone. :)

  • http://getpnr.blogspot.in/ Shayan

    I was looking for creating blurry text through CSS but didn’t know how to do it. But i can do it with the code you have provided.
    Thanks for sharing these important codes.

  • http://myirctcloginpage.blogspot.in/ jozane

    Awesome tips n tutorial :)