DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Modern CSS3 techniques to embellish your website

Modern CSS3 techniques to embellish your website

Jean-Baptiste Jung user avatar by
Jean-Baptiste Jung
·
Feb. 05, 13 · Interview
Like (0)
Save
Tweet
Share
3.67K Views

Join the DZone community and get the full member experience.

Join For Free

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=" " 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/

CSS

Published at DZone with permission of Jean-Baptiste Jung, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Was the Question Again, ChatGPT?
  • How to Submit a Post to DZone
  • API Design Patterns Review
  • What Is a Kubernetes CI/CD Pipeline?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: