Recreating Photoshop Effects in CSS – Part 2 – The CSS Code

In part 1 of this tutorial series I demonstrated how to create vector shape buttons with layer styles in Photoshop. For part 2 I’ll explain how to write code to recreate these layer styles in CSS.

Modern development techniques have advanced along with browser support to make CSS3 a viable option for everyone. Photoshop layer styles such as drop shadows and gradients originally required images. Now it’s possible to create these effects with nothing more than CSS3.

css3 buttons final preview screenshot

If you want to see the final outcome check out my preview on CodePen which you can edit and reuse for your own project work.

View Full Source Code

HTML/CSS Document Structure

First we need to create the document for housing this code. It’s a good idea to use the HTML5 doctype and possibly create a new stylesheet for the CSS code.

<!doctype html> <html lang="en-US"> <head>   <meta charset="utf-8">   <meta http-equiv="Content-Type" content="text/html">   <title>SpyreStudios Buttons in CSS3</title> </head>  <body> </body> </html> 

Notice I have a very barren header with only the essentials. This is going to be a really simple document because we’re just creating a few buttons, so there really isn’t a lot to worry about.

In the page body we need a couple elements for containing the buttons. I’m creating an outer container with the ID #wrapper to center everything on the page. Inside the wrapper are two containers with the class .btnrow.

Each button row holds an individual button. We actually could use anchor elements but I’ve gone the traditional route and used HTML button elements. Here’s what my body HTML looks like:

<body>   <div id="wrapper">     <div class="btnrow">       <button class="btn1">Click Me</button>     </div>          <div class="btnrow">       <button class="btn2">Click Me</button>     </div>   </div>  </body> 

Now that each button has been defined with a unique class we can go ahead and work in CSS.

Styling Button Set #1

During the CSS development process it will help to keep your PSD file open as a reference. This way you can check layer style values for shadow sizes and gradient colors.

My first button class .btn1 is 200px wide and 40px tall. Obviously in CSS this is flexible, but in Photoshop we used these numbers just because we needed to pick something.

.btn1 {   cursor: pointer;   width: 200px;   height: 40px;   text-align: center;   color: #fff;   font-size: 16px;   font-weight: bold;   text-shadow: 1px 1px 0 #3d628f;   border: 1px solid #3980d2;   background: #68a2f0;   background: -moz-linear-gradient(top,  #68a2f0 0%, #4b83c3 100%);   background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#68a2f0), color-stop(100%,#4b83c3));   background: -webkit-linear-gradient(top,  #68a2f0 0%,#4b83c3 100%);   background: -o-linear-gradient(top,  #68a2f0 0%,#4b83c3 100%);   background: -ms-linear-gradient(top,  #68a2f0 0%,#4b83c3 100%);   background: linear-gradient(to bottom,  #68a2f0 0%,#4b83c3 100%);   filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#68a2f0', endColorstr='#4b83c3',GradientType=0 );   border-radius: 5px;   outline: none; } 

First off each button needs to give the impression of a natural link hover. This is created by forcing the cursor to look like a pointer hand at all times. Next I’ve defined text colors and text shadow effects.

The gradient is undoubtedly the trickiest part. CSS3 has various prefixes for gradients that allow developers to maintain integrity dating back to IE6. I’ve used the ColorZilla Generator to create the base code for this tutorial.

Just select your button’s layer styles and open the gradient editor. From here you can copy both color values into Notepad or a blank document and then copy them into the ColorZilla webapp. This way you’ll save time and generate more accurate gradients.

The last point to make is my use of the outline property. In certain WebKit browsers(particularly Chrome) a blue outline is added to active elements. This is distracting and takes away from the inset shadow effect, so I’ve remove the outline entirely. In most cases you should remove the outline globally using a CSS reset snippet like Eric Meyer’s template.

.btn1:hover {   background: #8cbaf8;   background: -moz-linear-gradient(top,  #8cbaf8 0%, #5c93d5 100%);   background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#8cbaf8), color-stop(100%,#5c93d5));   background: -webkit-linear-gradient(top,  #8cbaf8 0%,#5c93d5 100%);   background: -o-linear-gradient(top,  #8cbaf8 0%,#5c93d5 100%);   background: -ms-linear-gradient(top,  #8cbaf8 0%,#5c93d5 100%);   background: linear-gradient(to bottom,  #8cbaf8 0%,#5c93d5 100%);   filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#8cbaf8', endColorstr='#5c93d5',GradientType=0 ); } .btn1:active {   color: #afc3da;   border: 0;   background: #2e4a6b;   background: -moz-linear-gradient(top,  #2e4a6b 0%, #34639a 100%);   background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#2e4a6b), color-stop(100%,#34639a));   background: -webkit-linear-gradient(top,  #2e4a6b 0%,#34639a 100%);   background: -o-linear-gradient(top,  #2e4a6b 0%,#34639a 100%);   background: -ms-linear-gradient(top,  #2e4a6b 0%,#34639a 100%);   background: linear-gradient(to bottom,  #2e4a6b 0%,#34639a 100%);   filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#2e4a6b', endColorstr='#34639a',GradientType=0 );   -webkit-box-shadow: inset 3px 0 10px #21364f;   -moz-box-shadow: inset 3px 0 10px #21364f;   box-shadow: inset 3px 0 10px #21364f; } 

Both hover & active states are created using very similar syntax. Gradients are updated when the user clicks or hovers over the button. Additionally during the active state the text color changes and the button gains an inner shadow.

If you don’t need all of these gradient properties feel free to removes the extraneous ones. I’ve written this code to be as compatible as possible to reach the widest audience of Internet users.

Styling Button Set #2

The second button style is a bit more complicated, but follows many of the same rules. We’re using the typical HTML button attribute with a class of .btn2.

I’ve copied many of the same styles for text color, button size, and mouse cursor icon. One major difference is the gradient code which now includes two distinct stops around 50%.

.btn2 {   cursor: pointer;   width: 200px;   height: 50px;   text-align: center;   color: #fff;   font-size: 20px;   font-weight: bold;   text-shadow: 2px 1px 1px #386379;   border: 1px solid #3180a7;   background: #6dbfe8;   background: -moz-linear-gradient(top,  #6dbfe8 0%, #28a1de 50%, #28a1de 50%, #1f8cc2 51%, #1f8cc2 51%, #33a0d6 100%);   background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6dbfe8), color-stop(50%,#28a1de), color-stop(50%,#28a1de), color-stop(51%,#1f8cc2), color-stop(51%,#1f8cc2), color-stop(100%,#33a0d6));   background: -webkit-linear-gradient(top,  #6dbfe8 0%,#28a1de 50%,#28a1de 50%,#1f8cc2 51%,#1f8cc2 51%,#33a0d6 100%);   background: -o-linear-gradient(top,  #6dbfe8 0%,#28a1de 50%,#28a1de 50%,#1f8cc2 51%,#1f8cc2 51%,#33a0d6 100%);   background: -ms-linear-gradient(top,  #6dbfe8 0%,#28a1de 50%,#28a1de 50%,#1f8cc2 51%,#1f8cc2 51%,#33a0d6 100%);   background: linear-gradient(to bottom,  #6dbfe8 0%,#28a1de 50%,#28a1de 50%,#1f8cc2 51%,#1f8cc2 51%,#33a0d6 100%);   filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6dbfe8', endColorstr='#33a0d6',GradientType=0 );   border-radius: 3px;   outline: none;   box-shadow: inset 0 1px 1px rgba(255,255,255,0.7); } 

Take note that CSS gradient syntax is very flexible and allows for anything you can imagine. It works very much like Photoshop where you can specifically force certain colors to appear at certain intervals.

.btn2:hover {   background: #5ba8d5;   background: -moz-linear-gradient(top,  #5ba8d5 0%, #2c8cc0 50%, #267eac 51%, #3190c2 100%);   background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#5ba8d5), color-stop(50%,#2c8cc0), color-stop(51%,#267eac), color-stop(100%,#3190c2));   background: -webkit-linear-gradient(top,  #5ba8d5 0%,#2c8cc0 50%,#267eac 51%,#3190c2 100%);   background: -o-linear-gradient(top,  #5ba8d5 0%,#2c8cc0 50%,#267eac 51%,#3190c2 100%);   background: -ms-linear-gradient(top,  #5ba8d5 0%,#2c8cc0 50%,#267eac 51%,#3190c2 100%);   background: linear-gradient(to bottom,  #5ba8d5 0%,#2c8cc0 50%,#267eac 51%,#3190c2 100%);   filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#5ba8d5', endColorstr='#3190c2',GradientType=0 ); } .btn2:active {   color: #add1e4;   border: 0;   background: #155f86;   background: -moz-linear-gradient(top,  #155f86 0%, #2880ae 75%);   background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#155f86), color-stop(75%,#2880ae));   background: -webkit-linear-gradient(top,  #155f86 0%,#2880ae 75%);   background: -o-linear-gradient(top,  #155f86 0%,#2880ae 75%);   background: -ms-linear-gradient(top,  #155f86 0%,#2880ae 75%);   background: linear-gradient(to bottom,  #155f86 0%,#2880ae 75%);   filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#155f86', endColorstr='#2880ae',GradientType=0 );   box-shadow: inset 0 4px 15px rgba(0,0,0,0.3), 0 1px 0 0 rgba(255,255,255,0.7);   } 

The hover class basically uses the exact same code but with a darker gradient.

Looking over the :active pseudo-class you’ll notice that it’s got a few interesting properties. I’ve updated the text color and shadow just like in the first button.

The button border has also been removed to create the effect of recession into the page.

What’s different this time is that I’m using two distinct box shadow effects. The first is an inset shadow which spreads out at 15px blur streaming down from the top. It makes the button feel more 3D as if a shadow is being cast on top of the gradient.

The secondary outer shadow becomes a thin 1px edge at the very bottom of the button. This represents a glossy lip at the corner of an edge which becomes more noticeable when the button “moves down” into the page.

All-in-all it’s pretty darn simple to recreate Photoshop styles once you get the hang of CSS3 syntax.

Wrap-Up

In this two-part series I’ve demonstrated how to create stylish buttons from a blank canvas and then transform them into working buttons via CSS3. If you want to improve your design/dev workflow then practice by making little interfaces similar to the buttons we created here.

Also feel free to download a copy of the PSD or edit my HTML/CSS source directly.

With this simple demonstration you should have a much better idea of the steps and skills required to create interactive elements for the web.

The post Recreating Photoshop Effects in CSS – Part 2 – The CSS Code appeared first on SpyreStudios.


SpyreStudios