<?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: Igor Zanella</title>
    <description>The latest articles on DEV Community by Igor Zanella (@igorzanelladev).</description>
    <link>https://dev.to/igorzanelladev</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%2F470815%2Fc4e5f12d-0c9a-4042-89ad-a952ea126637.jpeg</url>
      <title>DEV Community: Igor Zanella</title>
      <link>https://dev.to/igorzanelladev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/igorzanelladev"/>
    <language>en</language>
    <item>
      <title>Light Switch canvas responsive background</title>
      <dc:creator>Igor Zanella</dc:creator>
      <pubDate>Fri, 02 Oct 2020 00:28:59 +0000</pubDate>
      <link>https://dev.to/igorzanelladev/light-switch-canvas-responsive-background-l4a</link>
      <guid>https://dev.to/igorzanelladev/light-switch-canvas-responsive-background-l4a</guid>
      <description>&lt;p&gt;Welcome to this new tutorial. I did it using vanilla JS because it's more simple to adapt it to various framework. I did it previously on Svelte, but obviously is possible also on React, Angular, Vue, etc. with some little modification.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is the effect?
&lt;/h1&gt;

&lt;p&gt;The effect we want is the light turning on from the place where we have the light switch, it's also responsive.&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%2Fi%2Fzjzvswrn85maynkgknpy.gif" 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%2Fi%2Fzjzvswrn85maynkgknpy.gif" alt="Example"&gt;&lt;/a&gt;&lt;br&gt;
In this example, switch is only a checkbox, but as you can see below you can use something prettier, like a svg bulb.&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%2Fi%2Fvkrc5x9q7pzpboeu3grv.gif" 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%2Fi%2Fvkrc5x9q7pzpboeu3grv.gif" alt="Igor Zanella Old Site"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Let's start!
&lt;/h1&gt;

&lt;p&gt;I assume you already know basics of HTML, CSS and JS. We are going fast on simple declarations, concentrating on Canvas functions.&lt;/p&gt;
&lt;h2&gt;
  
  
  HTML
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
    &amp;lt;body&amp;gt;
        &amp;lt;canvas id="canvas-bg"&amp;gt;&amp;lt;/canvas&amp;gt;
        &amp;lt;main id="main"&amp;gt;
            &amp;lt;h1 id="title" class="sans-serif"&amp;gt;Changing color title&amp;lt;/h1&amp;gt;
            &amp;lt;div class="sans-serif"&amp;gt;&amp;lt;input type="checkbox" id="switch" onclick="handleClick(event)"/&amp;gt;Switch Bulb&amp;lt;/div&amp;gt;
        &amp;lt;/main&amp;gt;
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;HTML code is pretty simple, we are declaring 2 main containers, the canvas (background) and the main with the page content.&lt;br&gt;
We are declaring a checkbox which now will do the light switch, you can also use something else.&lt;/p&gt;
&lt;h2&gt;
  
  
  CSS
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:root {
    --dark-color: #040020;
    --light-color: #fff;
}

body {
    background-color: var(--dark-color);
    margin: 0;
}

main {
    display: flex;
    justify-content: center;
    padding: 10px;
}

#canvas-bg {
    position: fixed;
    z-index: -1;
    width: 100%;
    height: 100%;
}

.sans-serif {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

h1 {
    transition: all .5s;
}

.dark *{
    color: var(--dark-color);
}

.light *{
    color: var(--light-color);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;CSS is simple, too. Let's concentrate on colors, I created dark and light classes, which give the color to all the  children.&lt;/p&gt;

&lt;p&gt;The #canvas-bg which is the canvas object, is positioned fixed, which is relative to viewport, so it will remain there also if scrolling. Z-index is used to put the background behind the other elements.&lt;/p&gt;
&lt;h2&gt;
  
  
  Javascript
&lt;/h2&gt;

&lt;p&gt;Ok, now we are seeing Javascript in pieces, to explain what I did and why.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dark_color =  "#040020", light_color = "#fff";
let animationCount = 0;
const speed = 10;
const clickPosition = {x: 0, y: 0};

const switchBulb = document.getElementById("switch");
let lightOn = switchBulb.checked;

let canvas = document.getElementById("canvas-bg");
let ctx = canvas.getContext("2d");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are declaring some variables, starting from colors. Then we will find out what are animationCount, speed and clickPosition variables.&lt;/p&gt;

&lt;p&gt;We are binding switchBulb element with the checkbox and lightOn will be the value which is telling us if the light is on or off.&lt;/p&gt;

&lt;p&gt;After that we declare the canvas and we get the context from it.&lt;br&gt;
Now let's go to functions.&lt;/p&gt;
&lt;h3&gt;
  
  
  handleClick(e)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function handleClick(e) {
    lightOn = switchBulb.checked;
    clickPosition.x = e.x;
    clickPosition.y = e.y;
    if(lightOn) turnOn();
    else turnOff();
    changeContent();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;What do we do here? We are handling the click. So first we are assigning to lightOn the value of checkbox.&lt;/p&gt;

&lt;p&gt;Then we get from the event what is the click position based on the document, so we set it in the object we created before. That will be the starting point of the animation.&lt;/p&gt;

&lt;p&gt;Then we call one of the two functions, if lights is set to on we obviously call turnOn and viceversa.&lt;/p&gt;

&lt;p&gt;After that we call the changeContent function, which is explained below.&lt;/p&gt;
&lt;h3&gt;
  
  
  resizeCanvas()
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function resizeCanvas(){
    ctx.canvas.width  = window.innerWidth;
    ctx.canvas.height = window.innerHeight;
    ctx.fillStyle = lightOn ? light_color : dark_color;
    ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
};
window.addEventListener("resize", resizeCanvas);
resizeCanvas();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;What are we doing here? This is the function dedicated to responsive design.&lt;/p&gt;

&lt;p&gt;We are resizing the width and height of canvas and filling it with a rect with the same size for background with the correct color, based on light state.&lt;/p&gt;

&lt;p&gt;Then we call the function to adapt at first time the canvas and adding the listener on windows resize.&lt;/p&gt;
&lt;h3&gt;
  
  
  changeContent()
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function changeContent(){
    let main = document.getElementById("main");
    main.classList.add(lightOn ? "dark" : "light");
    main.classList.remove(lightOn ? "light" : "dark");
}
changeContent();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This is simple, we are adding the class we created before, to change the content color, based on light state.&lt;/p&gt;

&lt;p&gt;We are also calling it for the first time to adapt the content color.&lt;/p&gt;

&lt;p&gt;In frameworks, this and other functions are useless, because you can set class directly on html based on js vars.&lt;/p&gt;
&lt;h3&gt;
  
  
  turnOn()
&lt;/h3&gt;

&lt;p&gt;Ok, this is the start of "difficult" part. Let's see the functions which turn on the lights divided into parts.&lt;/p&gt;

&lt;p&gt;What do we need? We need to create a circle, starting from zero pixels, till the maximum size. What will be the maximum size? It's calculated, we will see how.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function turnOn() {
    if(animationCount === 0) switchBulb.disabled = true;
    let pixelRadius = animationCount * speed;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we are at the start of animation, we are disabling the switch, to prevent some bugs on turning on and off.&lt;/p&gt;

&lt;p&gt;Then we are calculating the radius of the circle in pixels, which will be the animationCount (starting from zero) multiplied by speed, which is a default multiplier specified at the start.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ctx.fillStyle = dark_color;
    ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    ctx.beginPath();
    ctx.arc(clickPosition.x, clickPosition.y, pixelRadius, 0, 2 * Math.PI, true);
    ctx.fillStyle = light_color;
    ctx.fill();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What are we doing here? We are drawing. Starting with filling with dark background color the canvas.&lt;/p&gt;

&lt;p&gt;Then we are drawing the circle, starting from click position, with the pixel radius declared before, we fill it and we confirm the drawing. This is the first step of the animation, then?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    animationCount++;
    if(pixelRadius &amp;lt; (Math.sqrt(Math.pow(ctx.canvas.width,2) + Math.pow(ctx.canvas.height,2))+ 200)){
        setTimeout(turnOn, 1);
    } else {
        animationCount = 0;
        switchBulb.disabled = false;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This part is important, we are raising animationCount value by one.&lt;/p&gt;

&lt;p&gt;Then we are checking if pixelRadius is at the size we want the animation ends. What is it? That is Pythagorean theorem to calculate the diagonal between screen width and height. Then we add 200px to be sure the circle is out of the screen.&lt;/p&gt;

&lt;p&gt;So, if the circle reached the end, the animationCount returns to zero and switch is enabled, otherwise, this function will be relaunched asynchronously in 1 ms.&lt;/p&gt;

&lt;h3&gt;
  
  
  turnOff()
&lt;/h3&gt;

&lt;p&gt;Ok, this is the last important function.&lt;/p&gt;

&lt;p&gt;What do we need for the turnOff function? We need that the light circle, starts from maximum size and goes to zero, to switch off the light.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function turnOff() {
    let pixelRadius = animationCount * speed;
    if(animationCount === 0) {
        switchBulb.disabled = true;
        pixelRadius = (Math.sqrt(Math.pow(ctx.canvas.width,2) + Math.pow(ctx.canvas.height,2))+ 200);
        animationCount = Math.ceil(pixelRadius / speed);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are declaring the pixelRadius, like before. Obviously it can't works with animationCount to zero, so we check it.&lt;/p&gt;

&lt;p&gt;If animationCount is zero, we are disabling the switch and we are calculating the maximum size of the circle like on the function above. After the calculation we divide it by speed to get the starting animationCount value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ctx.fillStyle = dark_color;
    ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    ctx.beginPath();
    ctx.arc(clickPosition.x, clickPosition.y, pixelRadius, 0, 2 * Math.PI, true);
    ctx.fillStyle = light_color;
    ctx.fill();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like on the turnOn function, we are setting the background in dark color, and we are creating the circle with pixelRadius size.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    animationCount--;
    if(animationCount &amp;gt; 0) setTimeout(turnOff, 1);
    else {
        ctx.fillStyle = dark_color;
        ctx.fillRect(0, 0, ctx.canvas.width, canvas.height);
        switchBulb.disabled = false;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we are lowering animationCount value, then we check, if it is greater than zero, we relaunch the function asynchronously in 1 ms.&lt;br&gt;
If animationCount is zero or less, we are filling the background just to be sure, and then we are enabling the switch checkbox.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I hope this could be useful for you. I know I could put turnOn and turnOff together in some way, but now it's easier to explain.&lt;br&gt;
If you are using it in some project let me know.&lt;/p&gt;

&lt;p&gt;You can find the complete code here:&lt;br&gt;
&lt;a href="https://codepen.io/igorzanelladev/pen/yLOdbym" rel="noopener noreferrer"&gt;CodePen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can find me on:&lt;br&gt;
&lt;a href="https://igorzanella.dev" rel="noopener noreferrer"&gt;igorzanella.dev&lt;/a&gt;&lt;br&gt;
&lt;a href="//mailto:hello@igorzanella.dev"&gt;hello@igorzanella.dev&lt;/a&gt;&lt;br&gt;
&lt;a href="https://twitter.com/IgorZanellaDev" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/IgorZanellaDev" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Simple 3D CSS Buttons</title>
      <dc:creator>Igor Zanella</dc:creator>
      <pubDate>Thu, 17 Sep 2020 18:53:28 +0000</pubDate>
      <link>https://dev.to/igorzanelladev/simple-3d-css-buttons-2aj7</link>
      <guid>https://dev.to/igorzanelladev/simple-3d-css-buttons-2aj7</guid>
      <description>&lt;p&gt;Hi, this is my first article and I will do a CSS tutorial for noobs.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML
&lt;/h2&gt;

&lt;p&gt;The HTML is really simple here, we are putting 3 links using tag &lt;a&gt; in the body.&lt;/a&gt;&lt;/p&gt;

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

&amp;lt;body&amp;gt;
    &amp;lt;a href="#" class="btn b-1"&amp;gt;
        Button 1 (.b-1)
    &amp;lt;/a&amp;gt;
    &amp;lt;a href="#" class="btn b-2"&amp;gt;
        Button 2 (.b-2)
    &amp;lt;/a&amp;gt;
    &amp;lt;a href="#" class="btn b-3"&amp;gt;
        Button 3 (.b-3)
    &amp;lt;/a&amp;gt;
&amp;lt;/body&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;We declared 3 buttons with 2 classes: &lt;code&gt;btn&lt;/code&gt; for the basic CSS properties and &lt;code&gt;b-x&lt;/code&gt; for three different behaviors.&lt;/p&gt;

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

&lt;h4&gt;
  
  
  Basic CSS properties
&lt;/h4&gt;

&lt;p&gt;Ok, let's go to CSS part. I will jump to buttons part, I'm not explaining the body properties, because it's useless now.&lt;/p&gt;

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

.btn {
  color: #e5dc15;
  font-family: "Montserrat", sans-serif;
  text-transform: uppercase;
  text-decoration: none;
  margin: 1rem;
  padding: 0.5rem;
  border: 2px solid #e5dc15;
  border-radius: 0.5rem;
}

.btn:hover {
  background: #e5dc15;
  color: #0e79b2;
}


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

&lt;/div&gt;

&lt;p&gt;We declared some basic properties to give buttons colors and borders.&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%2Fi%2Fcg57p11885gtg1ektgdh.jpg" 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%2Fi%2Fcg57p11885gtg1ektgdh.jpg" alt="Buttons"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  "3D" property
&lt;/h4&gt;

&lt;p&gt;Ok I like to call it "3D" because it adds a shadow in the third dimension. Let's add this property to &lt;code&gt;.btn&lt;/code&gt; class.&lt;br&gt;
&lt;code&gt;box-shadow: 0 0.6em #c2bb11, 0 0.9em rgba(0, 0, 0, 0.4);&lt;/code&gt;&lt;br&gt;
What does it do? We are declaring two shadows, the first value is the horizontal offset (which is obviously zero), the second is the vertical offset, then there is the color.&lt;br&gt;
We are setting a shadow using darker yellow to do the "3D" shape, then we are adding the "real" gray shadow to button.&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%2Fi%2Ftshbclmgzr88mph2e9iz.jpg" 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%2Fi%2Ftshbclmgzr88mph2e9iz.jpg" alt="Buttons"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Transitions properties and related
&lt;/h4&gt;

&lt;p&gt;Now we are adding 3 properties to &lt;code&gt;.btn&lt;/code&gt; class, these are the base of the transition, the starting point.&lt;/p&gt;

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

  position: relative;
  top: 0;
  transition: all 300ms ease-in-out;


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

&lt;/div&gt;

&lt;p&gt;We are setting the &lt;code&gt;position: relative&lt;/code&gt;, to set then the &lt;code&gt;top&lt;/code&gt; property to zero, the base.&lt;br&gt;
Then we set the &lt;code&gt;transition&lt;/code&gt; on all the elements (in this case background, text, position and box-shadow) with a duration of 300ms and the timing function which give a smoother transition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Button transitions
&lt;/h3&gt;

&lt;p&gt;Ok let's set three different &lt;code&gt;:hover&lt;/code&gt; and three &lt;code&gt;:active&lt;/code&gt; declarations for the three buttons.&lt;/p&gt;

&lt;h4&gt;
  
  
  b-1
&lt;/h4&gt;

&lt;p&gt;The first button will lower on hover and get even lower on click.&lt;br&gt;
So we will set &lt;code&gt;top: 0.2em&lt;/code&gt; to lower button of 0.2em and then we are lowering also the box shadow for same values.&lt;br&gt;
Then we will set &lt;code&gt;top: 0.4em&lt;/code&gt; and box-shadows in the same way as before.&lt;/p&gt;

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

.btn.b-1:hover {
  top: 0.2em;
  box-shadow: 0 0.4em #c2bb11, 0 0.7em rgba(0, 0, 0, 0.4);
}

.btn.b-1:active {
  top: 0.4em;
  box-shadow: 0 0.2em #c2bb11, 0 0.5em rgba(0, 0, 0, 0.4);
}


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

&lt;/div&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%2Fi%2Fls6cz4j78783es0cbt2x.jpg" 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%2Fi%2Fls6cz4j78783es0cbt2x.jpg" alt="Button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  b-2
&lt;/h4&gt;

&lt;p&gt;In the second button we don't do nothing on hover, but we lower it all on the click.&lt;br&gt;
So we put top: &lt;code&gt;0.6em&lt;/code&gt; to lower the button for the maximum value of initial yellow box shadows, but we leave a little gray shadow to leave the "3d effect".&lt;/p&gt;

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

 .btn.b-2:active {
  top: 0.6em;
  box-shadow: 0 0.2em rgba(0, 0, 0, 0.4);
}


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

&lt;/div&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%2Fi%2F2jhbhp5yna4pf2qobx8p.jpg" 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%2Fi%2F2jhbhp5yna4pf2qobx8p.jpg" alt="Button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  b-3
&lt;/h4&gt;

&lt;p&gt;In the third button we are lifting the button on hover and doing the same thing as first button on click.&lt;br&gt;
So how can we lift the button? You could use &lt;code&gt;bottom&lt;/code&gt; properties, but you should also set it to zero for the initial classes.&lt;br&gt;
Here, for simplicity, we are using a negative top value, which it works in the same way.&lt;br&gt;
So we set the negative &lt;code&gt;top&lt;/code&gt; value and an higher gray shadow, because element is now higher.&lt;/p&gt;

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

.btn.b-3:hover {
  top: -0.5em;
  box-shadow: 0 0.6em #c2bb11, 0 1.3em rgba(0, 0, 0, 0.4);
}

.btn.b-3:active {
  top: 0.4em;
  box-shadow: 0 0.2em #c2bb11, 0 0.5em rgba(0, 0, 0, 0.4);
}


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

&lt;/div&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%2Fi%2Fshop08hsbm0qvo9mjb9m.jpg" 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%2Fi%2Fshop08hsbm0qvo9mjb9m.jpg" alt="Button"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Ok, we finished this simple CSS tutorial. Maybe the next will be on a Canvas animation i would like to use in my new portfolio website.&lt;/p&gt;

&lt;p&gt;Thank you.&lt;/p&gt;

&lt;p&gt;Demo &amp;amp; code: &lt;a href="https://codesandbox.io/s/simple-css-3d-buttons-5c9rg" rel="noopener noreferrer"&gt;CodeSandbox&lt;/a&gt;&lt;/p&gt;

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