DEV Community

Cover image for 4 Simple CSS Hover Transitions for your Elements Background
Adrian Twarog
Adrian Twarog

Posted on

25 9

4 Simple CSS Hover Transitions for your Elements Background

This video will show you 4 different ways you can perform a CSS background transition on a clickable element. CSS transitions are useful, especially because often mobile and tablet devices no longer have a clickable title or button eg. readme. Instead, the whole element is clickable.

Being able to have nice CSS hover effects allows users on desktop to know the elements are clickable. There are many different ways you can approach this using CSS and transitions.

We will take a look at:

  • Background color changes
  • Using transition
  • Using special CSS elements like :before
  • Applying different transitions effects like:
  • Using absolute values to have a top-down transition
  • Using border-radius to create a circular transition
  • Using border-width to create angled transition

Background Transition 1

Just changing the background with a smooth transition

.v1 {
  transition: 0.25s ease background;
  &:hover {
    background:#e1e1e1;
  }
}

Background Transition 2

Sliding the overlay from the top to the bottom

.v2 {
  position: relative;
  overflow: hidden;
  h2,.img,p {
    position: relative;
  }
  &:before {
    content:"";
    display: block;
    position: absolute;
    top:0;
    left:0;right:0;
    bottom:100%;
    background:#e1e1e1;
    transition: 0.25s ease bottom;
  }
  &:hover {
    &:before {
      bottom:0%;
    }
  }
}

Background Transition 3

Creating a circle that expands from the middle

.v3 {
    position: relative;
    overflow: hidden;
    h2,.img,p {
        position: relative;
    }
    &:before {
        content:"";
        display: block;
        position: absolute;
        top:50%;
        left:50%;
        border-radius:50%;
        width:0%;
        padding-top:0%;
        transform: translate(-50%,-50%);
        background:#e1e1e1;
        transition: 0.5s ease padding-top, 0.5s ease width;
    }
    &:hover {
        &:before {
            width:150%;
            padding-top:150%;
        }
    }
}

Background Transition 4

Creating an angle that comes from the top right to the bottom left

.v4 {
    position: relative;
    overflow: hidden;
    h2,.img,p {
        position: relative;
    }
    &:before {
        content:"";
        display: block;
        position: absolute;
        top:0;
        left:0;
        width:0px;
        height:0px;
        border-style:solid;
        border-width:0px 0px 0 0;
        border-color:#e1e1e1 transparent transparent transparent;
          transition: 0.5s ease border-width;
    }
    &:hover {
        &:before {
            border-width:800px 800px 0 0;
        }
    }
}

Want to see more:

I will try to post new great content every day. Here are the latest items:

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (7)

Collapse
 
sanchezdav profile image
David Sanchez

Awesome transitions!

Collapse
 
adriantwarog profile image
Adrian Twarog

Thanks, will be doing box-shadow transitions today, stay tuned!

Collapse
 
yarmaa profile image
Yarmaa

Wow thank you very much :)

Collapse
 
adriantwarog profile image
Adrian Twarog

Thanks, going to actually be making slightly more complex transitions each day this week to build on the previous, so stay tuned.

Collapse
 
berns_churches profile image
Berns Fire Death

These are sick, cheers mate.

Collapse
 
adriantwarog profile image
Adrian Twarog

Thanks, hoping to build a collection of videos to show the fundamentals of CSS properties and build better and better designs and transitions.

Collapse
 
baldezg profile image
Gabriel

One way I like to create the slide effect is by using box-shadow: inset.

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

Build With Me: AI-Powered Adaptive Web Scraper with LLMs

Join us for a hands-on session with Zia Ahmad where we build an AI-driven web scraper that adapts to site changes in real-time. Code along and level up your automation skills.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️