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 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
WordPress plugin development: How to fix a SQL injection?WordPressHow to detect the Blog Page in WordPress?WordPressWhat is the best way to add JavaScript Code into HTML?HTMLForcing the domain to serve securely using HTTPSMiscellaneousGlobal JavaScript methods to convert variables to numbersJavaScriptThe basics of @import rule in SCSS (Sass)SCSSLIKE and ILIKE Operators in PostgresPostgresA simple example on grid ‘auto-fill’ vs ‘auto-fit’CSSHow to remove WordPress TinyMCE Editor buttons?WordPressThe order Property in Flex Items in CSSCSSHow to link/add CSS file to HTML Document?CSSSimple Page Hit Counter in PHPPHP