Most buttons are boring . Solid standard frame . Most of them are not aligned correctly . In today's article , Let's learn how to create a gradient color button with animated borders and text ! One CSS Property handles all animations .
Contour button - simple , Straight and boring
Let's start with the basic outline button with hover . You can create it this way :
HTML:
<a href="/" title="Hello button" class="btn">Hello</a>
CSS:
.btn { border: 2px solid #4CAF50; background-color: transparent; color: #4CAF50
; padding: 10px 28px; font-size: 16px; cursor: pointer; transition: 256ms all;
border-radius: 20px; } .btn:hover { background-color: #4CAF50; color: #fff; }
<> Gradient buttons and text
further more , Let's add gradient borders and text . to this end , We need to do something :
* use div Wrap our buttons , And set the background to our body color .
* Add pseudo elements to create borders .
* last , We need to add three CSS attribute (background-clip: text ; -webkit-background-clip: text ;).
* -webkit-text-fill-color:rgba(255,255,255,0.001)
<>HTML:
<a href="/" title="Hello button" class="btn">Hello</a>
<>CSS:
.btn { display: block; width: 150px; background: linear-gradient(90deg,
#FFFF00 6%, #FFA500 25%, #F14444 45%, #D53567 55%, #9A109A 94%); text-align:
center; padding: 13px 20px; color: #fff; background-clip: text;
-webkit-background-clip: text; -webkit-text-fill-color: rgba(255,255,255,0.001);
transition: 256ms all; position: relative; cursor: pointer; } .btn:before {
background: rgb(24,24,24); content: ''; position: absolute; top: 0px; bottom:
0px; left: 0px; right: 0px; z-index: -1; } .btn:after { content: ''; position:
absolute; top: -1px; bottom: -1px; left: -1px; right: -1px; background:
linear-gradient(90deg, #FFFF00 6%, #FFA500 25%, #F14444 45%, #D53567 55%,
#9A109A 94%); transition: 256ms all; z-index: -1; }
fantastic ! You have created a gradient border button with gradient text ! Now? , We will pass additional CSS Make it a reality .
Ultimate BOSS - Animation gradient button
stay CSS in , We can't transition gradients . See this CSS The animation is great :
.gradient {
background-image: linear-gradient(red, blue);
transition: background-image 0.5s linear;
}
.gradient:hover {
background-image: linear-gradient(green, orange);
}
But it won't work . It immediately changes to another , No transition required . There are some techniques you can do , But my favorite is the animation background position .
first , We need to add two properties to the button :
Background size :200% automatic
Background position : Left middle
Then hover :
Background position : Right middle
under these circumstances , I added a gradient starting with white . It enhances the impression of animated borders .
HTML:
<a href="/" title="Hello button" class="btn">Hello</a>
CSS:
.btn { display: block; width: 150px; background: linear-gradient(90deg,#ffffff
3%,#ffffff 47%,#FFFF00 53%,#FFA500 72%,#F14444 77%,#D53567 88%,#9A109A 97%);
background-size: 200% 100%; background-position: left center; text-align: center
; padding: 13px 20px; color: #fff; background-clip: text;
-webkit-background-clip: text; -webkit-text-fill-color: rgba(255,255,255,0.001);
transition: 256ms all; position: relative; cursor: pointer; } .btn:before {
content: ''; position: absolute; top: -1px; bottom: -1px; left: -1px; right:
-1px; background: linear-gradient(90deg,#ffffff 3%,#ffffff 47%,#FFFF00
53%,#FFA500 72%,#F14444 77%,#D53567 88%,#9A109A 97%); transition: 256ms all;
z-index: -1; background-size: 200% 100%; background-position: left center; }
.btn:after { background: rgb(24,24,24); content: ''; position: absolute; top:
0px; bottom: 0px; left: 0px; right: 0px; z-index: -1; } .btn, .btn:before {
background-position: right center; }
Interested partners , You can join my front-end communication group 1141625295
Technology
Daily Recommendation