<?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: Giulia Malaroda</title>
    <description>The latest articles on DEV Community by Giulia Malaroda (@giuliamalaroda).</description>
    <link>https://dev.to/giuliamalaroda</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%2F665057%2F8306669e-e6d5-45c1-b03d-f0ac65a5a66e.jpeg</url>
      <title>DEV Community: Giulia Malaroda</title>
      <link>https://dev.to/giuliamalaroda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/giuliamalaroda"/>
    <language>en</language>
    <item>
      <title>Animated gradient background using only CSS</title>
      <dc:creator>Giulia Malaroda</dc:creator>
      <pubDate>Fri, 17 Sep 2021 15:32:53 +0000</pubDate>
      <link>https://dev.to/giuliamalaroda/animated-gradient-background-using-only-css-4e6l</link>
      <guid>https://dev.to/giuliamalaroda/animated-gradient-background-using-only-css-4e6l</guid>
      <description>&lt;p&gt;Have you ever had to create an element that has a gradient as a background and the colors of the gradient are inverted upon hover? The answer is certainly "Yes".&lt;/p&gt;

&lt;h2&gt;
  
  
  👣 First steps
&lt;/h2&gt;

&lt;p&gt;First we need an element that has a gradient background (from &lt;code&gt;#3DE9BD&lt;/code&gt; to &lt;code&gt;#2FB594&lt;/code&gt;) to animate: I have chosen to use a button.&lt;/p&gt;

&lt;p&gt;💡 Curiosity - today the css &lt;code&gt;linear-gradient()&lt;/code&gt; function is supported by all browsers, but for some old versions of some of these (Firefox, Internet Explorer) there is no full compatibility and to display the gradient correctly it is necessary to use a prefix. In this regard, there are autoprefix tools for css that take care of inserting any prefixes based on the browser versions that must be supported by the project.&lt;/p&gt;

&lt;p&gt;It’s time to talk about animation.&lt;br&gt;
To animate any other property of an element with css we use the css transition property, but in the case of a gradient this is not possible. Now I will share with you two alternatives to solve the problem.&lt;/p&gt;
&lt;h2&gt;
  
  
  1️⃣ Solution 1 – using &lt;code&gt;::before&lt;/code&gt; and &lt;code&gt;::after&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The first solution is to use the &lt;code&gt;::before&lt;/code&gt; and &lt;code&gt;::after&lt;/code&gt; pseudo-elements.&lt;/p&gt;

&lt;p&gt;We set the positioning of the button as relative so that we can position the two pseudo-elements absolutely inside it with 100% width and height to cover its entire size.&lt;/p&gt;

&lt;p&gt;At this point, to &lt;code&gt;::before&lt;/code&gt; we assign as background a gradient from &lt;code&gt;#3DE9BD&lt;/code&gt; to &lt;code&gt;#2FB594&lt;/code&gt; and set its opacity to 1 while to &lt;code&gt;::after&lt;/code&gt; we set the gradient with inverted colors, i.e. from &lt;code&gt;#2FB594&lt;/code&gt; to &lt;code&gt;#3DE9BD&lt;/code&gt; and opacity 0.&lt;/p&gt;

&lt;p&gt;To both we add the transition property to get an animation when we hover the button with the mouse. Also on hover, the opacity values ​​will have to reverse: &lt;code&gt;::before&lt;/code&gt;‘s opacity becomes 0 and &lt;code&gt;::after&lt;/code&gt;‘s opacity becomes 1.&lt;/p&gt;

&lt;p&gt;We are almost there, now we have to make sure that the text of the button is visible and not covered by the two pseudo-elements: just insert the text within a &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; tag and assign it a relative positioning and a z-index equal to 1.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/giuliamalaroda/embed/eYWPEJP?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;🏆 Goal achieved!&lt;/p&gt;

&lt;h2&gt;
  
  
  2️⃣ Solution 2 – using &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s suppose that for some reason we need one of the two pseudo-elements for our button, for example we need to show an icon, we need to use some other element to achieve our purpose.&lt;/p&gt;

&lt;p&gt;And why not use a &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt;?&lt;br&gt;
&lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; has no semantic implications, so it’s the perfect solution.&lt;/p&gt;

&lt;p&gt;First of all, also for this solution we must assign the relative positioning to the button to position the &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; absolutely and fix its width and height at 100%.&lt;/p&gt;

&lt;p&gt;Next step? Simple: we exploit the pseudo-elements of the &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; by giving it exactly the same characteristics as in solution 1. We insert the text inside a second &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; to which we assign a relative positioning and a z-index equal to 1 and…&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/giuliamalaroda/embed/MWmPvmz?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;🏆 The game is done!&lt;/p&gt;

&lt;h2&gt;
  
  
  🎓 Conclusion
&lt;/h2&gt;

&lt;p&gt;With these two simple methods we get the expected result without making use of infinite lines of code or javascript libraries, but simply by writing a few more lines of css.&lt;/p&gt;

&lt;p&gt;In any case, my hope is that in the near future not even this work-around is necessary and that the goal can be reached with a simple transition.&lt;/p&gt;

&lt;p&gt;Fingers crossed 🤞🏼😉&lt;/p&gt;

</description>
      <category>css3</category>
      <category>frontend</category>
      <category>tutorial</category>
      <category>snippet</category>
    </item>
  </channel>
</rss>
