• Skip to content
  • Skip to primary sidebar

Dynamic Drive Blog

  • JavaScript
  • CSS Library
  • Help Forums
  • Tools
Home / CSS / Popular CSS Button Hover Effects Explained
Popular CSS Hover and Mouseover Effects using CSS3 transitions and Pseudo Elements

Popular CSS Button Hover Effects Explained

December 1, 2017 by Dynamic Drive

Many CSS button effects I’ve seen lately involve animating one or both of the :before and :after pseudo elements to create interesting visuals on hover. To get an idea what I’m talking about, take a look at the following submit buttons:

Here I’m using submit buttons, but the same effect can easily be applied to any container element, such as a <a> element instead. Access or fork the full code here.

I thought it’d be helpful to break down the basic technique shared by all of these button styles, so that you can modify or create your own, completely unique CSS button effects using this simple but very versatile technique.

Setting Up Each Button

The basic set up of each of the buttons you see above is the same. Here’s a nifty diagram to visually explain the 3 moving parts:

CSS :before and :after Hover Effects Explained

Each button consists of:

  1. A main button container element (ie: BUTTON, A etc) set to position:relative and with a transparent background to allow its pseudo elements behind to show through.
  2. A :before pseudo element with z-index: -2 to act as the solid background color of the button. In some cases we’ll be animating this layer as well.
  3. A :after pseudo element with z-index: -1 to overlay the :before element. We’ll be animating this layer in all cases to create interesting hover effects.

Such a set up allows the button text to always remain on top while we animate layers beneath the main button.

Here is the stripped down code for the first “Slide Down” button example you see above:

The HTML:

<button class="buttonfx" type="submit">Slide Down</button>

Main CSS for Main Button Element:

.buttonfx{
    color: black; /* button text color */
    outline: none;
    background: transparent;
    border: none;
    border-bottom: 4px solid #eee;
    position: relative;
    display: inline-block;
    overflow: hidden;
    transition: all .5s;
}

Main CSS for :before and :after pseudo elements:

/* //// Default effect: Slide from Top  //// */

.buttonfx:before,
.buttonfx:after{
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    height: 100%;
    background: #259f6c; /* onhover background color */
    z-index: -1;
    transform: translate3D(0,-100%,0); /* move elements above button so they don't appear initially */
    transition: all .5s;
}

.buttonfx:before{
    background: #fafcd6; /* button default background color */
    z-index: -2;
    transform: translate3D(0,0,0);
}

.buttonfx:hover{
    color: white;
}

.buttonfx:hover:after{
    transform: translate3D(0,0,0);
    transition: all .5s;
}

As you can see, we begin by transforming the :before and :after elements above the button element, so they’re both out of view.

Then, for the :before element, we move it down so it shows through behind the button (transform: translate3D(0,0,0)). This layer acts as the default solid background color of the button.

When the user hovers over the button, we slide the :after element down to create the “slide down” effect. CSS3 transitions are used to animate the change in values.

Adding a Bounce Effect to Any Transition

The second row of buttons in the demo above are identical to the first, with the exception of a nifty bounce effect. This is accomplished by merely adding the following CSS class to any button:

.bouncein:hover:before,
.bouncein:hover:after{
    transition-timing-function: cubic-bezier(0.52, 1.64, 0.37, 0.66) !important;
}

The key lies in the custom cubic-bezier value, which creates a transition with the following behavior visually (as visualized in Chrome’s Developer’s Console):

Custom CSS Cubic Bezier value to create Bounce Effect

The dip in altitude near the end translates into the animation regressing temporarily before settling on the end value.

To add the bounce effect to any of the buttons, we just tag on the “bouncein” class to the button container:

<button class="buttonfx bouncein" type="submit">Slide Down</button>

Animating both Pseudo Elements at Once on Hover

The last row of buttons in the demo shows animating both the :before and :after elements when the mouse mouses over a button. The downside of this is that we lose the ability to specify the button’s default background color, as that role was previously assigned to the :before element. There are ways around this, however, such as by nesting the button inside a wrapper with a background color defined.

The following additional CSS class creates the “Curtain Down” effect you see in the first button of the 3rd row of the demo:

.curtaindown:before,
.curtaindown:after{
    background: #259f6c;
    transform: translate3D(0,-100%,0);
}

.curtaindown:after{
    transform: translate3D(0,100%,0);
}

.curtaindown:hover:before,
.curtaindown:hover:after{
    transform: translate3D(0,-50%,0);
    transition: all .5s;
}

.curtaindown:hover:after{
    transform: translate3D(0,50%,0);
}

HTML:

<button class="buttonfx curtaindown" type="submit">Slide Down</button>

Nothing much to it right?

Go and Create Your Own Pseudo Elements Based Hover Effects!

Now that you understand the parts and mechanics that go into so many of the CSS button effects making the rounds on the internet, go ahead and create your very own, totally unique button hover effect!

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to email this to a friend (Opens in new window)

Filed Under: CSS

Reader Interactions

Primary Sidebar

Featured Posts

  • Best Wordpress Theme Clubs and BundlesBest WordPress Theme Clubs That Offer Multiple The…
  • 7 Wordpress and Web Design Trends for 20187 WordPress and Web Design Trends to Watch in 2018
  • Add Custom JavaScript and CSS to WordPress
  • Best Wordpress Page Builders Compared and ReviewedBest WordPress Page Builders Compared and Reviewed
  • WordPress Genesis: Showing a different Featured Im…

Categories

  • CSS
  • Entrepreneurs
  • JavaScript
  • Plugins
  • Site News
  • Software
  • Web Building
  • Web Design
  • Wordpress
  • RSS Feeds
  • Twitter
  • Facebook
  • FAQs
  • Free Newsletter
  • Usage Terms
  • Contact
Affiliate Disclaimer: I earn a commission from some products mentioned. All opinions are my own. Copyright 2018 Dynamic Drive [ Top ]
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.