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

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
Kanban vs Scrum: The two frameworks of agile principlesMiscellaneousShort-Circuit Evaluation in JavaScriptJavaScriptHow to change your WordPress database prefix?WordPressHow to add AppRoutingModule in Angular application using command line?AngularThe Difference Between isNaN() Method And isNaN() Function In JavaScriptJavaScriptIntroduction to components and templates Part 1: Component MetadataAngularHow to refresh a page using PHP at regularly occurring intervals?PHPHow to add onclick event to html elements dynamically using JavaScript?JavaScriptWhat does useEffect do in React?ReactAvoid using new Array() in JavaScriptJavaScriptHow to get previous days or next days in PHP?PHPHow to convert a number rounding to a specified number of decimals in JavaScript?JavaScript