<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mustafa Hashim</title>
    <description>The latest articles on DEV Community by Mustafa Hashim (@xas).</description>
    <link>https://dev.to/xas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1324599%2F278c335e-9ba1-479d-84b9-abaded0455b2.png</url>
      <title>DEV Community: Mustafa Hashim</title>
      <link>https://dev.to/xas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xas"/>
    <language>en</language>
    <item>
      <title>Creating Gradient Buttons with Animated Text using CSS</title>
      <dc:creator>Mustafa Hashim</dc:creator>
      <pubDate>Mon, 04 Mar 2024 13:51:50 +0000</pubDate>
      <link>https://dev.to/xas/creating-gradient-buttons-with-animated-text-using-css-23jl</link>
      <guid>https://dev.to/xas/creating-gradient-buttons-with-animated-text-using-css-23jl</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Ever clicked a boring old button and thought, "there has to be a more exciting way"?  Buttons are a crucial part of any website, but they don't have to be plain and uninspired. With the power of CSS, you can create stunning gradient buttons that elevate the user experience and make your website visually pop.  This tutorial will guide you through the steps of crafting beautiful gradient buttons with a touch of animation, using just basic HTML and CSS knowledge.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Requirments:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic HTML Knowelage&lt;/li&gt;
&lt;li&gt;Basic CSS Knowelage&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  You will learn:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;How to use &lt;strong&gt;after&lt;/strong&gt; and apply real examples on it&lt;/li&gt;
&lt;li&gt;How to create a beutiful background gradients&lt;/li&gt;
&lt;li&gt;How to use &lt;code&gt;box-shadow&lt;/code&gt; property&lt;/li&gt;
&lt;li&gt;How to animate shadows and gradients&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Setup the HTML code, we will apply the properties on a button:&lt;/p&gt;

&lt;p&gt;index.html&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;button id="B" type="button"&amp;gt;Titanium &amp;lt;i class="fa-brands fa-apple"&amp;gt;&amp;lt;/i&amp;gt;&amp;lt;/button&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;strong&gt;id&lt;/strong&gt; attribute gives id to the button so we can edit it in a seperate css file.&lt;/p&gt;

&lt;p&gt;the &lt;code&gt;i&lt;/code&gt; element is the icon of the button, I'm using &lt;a href="https://fontawesome.com" rel="noopener noreferrer"&gt;fontawesome.com&lt;/a&gt; for the icon, the class &lt;code&gt;fa-apple&lt;/code&gt; retrives Apple icon for us.&lt;/p&gt;

&lt;p&gt;Now let's start with the CSS file:&lt;br&gt;
style.css&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#B {
  width: 148px;
  height: 48px;
  color: #fff;
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 8px;
  border: none;
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You should get something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsp05t9nv5lbeofd63gqy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsp05t9nv5lbeofd63gqy.png" alt="Button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Gradients
&lt;/h2&gt;

&lt;p&gt;Let's start with the main meal, the gradient background. Firstly, you should know the basic syntax of the gradient property:&lt;br&gt;
We start by applying &lt;code&gt;background&lt;/code&gt; or &lt;code&gt;background-image&lt;/code&gt; property in the button like this:&lt;br&gt;
style.css&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#B {
   background: ;
   /* you can also use background-image it doesn't matter */
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now let's add a gradient background, to add it, use &lt;code&gt;linear-gradient&lt;/code&gt; in the &lt;code&gt;background&lt;/code&gt; property, the main sytanx would look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

background: linear-gradient(0deg, first color, second color, third color and so on ..);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;the first parameter is degree, you should set it as you love to, it is used to determine the direction of the gradient.&lt;/p&gt;

&lt;p&gt;the second parameter is the color properties, you can add as much colors as you want but no less than two, this is the syntax for the color:&lt;br&gt;
style.css&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

background: linear-gradient(
    135deg,
    rgba(0, 0, 0, 1) 0%, /* first color */
    rgba(85, 69, 79, 1) 60%, /* second color */
    rgba(149, 143, 98, 1) 100% /* third color */
  );


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I prefer using rgba instead of hex to have more customizations.&lt;br&gt;
the second color's parameter is the color length, it starts with 0% as minimum and 100% as maximum, you can't use the same length for more than one color, each color should has it's own length.&lt;/p&gt;

&lt;p&gt;now you have learned how to make gradient background, final gradient code would look like this:&lt;br&gt;
style.css&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#B {
  width: 148px;
  height: 48px;
  background: #958462 #55454f;
  background: linear-gradient(
    135deg,
    rgba(0, 0, 0, 1) 0%,
    rgba(85, 69, 79, 1) 60%,
    rgba(149, 143, 98, 1) 100%
  );
  color: #fff;
  font-size: 20px;
  font-weight: 600;
  position: relative;
  cursor: pointer;
  border-radius: 8px;
  border: none;
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;the final touch would be adding a &lt;code&gt;box-shadow&lt;/code&gt; property:&lt;br&gt;
style.css&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

box-shadow: inset 2px 0 0px rgba(255, 255, 255, 0.2),
    inset -2px 0 0px rgba(255, 255, 255, 0.2),
    inset 0 2px 0px rgba(255, 255, 255, 0.2),
    inset 0 -2px 0px rgba(255, 255, 255, 0.2);


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This would make the button much better, after all of that, the button should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6w2kgjpjivkaexcpua7b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6w2kgjpjivkaexcpua7b.png" alt="Titanium Button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Animating the Gradient Background
&lt;/h2&gt;

&lt;p&gt;In CSS, We can't animate the gradient background directly by applying a &lt;code&gt;:hover&lt;/code&gt; on the button, instead we can use another gradient on top of it and animate it, to do that, we will use &lt;code&gt;:after&lt;/code&gt; to do this.&lt;br&gt;
To use &lt;code&gt;:after&lt;/code&gt;, add it in a new block of code after specifing the button:&lt;br&gt;
style.css&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#B:after {}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;YOU SHOULD USE the same properties you applied to the button in the &lt;code&gt;:after&lt;/code&gt; and not only the changes, &lt;code&gt;:after&lt;/code&gt; is different from &lt;code&gt;:hover&lt;/code&gt;:&lt;br&gt;
style.css&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#B5:after {
  content: "BUY";
  opacity: 0;
/* It should only appear when we hover so mainly, it should be invisible */
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  cursor: pointer;
  border-radius: 8px;
background: linear-gradient(
    180deg,
    rgba(0, 0, 0, 1) 0%,
    rgba(85, 69, 79, 1) 60%,
    rgba(149, 143, 98, 1) 100%
  ); /* You can set a different new position */
  border: none;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: inset 2px 0 0px rgba(255, 255, 255, 0.2),
    inset -2px 0 0px rgba(255, 255, 255, 0.2),
    inset 0 2px 0px rgba(255, 255, 255, 0.2),
    inset 0 -2px 0px rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease-in-out; /* you can edit the transition as you prefer to */
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The first propery is &lt;code&gt;content&lt;/code&gt;, It specifies the inner text of the element &lt;code&gt;:after&lt;/code&gt;, the second property applies an absolute position with &lt;code&gt;left: 0; right: 0; top: 0;&lt;/code&gt; and &lt;code&gt;bottom: 0;&lt;/code&gt; because we want the &lt;code&gt;:after&lt;/code&gt; element on top of the main button.&lt;/p&gt;

&lt;p&gt;It should only appear when we hover so mainly, it should be invisible, that's why we use &lt;code&gt;opacity: 0;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now add a hover effect to show the button &lt;code&gt;:after&lt;/code&gt;:&lt;br&gt;
style.css&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#B:hover:after {
  opacity: 1;
}

/* Optional */
#B:focus {
  box-shadow: 0px 0px 26px rgba(149, 132, 98, 0.5),
    inset 2px 0 0px rgba(255, 255, 255, 0.2),
    inset -2px 0 0px rgba(255, 255, 255, 0.2),
    inset 0 2px 0px rgba(255, 255, 255, 0.2),
    inset 0 -2px 0px rgba(255, 255, 255, 0.2);
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The final code snippet:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#B {
  /* Button styles */
  width: 148px;
  height: 48px;
  background: #958462 #55454f;
  /* Original gradient */
  background: linear-gradient(
    135deg,
    rgba(0, 0, 0, 1) 0%,
    rgba(85, 69, 79, 1) 60%,
    rgba(149, 143, 98, 1) 100%
  );
  color: #fff;
  font-size: 20px;
  font-weight: 600;
  position: relative;
  cursor: pointer;
  border-radius: 8px;
  border: none;
  box-shadow: inset 2px 0 0px rgba(255, 255, 255, 0.2),
              inset -2px 0 0px rgba(255, 255, 255, 0.2),
              inset 0 2px 0px rgba(255, 255, 255, 0.2),
              inset 0 -2px 0px rgba(255, 255, 255, 0.2);
}

#B:after {
  /* Overlay element on hover */
  content: "BUY";
  opacity: 0;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: linear-gradient(
    180deg,
    rgba(0, 0, 0, 1) 0%,
    rgba(85, 69, 79, 1) 60%,
    rgba(149, 143, 98, 1) 100%
  );
  cursor: pointer;
  border-radius: 8px;
  border: none;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: inset 2px 0 0px rgba(255, 255, 255, 0.2),
              inset -2px 0 0px rgba(255, 255, 255, 0.2),
              inset 0 2px 0px rgba(255, 255, 255, 0.2),
              inset 0 -2px 0px rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease-in-out;
}

#B:hover:after {
  /* Show overlay on hover */
  opacity: 1;
}

#B:focus {
  /* Optional focus style */
  box-shadow: 0px 0px 26px rgba(149, 132, 98, 0.5),
              inset 2px 0 0px rgba(255, 255, 255, 0.2),
              inset -2px 0 0px rgba(255, 255, 255, 0.2);
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The final result 😎:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fissb9ok50c3g4ign83a2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fissb9ok50c3g4ign83a2.png" alt="Final Result Button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do you like it? ? Do you? ?? ?&lt;br&gt;
IDC.&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>html</category>
    </item>
  </channel>
</rss>
