Yogesh Chauhan's Blog

Filling a button background from left to right using CSS

in CSS on July 30, 2021

This is an example of a simple CSS tricks that could produce a nice animation.

Basically, the trick is into adding a background using linear-gradient and changing its position on hover.

Let’s dive into it.

Let’s create a simple button first.

Add the background using linear-gradient.

Change the size of the background to 200%.

The overall background is doubled now and we can set the position of the background to right. If you want to create animation from right to left, set the background-position to left or change the linear-gradient.

Let’s also change the color of the button for better contarst.


Let’s set the background-position to right on hover.

Still we don’t see the animation. Do we?

At the end, all comes onto the transition property. Let’s add it. You can adjust the speed using the time in the transition property.

That’s it.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Default Values in SCSS (Sass)SCSSComposing and Extracting Components in ReactReactWordPress: How to setup and get values from an ACF options page?WordPressWhat is the difference between let and var in Swift?SwiftGeneral concepts in DrupalDrupalHow to update or disable WordPress Post Revisions?WordPressDifferent Types of Functions in JavaScriptJavaScriptWhy it’s not a good idea to create Number objects in JavaScript?JavaScriptThe 7 Security Objectives of Any Organization for IT and Network SecurityMiscellaneousHow to merge arrays in JavaScript?JavaScriptContent Blocks in SCSS (Sass)SCSSWordPress: How to find all posts from a custom post type with specific custom field?WordPress