SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

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

May 27, 2015 by Jake Rocheleau

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.

[html wraplines=”false”]<!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>
[/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:

[html wraplines=”false”]<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>
[/html]

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.

[css wraplines=”false”].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;
}
[/css]

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.

[css wraplines=”false”].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;
}
[/css]

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%.

[css wraplines=”false”].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);
}
[/css]

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.

[css wraplines=”false”].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);
}
[/css]

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.

Filed Under: Tutorial Tagged With: css3, open source, tutorial, web development

Recent Posts

  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022
  • How to Get Your First Web Design Client

Archives

  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022