Category: In the Browser

CSS, HTML, animations, and UI techniques that shape the web experience. Learn how to build beautiful, responsive, and interactive interfaces right in the browser.

  • How to Make a Circular Clickable Button with Shadow Effect

    How to Make a Circular Clickable Button with Shadow Effect

    Hey there! 😃 In today’s technology-obsessed world, it’s absolutely crucial to master the art of creating an amazing circular clickable button. A well-crafted button not only enhances the presentation but also elevates the user experience. Maximizing user attention is vital, and buttons are, most of the time, the key to achieving this goal. By utilizing the right tools and techniques, we can create a visually stunning and functionally efficient button that not only looks great but also delivers an unforgettable user experience. 🆙

    Let’s collaborate to create a standout button that supports user-friendliness.

    HTML Basic Structure

    To start building our button, we need to organize our HTML code snippet. The first step is to create a parent element that will act as the base of our button. We give this element the class name .button-base. Next, we need to add a child element within this parent element to serve as the clickable part of the button. We give this child element the class name .clicable-part. It’s important to nest these two elements within a button HTML element as it’s more semantic, better for accessibility, and behaves correctly in forms and interactive contexts by default.
    Additionally, we need to create the shadow effect for our button, so we add one more HTML element with the class name .button-shadow.

    <button>
      <div class="button-base">
        <div class="clicable-part"></div>
      </div>
    </button>
    <div class="button-shadow"></div>
    HTML

    CSS foundation

    We move forward with CSS and make our button fresh and stylish! For this post, I am writing CSS using Sass syntax. If you would like to convert it to vanilla CSS and don’t already know how, I’d suggest you use an online Sass — CSS converter.

    Prepare the body for the clickable button

    Firstly, we apply a color (#f5f5f5 – dark gray) to the body by setting the background-color. Additionally, we want to center our button. For that reason, we are using the flex method.

    body {
      background-color: #f5f5f5;
      
      display: flex;
      align-items: center;
      justify-content: center;
    }
    SCSS

    Create the base of the clickable button

    To create the base of our button, we start by making a square with a width and height of 200 pixels. Then, we make it rounded by adding a border-radius of 50%. We use a pale white color (#f9f9f9) for the button’s background-color. To give it a more stylish look, we add some shadows using the box-shadow property.

    Finally, we add the flex method to prepare the space to position the clickable part of the button in the center of the base.

    📍 As seen in the code snippet below, we need to include border: none and background-color: transparent properties in the button element to ensure it will display only the styles we define. These properties remove the browser’s default styles that typically come with. As a result, it provides us with a clean starting point, so the button appears exactly as we’ve styled it, with no unexpected borders or colors.

    button {
      border: none;
      background-color: transparent;
      .button-base {
        width: 200px;
        height: 200px;
        border-radius: 50%;
        background-color: #f9f9f9;
        box-shadow: inset 0 6px 8px #eae8e8, /* top inset shadow */
          inset 0 -2px 15px #ccc7c7, /* bottom inset shadow */
          0 3px 3px #d6d1d1; /* bottom shadow */
        
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
    SCSS

    In the image below, you can see the base of the button we just created.

    An image showing the base of the circular clickable button

    Create the clickable part of the button

    To create the clickable part of our button, we first set the height and width to 80%. Then, we apply a border-radius: inherit to make it appear rounded and follow the shape of the base. For the background of the button, we use a linear-gradient that adds depth as it gives a bright orange color at the top that gradually becomes darker as it goes down. To add a more stylish look, we include shadows using the box-shadow property.

    .clicable-part {
      width: 80%;
      height: 80%;
      border-radius: inherit;
      border: 1px solid white;
      background: linear-gradient(
        to bottom,
        #f7eb13,
        #fd741b
        );
      box-shadow: inset 0px 3px 6px #ffa500, /* top inset shadow */
        0px 1px 3px #d05a05, /* bottom inset shadow */
        0 6px 6px #bcb8b8; /* bottom shadow */
    }
    SCSS

    The following image shows the clickable part we just added on the top of the base.

    An image showing the base of the circular clickable button. On it's top it has the clickable part.

    Add content at the top of the clickable part

    To add content at the top of the clickable part of a button, we can use a :before pseudoelement. Inside the pseudoelement, we can insert the desired symbol or text using the content property. For instance, in our case, a question mark emoticon (?) has been used, but you can use any symbol or text you prefer. It’s important to note that the flex method in the .clickable-part class is essential, as it centers the mark correctly.

    .clicable-part {
      ...
      display: flex;
      align-items: center;
      justify-content: center;
      &:before {
        position: absolute;
        content: "❔";
      }
    }
    SCSS

    The image below displays the content we added on the top of the clickable part of the button.

    An image showing the base of the circular clickable button. On it's top it has the clickable part. The clickable part it has a question mark on.

    Add the hover effect on the top of the clickable button

    To add a hover effect, we set the :hover CSS property. For the cursor, I prefer using the value pointer (👆), but feel free to choose any other style from the plethora of options available here. Finally, I apply the filter: brightness and increase it to 110%. This makes our button appear brighter whenever the mouse hovers over it. ✨

    .clicable-part {
      ...
      &:before {
        ..
      }
      &:hover {
        cursor: pointer;
        filter: brightness(110%);
      }
    }
    SCSS

    The gif below demonstrates how the hover effect (👆) appears.

    Activate the clickable part of the button

    To make your button fully functional, you need to activate its clickable part. We can achieve this by adding the :active property. Next, we give it a background with a linear-gradient that creates a sense of depth by providing a bright magenta color at the top that gradually becomes darker as it goes down. To make it more visually appealing, we include shadows using the box-shadow property.

    .clicable-part {
      ...
      &:before {
        ..
      }
      &:hover {
        ...
      }
      &:active {
        background: linear-gradient(
          to bottom,
          #f767f7,
          #df0361
          );
          box-shadow: inset 0px 3px 6px #df0361, /* top inset shadow */
          0 3px 5px #bcb8b8; /* bottom shadow */
      }
    }
    SCSS

    The following gif displays the activated clickable area.

    Update the button’s content when it is activated

    To enhance the user experience, we can dynamically update the content displayed on the button when it’s clicked. This may be accomplished by adding a :before pseudoelement and inserting the desired content into the content property. In our case, we will display a white heart (🤍) when the button is clicked.

    .clicable-part {
      ...
      &:before {
        ..
      }
      &:hover {
        ...
      }
      &:active {
        ...
        &:before {
          position: absolute;
          content: "🤍";
        }
      }
    }
    SCSS

    With just a single click, this button comes to life and reveals its beautiful new content in the following gif (🤍) – it’s an absolute must-see!! 😎

    Add the shadow effect to the clickable button

    The last thing we have to do is add the shadow effect. We create a rectangle with 140 pixels width and 15 pixels height. Next, we give it a rounded shape by setting the border-radius property to 50%. To create a background that looks like it becomes lighter as it goes toward outer space, we use the radial-gradient technique and make the center darker. To make the whole thing look smoother, we add shadows with the box-shadow property.

    Finally, we utilize the position: absolute combined with the top, left, and transform properties to move it below the button and center it.

    .shadow {
      width: 140px;
      height: 15px;
      border-radius: 50%;
      background: radial-gradient(#a7aaaa, #b2b7b7 10%, #eaeded);
      box-shadow: -5px 0px 10px 5px #eaeded, /* shadow right side */
        5px 0px 10px 5px #eaeded, /* shadow left side */
        inset -5px 0px 5px #eaeded, /* inset shadow right side */
        inset 5px 0px 5px #eaeded; /* inset shadow left side */
        
      position: absolute;
      top: 520px;
      left: 50%;
      transform: translate(-50%);
    }
    SCSS

    The button’s captivating shadow effect is truly impressive and adds to the overall appeal. Hope you find it enjoyable and engaging. Thank you for your attention. 🥳

    An image showing the base of the circular clickable button. On it's top it has the clickable part. The clickable part it has a question mark on. Button also has a shadow below.

    Complete code solution

    Below is the full code referenced in this blog post. Feel free to copy and use it in your own projects. If you have any questions or encounter any issues, don’t hesitate to reach out for assistance. You can easily copy the desired code snippet by clicking on the copy icon, located in the top-right corner of each snippet.

    <button>
      <div class="button-base">
        <div class="clicable-part"></div>
      </div>
    </button>
    <div class="button-shadow"></div>
    HTML
    @mixin flex-all-center {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    $body-color: #f5f5f5;
    $button-base-color: #f9f9f9;
    $button-base-inset-top-shadow: #eae8e8;
    $button-base-inset-bottom-shadow: #ccc7c7;
    $button-base-dark-shadow: #d6d1d1;
    $button-color: linear-gradient(to bottom, #f7eb13, #fd741b);
    $button-inset-top-shadow: #ffa500;
    $button-inset-bottom-shadow: #d05a05;
    $button-dark-shadow: #bcb8b8;
    $button-active-color: linear-gradient(to bottom, #f767f7, #df0361);
    $button-active-inset-shadow: #df0361;
    $button-shadow: #eaeded;
    
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html,
    body {
      height: 100vh;
    }
    
    body {
      background: $body-color;
      @include flex-all-center;
      min-width: 300px;
    }
    
    button {
      border: none;
      background-color: transparent;
      .button-base {
        width: 200px;
        height: 200px;
        border-radius: 50%;
        background-color: $button-base-color;
        box-shadow: inset 0 6px 8px $button-base-inset-top-shadow, /* top inset shadow */
        inset 0 -2px 15px $button-base-inset-bottom-shadow, /* bottom inset shadow */
        0 3px 3px $button-base-dark-shadow; /* bottom shadow */
        @include flex-all-center;
        .clicable-part {
          width: 80%;
          height: 80%;
          font-size: 50px;
          border-radius: inherit;
          border: 1px solid white;
          background: $button-color;
          box-shadow: inset 0px 3px 6px $button-inset-top-shadow, /* top inset shadow */
          0px 1px 3px $button-inset-bottom-shadow, /* bottom shadow */
          0 6px 6px $button-dark-shadow; /* bottom shadow */
          @include flex-all-center;
          &:before {
            position: absolute;
            content: "❔";
          }
          &:hover {
            cursor: pointer;
            filter: brightness(110%);
          }
          &:active {
            background: $button-active-color;
            box-shadow: inset 0px 3px 6px $button-active-inset-shadow, /* top inset shadow */
              0 3px 5px $button-dark-shadow; /* bottom shadow */
            &:before {
              position: absolute;
              content: "🤍";
            }
          }
        } /* end of clicable-part */
      } /* end of button-base */
    } /* end of button */
    
    .button-shadow {
      width: 140px;
      height: 15px;
      border-radius: 50%;
      background: radial-gradient(#a7aaaa, #b2b7b7 10%, #eaeded);
      box-shadow: -5px 0px 10px 5px $button-shadow, /* shadow right side */
        5px 0px 10px 5px $button-shadow, /* shadow left side */
        inset -5px 0px 5px $button-shadow, /* inset shadow right side */
        inset 5px 0px 5px $button-shadow; /* inset shadow left side */
      position: absolute;
      top: 520px;
      left: 50%;
      transform: translate(-50%);
    } /* end of shadow */
    SCSS
    Expand
  • How inset Works in box-shadow (Made Simple)

    How inset Works in box-shadow (Made Simple)

    Shadows are a simple yet powerful way to add depth and dimension. In programming, the box shadow CSS property creates a sense of layering by simulating light or darkness on HTML elements. The inset box-shadow, which we will analyze in this post, creates a shadow inside the element, giving it a “pressed” or “embossed” effect that adds even more visual interest to your design.

    📌 Always remember that inset shadows are drawn inside the box, hence they can reduce the space inside the element.

    For better clarity, let’s begin by breaking down the box-shadow property as each value controls and affects a specific part of how the shadow appears.

    box-shadow: /* offset-X, offset-Y, blur-radius, spread-radius, color;
    CSS

    🔸 position
      • offset-X: moves the shadow horizontally
      • offset-Y: moves the shadow vertically

    🔸 blur-radius
     • It softens the edges of the shadow

    🔸 spread-radius
      • It makes the shadow bigger

    🔸 color
      • It defines the color and the transparency rgba() of a shadow

    Setting up the layout

    We begin by establishing the basic HTML and CSS structure. Our shadow will be housed within a gray ⬜ box, but for now, we need to adjust the body element so it can properly center and display our box.

    <div class="box"></div>
    HTML
    body {
      background-color: #e0e0e0; /* Light Gray */
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 0;
    }
    CSS

    Creating the box

    Now it’s time to create a rounded box with a light gray background color to gently contrast with the body. This subtle color difference helps us clearly see the shadow effect in action.

    .box {
      width: 200px;
      height: 200px;
      border-radius: 10px;
      background-color: #ece9e9; /* Very Light Gray */
    }
    CSS
    A gray box on a dark gray background.

    Creating inner shadows using box-shadow

    We finalize our work with the shadow effect. Let’s break down our code to make it clear:

    inset -5px -5px 10px rgba(255, 255, 255, 0.8)
    ➤ It positions the shadow at the bottom-right, with a softly blurred, which makes the edges smooth rather than sharp. Also, the shadow is white and mostly visible.

    inset 5px 5px 10px rgba(50, 50, 50, 0.2)
    ➤ It positions the shadow at the top-left, we have again a softly blurred effect, and the shadow now is dark gray and very soft.

    .box {
      ...
      box-shadow: inset -5px -5px 10px 0 rgba(255, 255, 255, 0.8),
        inset 5px 5px 10px 0 rgba(50, 50, 50, 0.2);
    }
    CSS
    A gray box with inset dark shadow at the top left sides and light shadow at the bottom right sides

    If you use spread-radius with inset shadows, it can make the shadow spread too much, making it look too big or messy. By leaving it out, the shadow stays close to the edges, keeping the effect clean and focused.

    Working with different shades of gray adds a touch of elegance and also elevates your work. Additionally, it maintains a clean and modern design. 🚀 ✨ So, what’s your take? Do you agree with me, or do you see it in a whole different way? 😄

  • How box-shadow Works (Made Simple)

    How box-shadow Works (Made Simple)

    Hello there! Want to make your HTML elements stand out a bit more? The box-shadow property in CSS lets you add depth by creating a shadow around elements — almost like they’re floating off the page.

    To get a better idea of how box-shadow works, let’s look at the different values it takes. Each one controls a part of how the shadow shows up on the page:

    box-shadow: /* offset-X, offset-Y, blur-radius, spread-radius, color; */
    CSS

    🔸 position
      • offset-X: moves the shadow horizontally
      • offset-Y: moves the shadow vertically

    🔸 blur-radius
      • Makes the shadow’s edges smoother

    🔸 spread-radius
      • Expands the size of the shadow

    🔸 color
      • Sets the color and transparency using rgba()

    Setting spread-radius to 0 keeps the shadow tightly within the blur area. This helps control how far the shadow extends, making it feel like it’s coming from a specific direction — useful when you want the shadow to appear only on certain sides.

    Setting up the layout

    We begin by creating the basic HTML and CSS structure. We will work with a light gray ⬜ box and adjust the layout to perfectly center it within the body element.

    <div class="box"></div>
    HTML
    body {
      background-color: #e0e0e0; /* Light gray */
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 0;
    }
    CSS

    Creating the box

    We create a simple, rounded box that will hold our shadow effect. It has a very light gray background color to contrast with the body, and helps us visualize the effect.

    .box {
      width: 200px;
      height: 200px;
      border-radius: 10px;
      background-color: #f0f0f0; /* Very Light Gray */
    }
    CSS
    A gray box on a dark gray background.

    Adding the box-shadow effect

    We finalize our work with the shadow effect. First, we shift the shadow bottom and to the right (8px 8px), then we give a nice soft blur of 20px and a semi-transparent gray (rgba(120, 120, 120, 0.4)). And just like that, this simple line of code makes our box look 3D. How amazing! 😃

    .box {
      ...
      /* offset-X | offset-Y | blur-radius | spread-radius | color */
      box-shadow: 8px 8px 20px 0 rgba(120, 120, 120, 0.4);
    }
    CSS
    A light gray box with gray shadow effect at the bottom right sides

    Now your box stands out! ✨ Try it yourself or try something different! Want the shadow above the box and to the left? Use negative values. Want it sharper? Decrease the blur. Maybe with a little bit of color? 🎨 Play with the rgba() values.

    /* Alternative shadow example */
    
    .box {
      ...
      /* offset-X | offset-Y | blur-radius | spread-radius | color */
      box-shadow: -5px -5px 5px 0 rgba(220, 100, 180, 0.3);
    }
    CSS
    A gray box with a pink shadow at the top left sides

    Box shadows do more than just enhance appearance — they help direct attention and add gentle emphasis where it counts. Utilizing this small detail intentionally can have a significant impact on your overall design. ✨ 😃

  • CSS Water Drop Effect: How to Make Text Look Wet & Glossy

    CSS Water Drop Effect: How to Make Text Look Wet & Glossy

    😃 In this post, we are beyond excited to show you how to create a breathtaking water drop effect that stands on top of a text using only the power of CSS.

    We will confidently guide you through every single step of the process, from the initial design phase to the final finishing touches.

    In addition, we will share expert tips on how to masterfully experiment with CSS properties to achieve the perfect shape and reflection effects. So, let’s jump right in and create something fantastic together!

    Setting up the HTML structure for the water drop effect

    We begin with the HTML structure. We need to create an element with the class name .wrapper. This element will have two child elements. The first child element will be dedicated to the text, so we give a suitable class name, such as .text. Similarly, we should name the second child element with a class name that reflects its purpose, such as .drop.

    <div class="wrapper">
      <div class="text">drop</div>
      <div class="drop"></div>
    </div>
    HTML

    Our top priority right now is to work on the second child element, which is our amazing drop. 💦 🤓 Once we’re done with that, we’ll move on to the text.

    Styling the water drop effect with CSS

    We move forward with the CSS structure. To create a flexible layout for our project, we set the flex property on the element with the class .wrapper. This will allow us to easily adjust the size and position of its child elements.

    Additionally, we added the position: relative property to the wrapper to establish a reference point for any absolutely positioned child elements later on in the project. By doing this, we have prepared the necessary space and layout for easier and more precise positioning of elements.

    .wrapper {
      display: flex;
      align-items: center;
      justify-content: center;
      position: relative;
    }
    CSS

    How to create a realistic water drop effect in CSS

    Firstly, we must select a color for the body, setting the background-color. Keep in mind that the same color must be used for the drop. Therefore, it is crucial to choose the color carefully. 🌈

    Then, we need to establish the dimensions and borders. Thus, we will set the width and height and then adjust the border-radius property to create a slightly rounded shape.

    Next, we maintain the body’s background-color by adjusting the transparency of the drop. In that way, our drop inherits the color of the body.

    Additionally, we add the box-shadow property which is the most crucial part as it is responsible for creating a realistic-looking drop that appears three-dimensional. Without it, the element may appear flat and lacking in depth. 😉 So, don’t be afraid to experiment with different shadow settings until you find the right combination that works for you. It’s truly amazing how much of a difference a small tweak-change can make!

    body {
      background-color: #5fc8e8; /* a light shade of blue */
    }
    
    .wrapper {
      ...
    }
    
    .drop {
      width: 210px;
      height: 220px;
      
      background-color: transparent;
      border-radius: 45% 50% 45% 55%;
      
      box-shadow: -2px -2px 10px 5px #0f9dc4, /* all around */
      5px 5px 10px #0796c1, /* top & right */
      inset 15px 15px 30px #0796c1, /* inset top & right */
      inset -29px -20px 50px #f2f4f7; /* inset left & bottom */
    }
    CSS

    This is what is rendered on the screen. For now, we continue to ignore the text and focus on our almost-ready 🙃 amazing drop!

    Adding shine to the water drop

    By utilizing the CSS properties :before and :after, we can create two new pseudo-elements without the need for additional HTML markup. This technique can be used to create a glancing effect. Both pseudo-elements have the same properties but with different values, which helps to differentiate them from each other and create a unique look.

    We can effortlessly position those glances within the drop element by using the CSS property position: absolute.

    Then, we specify their dimensions and the color by setting the width, height and background-color.

    Next, we modify the shadows and their shape by setting the box-shadow and border-radius .

    After that, we use the top and left properties to position them. Finally, to make them look even more realistic, we can add some rotation by using the transform: rotate() property.

    .drop {
      ...
      position: absolute;
    }
    
    .drop:before {
      position: absolute;
      content: "";
      width: 14%;
      height: 7%;
      background-color: #f5f5f5;
      box-shadow: 0 0 10px #f5f5f5;
      border-radius: 60% 45% 50% 60%/60% 45% 60% 50%;
      top: 15%;
      left: 20%;
      transform: rotate(140deg);
    }
      
    .drop:after {
      position: absolute;
      content: "";
      width: 10%;
      height: 5%;
      background-color: #fff;
      box-shadow: 0 0 10px #fff;
      border-radius: 50% 45% 45% 60%/50% 50% 40% 55%;
      top: 25%;
      left: 10%;
      transform: rotate(-45deg);
    }
    CSS

    After adding these glances, we can see our final drop. 💦 ✨ It’s pretty cool, isn’t it?

    Adjusting text appearance beneath the water drop effect

    We are moving forward by adjusting our text and making it more visually appealing. I have selected the “Lobster” font-family with a font size of 140 pixels, a warm dark orange, and a black 2px text-shadow.

    In case we wish to use another font-family than the usual ones, we need to insert it into our CSS. We do so by setting the @import url(...). Ensure this statement is placed at the beginning of your CSS code snippet, just like I did. Check below. 👇

    @import url('https://fonts.googleapis.com/css?family=Lobster');
    
    .text {
      font-family: "Lobster", sans-serif;
      font-size: 140px;
      color: #e85a5a; /* a shade of warm dark orange */
      text-shadow: 2px 2px 2px black;
    }
    CSS

    Take a closer look now. The text appears so different behind the water drop. It’s quite impressive, don’t you think? 😎

    One last step, and we are ready! We are free to place our text wherever we want by setting the position: absolute , top and left CSS properties in order to move it.

    We also have full control over the placement of our text. This can be achieved by confidently adjusting the CSS properties position: absolute , top and left.

    .text {
      ...
      position: absolute;
      top: -45px;
      left: -35px;
    }
    CSS

    The final step is complete! Congratulations! 🥳 You have the option to either keep the original design or create your own. Please remember that the most important thing is to combine the appropriate colors 🌈 and shadows; this is necessary for our drop to look realistic.

    Full CSS water drop effect code (copy & paste ready)

    Below is the full code referenced in this blog post. Feel free to copy and use it in your own projects. If you have any questions or encounter any issues, don’t hesitate to reach out for assistance. You can easily copy the desired code snippet by clicking on the copy icon, located in the top-right corner of each snippet.

    <div class="wrapper">
      <div class="text">drop</div>
      <div class="drop"></div>
    </div>
    HTML
    @import url('https://fonts.googleapis.com/css?family=Lobster');
    
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html,
    body {
      height: 100vh;
    }
    
    body {
      background-color: #5fc8e8;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .wrapper {
      display: flex;
      align-items: center;
      justify-content: center;
      position: relative;
    }
    
    .text {
      position: absolute;
      font-family: "Lobster", sans-serif;
      font-size: 140px;
      color: #e85a5a;
      text-shadow: 2px 2px 2px black;
      top: -45px;
      left: -35px;
    }
    
    .drop {
      width: 210px;
      height: 220px;
      background-color: transparent;
      border-radius: 45% 50% 45% 55%;
      box-shadow: -2px -2px 10px 5px #0f9dc4, /* all around */
      5px 5px 10px #0796c1, /* top & right */
      inset 15px 15px 30px #0796c1, /* inset top & right */
      inset -29px -20px 50px #f2f4f7; /* inset left & bottom */
      position: absolute;
    }
    
    .drop:before {
      position: absolute;
      content: "";
      width: 14%;
      height: 7%;
      background-color: #f5f5f5;
      box-shadow: 0 0 10px #f5f5f5;
      border-radius: 60% 45% 50% 60%/60% 45% 60% 50%;
      top: 15%;
      left: 20%;
      transform: rotate(140deg);
    }
    
    .drop:after {
      position: absolute;
      content: "";
      width: 10%;
      height: 5%;
      background-color: #fff;
      box-shadow: 0 0 10px #fff;
      border-radius: 50% 45% 45% 60%/50% 50% 40% 55%;
      top: 25%;
      left: 10%;
      transform: rotate(-45deg);
    }
    CSS
    Expand
  • How To Create Stunning Vibrant Neon Effect – CSS Only

    How To Create Stunning Vibrant Neon Effect – CSS Only

    The neon effect in web design brings that bold, electric vibe that grabs attention right away. ⚡🌈 Inspired by real-life neon signs, which I’m sure you have seen outside bars, arcades, or glowing up city streets, it mixes retro charm with a modern twist. It adds personality and mood, turning plain text or shapes into glowing, eye-catching pieces. That soft glow, the depth, those bright color fades — they take a simple design and make it feel alive, as if you have just travelled back in time. 🌟 And the best part? You can pull it off with just HTML and CSS. No images, no JavaScript. 🥳

    Setting the stage: Basic HTML structure

    To begin creating this effect, we’ll start with a basic HTML structure and add custom CSS later. Think of the HTML <div> we are using, as the frame for a neon sign, and the text as the glowing message inside it.

    <div class="neon-effect">Neon Vibes Simple Css</div>
    HTML

    From there, we move to CSS structure. We import a Google font named “Orbitron”, a futuristic style text, that fits perfectly for our neon effect.

    Then we lay the foundation: we structure the page by setting the body to fill the entire viewport and using display: flex to center our neon box both vertically and horizontally. Finally, we add a solid black background — it doesn’t look like much yet, but it sets the perfect stage for the neon glow we’ll add soon, creating strong contrast just like a real sign glowing in the dark.

    @import url('https://fonts.googleapis.com/css?family=Orbitron');
    
    body {
      max-width: 100%;
      height: 100vh;
      
      display: flex;
      align-items: center;
      justify-content: center;
      
      background-color: #000;
    }
    CSS

    Building the neon sign base

    The .neon-effect class is used to create the main structure of the glowing sign. It defines a rectangular box with a width of 700px and a height of 300px, along with a soft, rounded border in light violet (#f4cbf8).

    The box uses display: flex to perfectly center the text both vertically and horizontally. The font is set to Orbitron, giving the text a bold, futuristic style, and its size is large (80px) to make it stand out.

    .neon-effect {
      /* Frame */
      width: 700px;
      height: 300px;
      border: 6px solid #f4cbf8; /* light violet */
      
      border-radius: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      
      /* Fonts */
      font-family: 'Orbitron', Arial;
      font-size: 80px;
      text-align: center;
      color: #faf0fb; /* light pink */
    }
    CSS
    Building the neon sign base (frame and text) to create the neon effect.

    The text color is a very light pink (#faf0fb), which will work beautifully once we add glowing shadow effects around it. For now, this creates a clean and centered base — the canvas for our neon sign.

    Glowing borders: Neon frame with box-shadow

    To create the glowing neon frame around the box, we use layered box-shadow effects. The outer shadows softly expand in multiple directions, starting with white and transitioning through vivid shades of pink, magenta, and purple. This gives the impression of light softly spreading from the edges.

    We also include a subtle touch of yellow-gold hue, blending into the glow. This not only adds visual warmth but also gives a nostalgic nod to vintage neon signs, bringing up the charm of the old days while keeping the overall look fresh and vibrant.

    .neon-effect {
      ...
      box-shadow:
        /* outer glow */
        0 0 2px #fff, /* white */
        0 0 6px #ff99ff, /* light pink */
        0 0 12px #ff33ff, /* magenta */
        0 0 20px #cc00ff, /* violet */
        0 0 28px #9900cc, /* deep purple */
        0 0 36px #660066, /* dark purple */
        0 0 40px #ffe187, /* light yellow - soft gold */
        /* inner glow */
        inset 0 0 4px #ff99ff, /* light pink */
        inset 0 0 8px #ff33ff, /* magenta */
        inset 0 0 14px #cc00ff, /* violet */
        inset 0 0 18px #9900cc, /* deep purple */
        inset 0 0 24px #660066; /* dark purple */
      }
    CSS

    Equally important is the second group, which uses the inset keyword and adds an inner glow that shines inside the box. These inset shadows create depth and give the illusion that the light is not only around the sign, but also glowing from within. The combination of outer and inset shadows is essential for achieving a rich, realistic neon effect.

    Styling the neon frame with pink, magenta, purple and yellow-gold hues setting the box shadow CSS property.

    Illuminated Text: Neon Glow with Text-Shadow

    To create the glowing effect around the text, we use the same method, multiple layers of text-shadow. Each shadow in your code adds a soft light in a different shade: from white and light pink to vibrant magenta, deep purple.

    .neon-effect {
      ...
      text-shadow:
        0 0 2px #fff, /* white */
        0 0 10px #ff99ff, /* light pink */
        0 0 20px #ff33ff, /* magenta */
        0 0 26px #cc00ff, /* violet */
        0 0 24px #9900cc, /* deep purple */
        0 0 32px #660066; /* dark purple *
    }
    
    CSS

    These layered shadows build a rich glow around each letter, giving it a strong, radiant presence against the dark background. The variation in color and blur size creates depth, making the text appear as if it’s lit from within, just like real neon tubing.

    Styling the neon text with pink, magenta and purple hues setting the text shadow CSS property.

    Neon effects in web design are a powerful way to grab attention. ✨ They combine vivid colors, glowing shadows, and sharp contrast to mimic the iconic look of neon signs. Whether you’re aiming for a modern tech vibe or a nostalgic retro feel, neon brings style and energy. Using CSS alone, we can recreate this classic visual with precision and creativity — no electricity needed! 💡😎

  • CSS Grayscale Filter – A Complete Guide

    CSS Grayscale Filter – A Complete Guide

    Hello everybody! 😃 In the vibrant world of web design, the CSS grayscale filter stands as a powerful tool for transforming the visual appearance of images and elements on a webpage. The CSS filter: grayscale(%) works like a magic wand. 🪄 It takes colorful images or elements and converts them into grayscale shades. This effect is great for creating different aesthetics, whether you’re aiming for a vintage charm or a sleek, modern look. The CSS grayscale filter can be applied to various elements, including images, SVGs, emojis, font icons, and even HTML elements.

    Let’s explore what makes this filter so enchanting! 🧙‍♀️

    Grayscale on HTML elements

    First of all, it’s important to note that the CSS grayscale filter may not be noticeable on black and white elements. Since grayscale shifts colors toward shades of gray, and black is already the darkest shade, while white stays the lightest one. Secondly, always keep in mind that the filter: grayscale(100%) can make text completely dark, which may not be ideal when placed on black or very dark backgrounds.

    Using the CSS grayscale filter on fonts

    We will begin by applying grayscale to fonts. In the following example, I used blue text, instead of the default black to emphasize the contrast and make the transition to shades of gray more noticeable.

    This reinforces an important point we discussed earlier—this effect stands out most when applied to bright, colored elements rather than dark, black, or near-black ones. 😉

    <h1>The grayscale filter</h1>
    HTML
    h1 {
      color: blue;
      filter: grayscale(50%);
    }
    CSS

    With the HTML code, we define an <h1> heading element, then CSS sets its text color to blue and applies a grayscale filter to it. Initially, we use 50% intensity, creating a visual effect where the blue text is converted to grayscale while still retaining a subtle hint of its original blue color.

    To clarify, there is another example where we use the filter at 90%, resulting in the title appearing in different shades of gray. This time, it’s very close to being completely dark gray—almost black, we could say.

    This image shows a blue text.
    without grayscale(%)
    This image displays blue text with the CSS grayscale filter set to 50%, partially desaturating the blue color into varying shades of gray.
    with grayscale(50%)
    This image displays blue text with the CSS grayscale filter set to 90%, turning it into grayish shades while retaining 10% of the original blue color.
    with grayscale(90%)

    Grayscale on background color

    In this case, instead of setting the text color, we will set the background-color. Now, take a look at the orange. In the first image, we see a bright orange, while in the second, the color appears slightly darker (grayish) due to the filter: grayscale(40%) property.

    <h1>The grayscale filter</h1>
    HTML
    h1 {
      background-color: orange;
      filter: grayscale(40%);
    }
    CSS
    This image displays black text on an orange background color.
    without grayscale(%)
    This image displays black text on an orange background with the CSS grayscale filter set to 50%, converting the background into grayish shades while reducing its original color intensity.
    with grayscale(40%)

    Grayscale on other HTML elements

    We can also apply the grayscale filter to various HTML elements, such as <span>, <li>, and more. Below, I’ve provided two examples for clarity: the first applies the filter to the content of two <span> elements, while the second applies it to the bullets of an <li>.

    Example using a span

    <div class="grayscale-elements">
      <span>"</span> Happy Coding! <span>"</span>
    </div>
    HTML
    .grayscale-elements {
      color: orange;
    }
    
    .grayscale-elements span {
      filter: grayscale(80%);
    }
    CSS

    In the HTML snippet, we create a <div> element with the class .grayscale-elements. Inside this <div>, we add two <span> elements, each containing a double quote to frame the text “Happy Coding!” These <span> elements allow us to style the quotes differently from the rest of the text.

    In the associated CSS code, .grayscale-elements is targeting elements with this class. The text color of the elements with this class is set to orange. Additionally, the CSS rules target the <span> elements inside and apply an 80% grayscale filter to them, rendering the quotes in shades of gray while retaining the orange color for the rest of the content.

    This image displays orange text enclosed in quotation marks.
    without grayscale(%)
    This image displays orange text enclosed in quotes, with the quotes inside <span> elements. The spans have the CSS grayscale filter set to 80%, converting the orange color into grayish shades while retaining 20% of its original hue.
    with grayscale(80%)

    Example using a ul

    <ul>
      <li>HTML</li>
      <li>CSS</li>
      <li>Javascript</li>
    </ul>
    HTML
    ul {
      list-style: none; /* remove the default bullets */
    }
    
    li {
      color: magenta; /* change lists default (black) color */
    }
    
    li:before {
      content: "•"; /* add new bullets and now can be manipulatated */
      margin-right: 10px; /* add distance between the bullet and the text */
      filter: grayscale(50%); /* one of our examples */
    }
    
    CSS

    In the HTML snippet, we create an unordered list (<ul>) containing three list items (<li>).

    In the associated CSS, we target the <ul> element and remove the default bullets using list-style: none. Then, we style the list items (<li>), setting their text color to magenta.

    The :before pseudo-element is used to add custom bullets before each list item. In this case, the bullet is a solid circle (•) created using the content property. The margin-right property adds spacing between the bullet and the text.

    Additionally, a grayscale effect is applied to the bullets using filter: grayscale(50%), reducing their original color intensity by 50%. This results in bullets appearing in varying shades of gray while still retaining some color.

    For an even more pronounced effect, I also applied a 90% grayscale filter (filter: grayscale(90%)), making the bullets appear even closer to a true gray. 😃

    This image displays an unordered list with three list items, all styled in magenta color.
    without grayscale(%)
    This image shows an unordered list with three list items inside. The list items have color magenta while a grayscale effect is applied to the bullets using filter: grayscale(50%), rendering them with a 50% intensity of grayscale.
    with grayscale(50%)
    This image displays an unordered list with three magenta-colored list items. The bullets have the CSS grayscale filter set to 90%, making them appear in grayish shades.
    with grayscale(90%)

    CSS Grayscale on images

    Another way to take advantage of this CSS property is by applying the filter: grayscale to images, transforming their colors into shades of gray. This is particularly useful for making pictures look old-fashioned or monochromatic when they create a black and white effect.

    <div class="grayscale-image"><div>
    HTML
    .grayscale-image {
      background-image: url(<your-image>);
      background-repeat: no-repeat;
      background-size: cover;
      width: 300px;
      height: 400px;
      filter: grayscale(70%);
    }
    CSS

    The given code creates an HTML div element with the class .grayscale-image.

    This class is styled using CSS to display a grayscale image. The background of the div is set to an image from the provided URL using the background-image property. The image is styled to not repeat (background-repeat: no-repeat) and to cover the entire area (background-size: cover). Its dimensions are set to width of 300 pixels and height of 400 pixels. Finally, a grayscale filter is applied to the image using the filter property, with a 70% level of grayscale.

    This image displays a photo with vibrant colors, including shades of pink, gold, and green.
    without grayscale(%) – Original photo by Katie Harp on Unsplash
    This image displays a photo with colors close to pink, gold, and green, with the CSS grayscale filter set to 70%, partially desaturating the colors into grayish tones.
    with grayscale(70%)

    I did something similar for the following image but set the grayscale to 100%, which transformed the image entirely into shades of gray. Impressive, isn’t it? 🙃

    This image displays a photo of a brown building with the sky in the background and an airplane flying overhead.
    without grayscale – Original photo by Sorasak on Unsplash
    This image displays a photo of a brown building and the sky with an airplane. The CSS grayscale filter is set to 100%, converting the entire photo into grayscale shades.
    grayscale(100%)

    CSS Grayscale on SVG

    The grayscale filter can be applied to SVG elements too, making them appear in varying shades of gray. This is often used to achieve a specific artistic or design effect. For instance, we applied this filter to an SVG of a moon, which was originally yellow, transforming it into a soft gray and adding a unique visual dimension. Awesome, right?

    svg {
      filter: grayscale(80%);
    }
    CSS

    In our CSS code, we apply a filter with a specified grayscale percentage of 80%. This effect transforms the originally yellow image into varying shades of gray while preserving a subtle hint of its original color.

    🔖 Remember that directly targeting the <svg> element may cause conflicts 🌩 ⚡ if multiple SVGs exist in the document.

    This image displays a yellow moon against a dark background.
    without grayscale(%)
    This image displays a yellow moon with the CSS grayscale filter set to 80%, partially desaturating the colors into grayish shades while retaining 20% of the original hue.
    with grayscale(80%)

    CSS Grayscale in Font Awesome

    Another way to use this CSS trick is with Font Awesome icons. In the example below, we will see how to do it the correct way. Since Font Awesome icons start off as black, it’s important to apply color before adding the grayscale effect.

    <i class="fa-solid fa-camera-retro"></i>
    
    HTML
    .fa-camera-retro {
      color: red;
      filter: grayscale(70%);
    }
    
    /* OR - if you want a more generic rule */
    
    i {
      color: red;
      filter: grayscale(70%);
    }
    CSS

    In this HTM snippet, we have an <i> element with the class .fa-solid .fa-camera-retro from the Font Awesome library.

    The corresponding CSS styles .fa-camera-retro, setting the icon’s color to red and applying a 70% grayscale effect. Additionally, we can also target the <i> element directly for a more generic customization.

    🔖 Remember, if you use the Font Awesome .fa-solid class for multiple icons or style them via <i>, all instances will inherit the same appearance, affecting every <i> element in the document.

    This image displays a red retro camera icon from Font Awesome.
    without grayscale
    This image displays a red retro camera icon from Font Awesome with the CSS grayscale filter set to 70%, making the red appear much darker, almost brown.
    with grayscale(70%)

    CSS Grayscale on emoticons using pseudo-elements

    Next, we move on to the beloved emoticons (like 🤗, 😇, etc.). It’s a cool way to make them look a bit different! Below I crafted an example to give a clearer explanation. I chose two of my favorite emoticons 👩‍🎤 🧚‍♀️. Feel free to choose your personal favorite and enjoy the experience!! Always remember that the more vivid the colors, the stronger and more noticeable the effect will be! Good luck! 🌟 🥳

    <div class="grayscale-emoticons"></div>
    HTML
    .grayscale-emoticons:before {
      content: "👩‍🎤 🧚‍♀️";
      filter: grayscale(40%);
      font-size: '50px';
    }
    CSS

    The HTML snippet creates a div element with the class .grayscale-emoticons.

    The linked CSS code, .grayscale-emoticons uses the :before pseudo-element to insert content. The inserted content consists of two emojis: “👩‍🎤” and “🧚‍♀️.” Additionally, a grayscale filter of 40% is applied, giving the emojis a subtle grayscale effect, displaying them in varying shades of gray while retaining some of their original colors.

    I also included another example with a 100% grayscale filter, which completely removes all colors, rendering the emojis entirely in grayscale.

    This image shows two colorful emoticons.
    without grayscale
    This image shows two colorful emoticons with a grayscale filter of 40%. As a result the emoticons are now displayed in varying shades of gray with 40% intensity of the original colors.
    with grayscale(40%)
    This image shows two colorful emoticons with a grayscale filter of 100%. As a result the emoticons are now displayed only in shades of gray.
    with grayscale(100%)

    Using shades of gray adds a nice touch, but if you want to go the extra mile 🏃‍♀️, try adding shadows to elements for a more realistic look. Building on our previous example, I’ve incorporated the fantastic drop-shadow CSS property to achieve this. Pretty cool, right! 😃

    .grayscale-emoticons:before {
      content: "👩‍🎤 🧚‍♀️";
      filter: grayscale(40%) drop-shadow(2px -1px 1px #1d1e22);
      font-size: 100px;
    }
    CSS
    This image displays two colorful emoticons with the CSS grayscale filter set to 40%, partially desaturating their colors. They also feature a 2px -1px 1px gray shadow.
    filter: grayscale(40%) drop-shadow(2px -1px 1px #1d1e22);
    This image shows two colorful emoticons with a grayscale filter of 100%. As a result the emoticons are now displayed in various shades of gray. They also have a 2px -2px 2px gray shadow.
    filter: grayscale(100%) drop-shadow(2px -2px 2px #1d1e22);

  • CSS Conic Gradient Made Easy

    CSS Conic Gradient Made Easy

    Hello! 😃 CSS conic gradient techniques are a fantastic way to add colorful, creative flair to your designs. Today, we’re diving into the enchanting world of conic-gradient in CSS — a type of gradient where colors transition in a circular or conical pattern around a central point (either the default center or one you define within the element).

    This powerful technique allows you to blend and combine colors in unique ways, creating artistic and visually striking effects. Plus, it’s perfect for building things like pie charts for presentations — no extra libraries needed!

    In addition to conic-gradient CSS also offers linear-gradient and radial-gradient techniques. 🌈✨ They all have a starting and an ending point, giving us the flexibility to control the direction and flow of the gradient. It is essential to blend at least two colors, but we can blend even more, creating smooth transitions or distinct shifts between them based on our needs.

    Ready to unlock 🗝 the art and practical uses of conical gradients? Let’s jump into this creative adventure! 💻

    What is a conic gradient?

    A CSS conic gradient is a styling technique used in CSS to create a color transition that radiates from a central point in a circular or conical pattern. It allows you to specify different color stops and angles, leading to a visually pleasing gradient effect for backgrounds or other elements on a webpage.

    There are many variations and creative ways to use conic gradients — and we’ll be exploring them in the next sections!

    Understanding the default direction of a CSS conic gradient

    So, let’s begin with the default direction, which is clockwise. To create a conic gradient, we need at least two colors. Below, I’ve prepared some examples to show exactly what I mean.

    /* blend two colors */
    
    .conic-two-colors {
      background-image: conic-gradient(red, green);
    }
    
    CSS
    Default CSS conic gradient with two colors, red and green
    /* blend many colors */
    
    .conic-multiple-colors {
      background-image: conic-gradient(red, green, blue, magenta, yellow, purple, orange);
    }
    
    CSS
    Default conic gradient with many colors.
    /* blend black and white */
    
    .conic-black-white {
      background-image: conic-gradient(black, white);
    }
    
    CSS
    Default conic gradient with only black and white.

    🔖 Keep in mind that if we want to achieve a seamless transition between the last and first color, we have two ways to achieve this:

    • First, we can choose intermediate colors between the starting and ending colors. For example, in our case, we can pick colors from #FF4C00 (orange) to #FF6E00 (a reddish shade), creating a smooth blend from orange to red.
    • The second option, which is simpler, involves repeating the same color at both ends. In this example, we’ll use red as the repeated color.

    Here’s a simple code snippet to help illustrate this.

    /* using specific colors when blending */
    .conic-smooth-option-a {
      background-image: conic-gradient(red, green, blue, magenta, yellow, purple, orange, #FF4C00, #FF6E00);
    }
    
    /* using the same color at both ends */
    .conic-smooth-option-b {
      background-image: conic-gradient(red, green, blue, magenta, yellow, purple, orange, red);
    }
    
    CSS

    Positioning the center of a CSS conic gradient

    Move the center to the sides

    In CSS, conic gradients provide the flexibility to reposition the gradient’s center anywhere within the container. This enables precise alignment with a specific side, whether or not you use percentage values (%) or define specific color stops.

    In the following examples, we’ll focus on positioning the gradient center along different sides of the container.

    /* Gradient starting from the left */
    
    .conic-left-point {
      background-image: conic-gradient(at left, red, green, blue, purple, orange);
    }
    
    CSS
    Conic gradient with many colors. The center is at the left side of the square.
    /* Starting point at 75% from the left */
    
    .conic-using-percentage {
      background-image: conic-gradient(at left 75%, red, green, blue, purple, orange);
    }
    
    CSS
    Conic gradient with many colors. The center is at the 75% of the left side of the square.
    /* Gradient starting at 75% left with defined color stops */
    
    .conic-specific-stops {
      background-image: conic-gradient(at left 75%, red 5%, green 10%, blue 15%, purple 20%, orange 25%);
    }
    
    CSS
    Conic gradient with many colors and specific color stops. The center is at the 75% of the left side of the square.

    Move the center to the corners

    Building on the previous section, we can also move the gradient’s center to the corners of the container. Conic gradients in CSS allow this kind of precise positioning, with or without using color stops, giving you even more layout control.

    Without color stops

    The following code snippet shows a gradient that starts from the center point of our element with red color, transitions to green, then blue, followed by purple, ending with orange, creating a smooth, circular gradient effect.

    .conic-default {
      background-image: conic-gradient(red, green, blue, purple, orange);
    }
    CSS

    Below we can see how the previous gradient can break into different pieces the position we choose for the gradient’s center.

    The CSS code snippet .conic-corner-bottom-right starts at the bottom right corner of our element and smoothly transitions through red, green, blue, purple, and orange creating a visually appealing color blend.

    .conic-bottom-right-corner {
      background-image: conic-gradient(at bottom right, red, green, blue, purple, orange);
    }
    CSS
    CSS Conic-gradient where the center is at the bottom right corner.

    The reason you’re only seeing the purple and orange colors is due to the angle of the gradient in relation to the element.

    I followed the same steps with .conic-bottom-left-corner, and as the angle changes, the red and green colors become visible. The same goes for .conic-top-right-corner, where we can now see shades of blue and purple. Finally, by setting the center to the top left using .conic-top-left-corner, green and blue come into view. How cool is that! 🥳

    .conic-bottom-left-corner {
      background-image: conic-gradient(at bottom left, red, green, blue, purple, orange);
    }
    CSS
    CSS Conic-gradient where the center is at the bottom left corner.
    .conic-top-right-corner {
      background-image: conic-gradient(at top right, red, green, blue, purple, orange);
    }
    CSS
    Conic-gradient where the center is at the top right corner.
    .conic-top-left-corner {
      background-image: conic-gradient(at top left, red, green, blue, purple, orange);
    }
    CSS
    Conic-gradient where the center is at the top left corner.

    Imagine that changing the center position and adjusting the angle is like zooming in on the gradient — revealing each quarter of the circle more closely, one at a time. 🔍 It’s like examining the gradient through a magnifying glass as you explore each corner!

    With color stops

    Let’s take a look at the following example, where color stops are used to better control how and where each color appears within the conic gradient.

    Color stops let you define specific points along the gradient circle where a color should begin or end. This gives you greater precision over the flow of colors, rather than relying on an even, automatic blend. For example:

    .conic-color-stops {
      background-image: conic-gradient(red 75%, green 80%, blue 90%, purple 95%, orange 100%);
    }
    CSS
    CSS conic-gradient with color stops — red to 75%, then green, blue, purple, and orange up to 100%, creating sharp transitions in a circular layout.

    In this case, red takes up the majority of the gradient — up to 75% — and the remaining colors (green, blue, purple, and orange) appear in quicker succession toward the end.

    We can also combine color stops with center positioning to control not only when colors appear but also where they start. For instance:

    .conic-bottom-right-stops {
      background-image: conic-gradient(at bottom right, red 75%, green 80%, blue 90%, purple 95%, orange 100%);
    }
    CSS
    Conic-gradient with many colors and specific color stops. The center is at the bottom right corner.
    .conic-bottom-left-stops {
      background-image: conic-gradient(at bottom left, red 75%, green 80%, blue 90%, purple 95%, orange 100%);
    }
    CSS
    Conic-gradient with many colors and specific color stops. The center is at the bottom left corner.

    These examples show how positioning the gradient center at different corners—while using color stops—can dramatically affect which parts of the gradient are visible. You’ll notice that the dominant red still takes the lead, but the visibility of the other colors shifts depending on the corner you choose.

    Moving the center inside the element

    We continue with a really powerful feature of conic gradients: the ability to position the center anywhere inside the element. This opens up even more creative possibilities — and of course, you’re free to use color stops with these custom center positions as well.

    Below, I’ve prepared two examples.

    In the first one, the gradient starts from a point located 30% across and 55% down the element. It then smoothly transitions through different colors: red, green, purple, magenta, yellow, and orange. Imagine a color wheel starting from red and going around in order, stopping at each of these colors along the way.

    .conic-center-30-55 {
      background-image: conic-gradient(at 30% 55%, 
      red, green, purple, magenta, yellow, orange);
    }
    CSS
    Conic-gradient with many colors where the center is at 30% across and 55% down.

    In the second one, the gradient starts from a point 30% across and 55% down the element — but this time, it uses color stops to control exactly where each color appears.

    Then it transitions through different color stops: red 15%, green 30%, purple 45%, magenta 60%, yellow 90%, and finally orange at 100%. You can use my example or you are free to create your own. I chose these values based on what felt right to me—but feel free to experiment and create your own version. It’s totally up to you

    .conic-center-30-55-stops {
      background-image: conic-gradient(at 30% 55%, 
      red 15%, green 30%, purple 45%, magenta 60%, yellow 90%, orange 100%);
    }
    CSS
    Conic-gradient with many colors and specific color stops. The center is at 30% across and 55% down.

    Changing the starting angle of a CSS conic gradient

    Let’s move forward and explore another option we have another option available with conic gradients, as we are free to start the gradient from different points.

    In our example the gradient starts at 45 degrees to the right, moving from red to blue. This means the colors change smoothly in a circular motion, like a slice of pie with red at the starting edge and blue at the finishing point.

    .conic-angle-45 {
      background-image: conic-gradient(from 45deg, red, blue);
    }
    CSS

    CSS conic gradient without blending

    Another option is to use a non-blend gradient technique. In the following code snippet, the gradient starts with the color red at the top of the circle and makes sharp, sudden transitions to the next colors — green, blue, purple, and finally orange. Each color occupies a distinct 20% slice of the circle, creating a step-like, segmented pattern with no smooth blending between sections. It’s like a colorful pie chart made of equal parts..

    .conic-no-blending {
      background-image: conic-gradient(
      red 0%, red 20%, 
      green 20%, green 40%, 
      blue 40%, blue 60%, 
      purple 60%, purple 80%, 
      orange 60%, orange 100%
      );
    }
    CSS

    Creating repeating CSS conic gradients

    The repeating-conic-gradient is a function that is used to repeat the conic gradient. Below are two examples that show just how flexible and creative conic gradients can be.

    1. Repeating every 10%: The gradient originates from the center of the element. It begins with red at the center, then transitions to green at 5%, and to blue shortly after. At 10%, it shifts to a specific pinkish-red color defined by the hex code #ff1064. These color transitions repeat every 10% of the circle, creating a series of concentric slices that cycle outward in a continuous, colorful loop.

    .conic-repeat-10 {
      background-image: repeating-conic-gradient(red, green 5%, blue 5%, #ff1064 10%);
    }
    CSS

    2. Repeating every 25%: In this example, the gradient is centered within the element and begins with green at the starting point. As it radiates outward, it shifts to black at 8%, then to orange at 18%, and finally to a vibrant pinkish-red at 25% (using the hex code #ff1064). These defined color stops repeat every 25% of the circle, creating a looping, segmented pattern that spirals out from the center in consistent, colorful sections.

    .conic-repeat-25 {
      background-image: repeating-conic-gradient(green, black 8%, orange 18%, #ff1064 25%)
    }
    CSS

    What to avoid when creating CSS conic gradients

    Always bear in mind that, to create a harmonious and uniform repeating conic gradient, it’s important to maintain consistent spacing and carefully planned distances between color stops.

    In the image below, we can observe that green, black, and orange are each repeated three times, while the color #ff1064 (a shade of pinkish-red) appears only twice. This happens because the total space occupied by all the defined color stops—green, black, orange, and #ff1064—adds up to 40% of the conic gradient.

    This setup ensures that the entire pattern repeats in a balanced way: the specified colors take up 40% of the gradient, and the remaining 60% of the circle is evenly distributed across the repeated pattern. The result is a visually pleasing and consistent circular repetition.

    The following code snippet defines the color stops and their positions for this repeating conic gradient:

    .conic-avoid-repeating {
      background-image: repeating-conic-gradient(green, black 8%, orange 18%, #ff1064 40%);
    }
    CSS

    Here’s what each part means:

    • green: This is the starting color of the gradient. It begins right at the 0% mark.
    • black 8%: The next color in the gradient is black, and it starts at 8%.
    • orange 18%: Orange follows next, starting at 18% of the circle.
    • #ff1064 40%: Finally, the last color in the gradient is a shade of pinkish-red specified by the hexadecimal color code #ff1064, and it starts at 40% of the circle’s circumference.

    After that, the pattern repeats, starting again from green and continuing through the same sequence.

    Creating amazing pie charts with CSS conic gradient

    The CSS conic function is used to create conic gradient backgrounds. A conic gradient creates a circular gradient, much like a pie chart, with or without different color stops defining different sections of the circle.

    And here’s the fun part: we’re completely free to shape our gradients into perfect circles using CSS! Just apply a border-radius of 50%, and you can create stunning pie-style visuals. 😃

    .pie-chart {
      background-image: conic-gradient(
      red 0%, red 12%, 
      orange 12%, orange 30%, 
      yellow 30%, yellow 48%, 
      green 48%, green 65%, 
      blue 65%, blue 82%, 
      purple 82%, purple 100%
      );
    }
    
    .circle {
      width: 240px;
      height: 240px;
      border-radius: 50%;
    }
    CSS

    Breakdown of the color segments:

    • red 0%, red 12% This pair defines a red segment that starts at 0% and ends at 12% of the circle. This represents the first segment of the conic gradient.
    • orange 12%, orange 30%: Starts right after red and continues to 30%, forming the orange slice.
    • yellow 30%, yellow 48%, green 48%, green 65%, and blue 65%, blue 82% each define their own slices.
    • The last segment, purple 82%, purple 100%, defines the purple segment that starts at 82% and goes all the way to 100% of the circle. This represents the last segment of the conic gradient.

    Each pair of color stops defines a slice of the CSS conic gradient, making it easy to visualize proportions — just like a pie chart!

  • How To Create A Stunning Gray CSS Water Drop

    How To Create A Stunning Gray CSS Water Drop

    Hello there! 😃 In this post, we will guide you drop-by-drop ☔ on how to make a beautiful gray CSS water drop using only CSS. We will take you through the entire process, from the initial design to the final touches, and provide useful tips on how to adjust CSS properties to achieve the perfect shape and reflection effects. By the end, you will have created stunning water drops that will capture everyone’s attention.

    So, let’s begin this adventure and dive in! 🤿

    HTML Structure for water drop

    First, we create an HTML element with a class name that reflects the intended purpose and makes it easily identifiable. In our example, we will use the class name .drop 💦✨.

    <div class="drop"></div>
    HTML

    CSS Structure for a water drop

    We continue working on our CSS code. First, we have to create our drop, then we will add some shine as a way to look more natural, and finally, we will add two more drops with different sizes. Let the game begin! ⏳

    Create the water drop

    We begin by setting the background-color for our body, giving it a light shade of grayish blue (#c9c9db). As drops are transparent, picking the appropriate color as their background is essential.

    Then we define the dimensions and borders of our drop, setting the width and height and adjusting the border-radius property to create a slightly rounded shape. (It’s important to note that nothing will be rendered on the screen at this stage as our drop does not have any color yet. 💬 )

    Next, we work on creating the drop effect using only shadows, which can be quite challenging. To achieve this, we have to make sure that the drop’s background-color is set to transparent.

    body {
      background-color: #c9c9db; /* a light shade of grayish blue */
    }
    
    .drop {
      width: 200px;
      height: 200px;
      
      border-radius: 60% 45% 50% 60%/60% 45% 60% 50%;
      background-color: transparent;
    }
    CSS

    We finalize by adding the box-shadow property to create the CSS water drop effect. We have to play somehow 🎨 🖌 with our color shades till we manage 🤯 to create the perfect result. 💪

    body {
      background-color: #c9c9db; /* a light shade of grayish blue */
    }
    
    .drop {
      ...
      box-shadow: 0 0 10px #8a8a8e, /* top-right-left-bottom */
      5px 5px 10px 5px #929297, /* left & bottom */
      inset 8px 8px 25px #71717a, /* inset top & right */
      inset -8px -8px 25px #f2f2f2; /* inset left & bottom */
    }
    CSS

    Below, we can see what has been displayed on the screen up to this point.

    Adding shine to the CSS water drop

    With the power of CSS, we can bring to life unique and captivating shine ✨ effects using the :before and : after pseudo-elements. By setting different values for the same properties, we can create a distinctive and inspiring look that will leave a lasting impression on our audience.

    To position the shine inside the drop element, we utilize the position: relative to our class and then the position: absolute to our pseudoelements.

    .drop {
      ...
      position: relative;
    }
    
    .drop:before {
      position: absolute;
    
      content: "";
      width: 14%;
      height: 10%;
      background-color: #f5f5f5;
      
      box-shadow: 0 0 10px #fff;
      border-radius: 60% 45% 50% 60%/60% 45% 60% 50%;
      top: 20%;
      left: 25%;
      transform: rotate(-40deg);
    }
    
    .drop:after {
      position: absolute;
    
      content: "";
      width: 10%;
      height: 6%;
      background-color: #f5f5f5;
      
      box-shadow: 0 0 10px #fff;
      border-radius: 60% 45% 50% 60%/60% 45% 60% 50%;
      top: 35%;
      left: 15%;
      transform: rotate(-50deg);
    }
    CSS

    Then, we specify the dimensions and colors for the shine by setting the width, height and background-color.

    Next, we add the box-shadow and border-radius properties that allow us to create smooth and rounded corners on the shiny elements. This can give the design a softer look.

    After that, we use the top and left properties to position them.

    Finally, to make them look even more realistic, we can add some rotation by using the transform: rotate() property.

    In the following image, you can see the changes applied: a shiny, grey water drop!

    Create the rest of the water drops

    We will complete our work by adding two more drops. Each drop will have a different class, depending on its size.

    <div class="drop"></div>
    <div class="drop drop--medium"></div>
    <div class="drop drop--small"></div>
    HTML

    First of all, we add a small margin of 30 pixels between the drops. Then, we continue with adding the drop’s size based on our preferences.

    .drop {
      ...
      margin: 30px;
    }
    .drop--medium {
      width: 120px;
      height: 120px;
    }
    
    .drop--small {
      width: 50px;
      height: 50px;
    }
    CSS

    Here we are! Our stunning water drops are displayed on the screen! Nice, isn’t it? 😎
    (We added some extra styling, not included in our code snippets, to center our drops, in case you are wondering about the last screenshot)

    Hope you found it useful. Utilize these techniques to create stunning water drops that will impress your audience. Have fun experimenting with different colors, shades, and shapes to produce your own unique designs! 💦 ✨ Happy coding!

    Complete code solution

    Below is the full code referenced in this blog post, along with some extra stylings to enhance our final result. Feel free to copy and use it in your own projects. If you have any questions or encounter any issues, don’t hesitate to reach out for assistance in the comments section. You can easily copy the desired code snippet by clicking on the copy icon located in the top-right corner of each snippet.

    <div class="drop"></div>
    <div class="drop drop--medium"></div>
    <div class="drop drop--small"></div>
    HTML
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html,
    body {
      height: 100vh;
    }
    
    body {
      background: #c9c9db;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .drop {
      width: 200px;
      height: 200px;
      border-radius: 60% 45% 50% 60%/60% 45% 60% 50%;
      background-color: transparent;
      box-shadow: 0 0 10px #8a8a8e, /* top-right-left-bottom */
      5px 5px 10px 5px #929297, /* left & bottom */
      inset 8px 8px 25px #71717a, /* inset top & right */
      inset -8px -8px 25px #f2f2f2; /* inset left & bottom */
      
      position: relative;
      margin: 30px;
    }
     
    .drop:before {
      position: absolute;
    
      content: "";
      width: 14%;
      height: 10%;
      background-color: #f5f5f5;
      
      box-shadow: 0 0 10px #fff;
      border-radius: 60% 45% 50% 60%/60% 45% 60% 50%;
      top: 20%;
      left: 25%;
      transform: rotate(-40deg);
    }
    
    .drop:after {
      position: absolute;
    
      content: "";
      width: 10%;
      height: 6%;
      background-color: #f5f5f5;
      
      box-shadow: 0 0 10px #fff;
      border-radius: 60% 45% 50% 60%/60% 45% 60% 50%;
      top: 35%;
      left: 15%;
      transform: rotate(-50deg);
    }
    
    .drop--medium {
      width: 120px;
      height: 120px;
    }
    
    .drop--small {
      width: 50px;
      height: 50px;
    }
    CSS
    Expand
  • Make a Unique Folded Corner Effect Utilizing the clip-path CSS Property

    Make a Unique Folded Corner Effect Utilizing the clip-path CSS Property

    Hello everybody! I’m excited to share a cool folded corner effect I created using just HTML and CSS. This eye-catching design adds a dynamic flair, creating a realistic illusion that enhances your webpage’s visual appeal —no images, extra scripts, or complex code required. How awesome is that? 😎

    Elements with clip-path do not take shadows directly. Instead, they must be nested within a container to inherit its shadows.

    Creating the folded corner effect using pseudo-element

    We’ll start by using the :before pseudo-element combined with the clip-path property to create the folded corner effect. The main element, which contains the pseudo-element, is styled with a filter property to apply a shadow. This shadow is then inherited by the pseudo-element, allowing the folded corner to display a subtle, realistic shadow as well. This technique keeps the design lightweight and visually appealing.

    Basic HTML structure for the folded corner effect

    The HTML code snippet creates a card layout. We have a parent element with the class .card, housing two child elements: the first with the class .title for the card’s title, and the second with the class .folded-corner to apply the folded corner effect via CSS.

    <div class="card">
      <div class="title">Folded Corner Effect</div>
      <div class="folded-corner"></div>
    </div>
    HTML

    CSS structure: creating the paper and cut corner

    Starting with the CSS, we define a horizontal rectangle by setting the width to 450px and the height to 300px, along with a subtle 2px border-radius for slightly rounded corners. The background color is set to #228a90, a rich teal with greenish-blue tones that gives the card a fresh, modern look.

    .card {
      width: 450px;
      height: 300px;
      border-radius: 2px;
      background-color: #228a90;
        
      display: flex;
      align-items: center;
      justify-content: center;
    }
    CSS
    Initial paper design before applying folded corner effect

    Then we use the clip-path property to shape the desired paper corner for the folded effect. I chose the top-left corner, but you can select any one you prefer by adjusting the corresponding points in the clip-path: polygon() function.

    .card {
      width: 450px;
      height: 300px;
      border-radius: 2px;
      background-color: #228a90;
      
      clip-path: polygon(0% 30%, 30% 0%, 100% 0%, 100% 100%, 0% 100%);
      position: relative;
    }
    CSS

    Think of the corners in this order: top-left, top-right, bottom-right, bottom-left (clockwise).

    Remember to split each selected corner into two coordinates to get the right look (watch those 👇 magenta measurements! 😉)
    Take into account that the top-left corner has both Axis-X and Axis-Y on 0%.

    Axis coordinates of card showing top-left corner cut, preparing the folded corner effect

    Finally, adding position:relative does not change something in our effect but prepares us for future positioning adjustments. As for centering our elements using flexbox—it’s purely for aesthetic purposes, helping keep everything visually balanced and neat.
    Below, we can see what is rendered on the screen for now. Pretty cool, right!? 😃

    Progress so far: paper with cut corner created using HTML and CSS folded corner effect

    CSS structure: how to create the folded corner effect

    To continue building our effect, we’ll use the folder-corner child element to create a second rectangle within the parent card element. This element will act as the folded piece of paper. We’ll give it a width of 70px and a height of 160px.

    .card .folded-corner {
      width: 70px;
      height: 160px;
      background-color: red;
      position: absolute;
    }
    CSS

    For now, we’ll use a red background color to help us visualize its position and behavior more clearly—this will be updated to its final color later. We’ll also apply position: absolute to enable precise positioning.

    Initial red-colored container element for folded corner effect

    Next, we continue with positioning. We use the top and left properties to move the .folded-corner element closer to the clipped corner, then apply the transform property to rotate it into place.

    .card .folded-corner {
      ...
      top: -7px;
      left: 52px;
      transform: rotate(56deg);
    }
    CSS
    Positioning folded-corner element at top -7px in folded corner effect layout
    top: -7px;
    Positioning folded-corner element at left 52px to align with cut corner
    left: 52px;
    Rotating folded-corner element by 52 degrees using CSS transform property
    transform: rotate(56deg);

    Finally, adding the filter property is essential for completing our effect. As mentioned earlier, applying a shadow directly to an element with a clip-path isn’t possible — so the solution is to create an additional element that can hold the shadow.

    To do this, we’ll add a :before pseudo-element with the same dimensions as our folded corner. This allows us to recreate the folded shape and apply the shadow to it — adding depth and realism to the folded corner effect. 😎

    .card .folded-corner {
      ...
      top: -7px;
      left: 52px;
      transform: rotate(56deg);
      filter: drop-shadow(6px 4px 5px rgba(0, 0, 0, 0.4));
    }
    CSS
    Initial red-colored container element for folded corner effect
    filter: drop-shadow(6px 4px 5px rgba(0, 0, 0, 0.4));

    Next, we move forward by using the before pseudo-element with the content property and set its position to absolute for precise placement within the parent.
    We want this pseudo-element to have the exact dimensions as the parent, so we set both its width and height to 100%, something that allows it to inherit the parent’s size.

    .card .folded-corner:before {
      content: "";
      position: absolute;
      width: 100%;
      height: 100%;
      background: pink;
      border-radius: 0 0 10% 0;
    }
    CSS

    For now, we apply a pink background to help visualize the structure. Finally, we add a border-radius of 10% to the bottom corner, which softens the edge and creates a smoother, more realistic folded appearance.

    Adding the before pseudoelement

    Next, we replace the pink background with a smooth linear gradient. We choose colors similar to the main hue but make them darker towards the edges and lighter in the center. This gradient effect enhances the appearance by making the center appear brighter and polish. ✨

    .card .folded-corner:before {
      ...
      background: linear-gradient(to right, #277c7f 5%, #5abcbc 30%, #63c5c6 40%, #5abcbc 50%, #277c7f 90%, #0d4749);
    }
    CSS
    Adding linear- gradient at the before pseudoelement

    To complete the shape, we apply the clip-path: polygon(0% -2%, 0% 100%, 100% 100%) property. This is essential for transforming the before pseudo-element —which starts as a rectangle, just like its parent—into a triangle.

    In simple words, this clipping path reshapes the element into the desired triangular form. 🥳

    .card .folded-corner:before {
      ...
      clip-path: polygon(0% -2%, 0% 100%, 100% 100%);
    }
    CSS
    Adding clip-path to the before pseudoelement

    The last step is to turn back to the parent element with the class .folded-corner and “remove” the red rectangle from view by simply replacing the red background-color with a transparent value.
    As we can see, the :before pseudoelement inherits its parent shadow effect while the parent becomes invisible.

    .card .folded-corner {
      width: 70px;
      height: 160px;
      background-color: transparent;
      position: absolute;
      top: -7px;
      left: 52px;
      transform: rotate(56deg);
      filter: drop-shadow(6px 4px 5px rgba(0, 0, 0, 0.4));
    }
    CSS
    Finalize the folded corner effect

    We can enhance the title to have a more eye-cathing result. Applying a white background-color, increasing the font-size, and adding a subtle black text-shadow will make the title stand out beautifully, elevating the overall design. 📄 ✨

    .card .title {
      font-family: sans-serif;
      font-size: 40px;
      font-weight: bold;
      text-align: center;
      padding: 0 50px;
      color: white;
      text-shadow: 1px 1px 1px black;
    }
    CSS
    Stylling paper's title

    Complete code example

    <div class="card">
      <div class="title">Folded Corner Effect</div>
      <div class="folded-corner"></div>
    </div>
    HTML
    .card {
      width: 450px;
      height: 300px;
      border-radius: 2px;
      background-color: #228a90;
      clip-path: polygon(0% 30%, 30% 0%, 100% 0%, 100% 100%, 0% 100%);
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .card .folded-corner {
      width: 70px;
      height: 160px;
      background-color: transparent;
      position: absolute;
      top: -7px;
      left: 52px;
      transform: rotate(56deg);
      filter: drop-shadow(6px 4px 5px rgba(0, 0, 0, 0.4));
    }
    
    .card .folded-corner:before {
      content: "";
      position: absolute;
      width: 100%;
      height: 100%;
      border-radius: 0 0 10% 0;
      background: linear-gradient(to right, #277c7f 5%, #5abcbc 30%, #63c5c6 40%, #5abcbc 50%, #277c7f 90%, #0d4749);
      clip-path: polygon(0% -2%, 0% 100%, 100% 100%);
    }
    
    .card .title {
      font-family: sans-serif;
      font-size: 40px;
      font-weight: bold;
      text-align: center;
      padding: 0 50px;
      color: white;
      text-shadow: 1px 1px 1px black;
    }
    CSS
    Expand

    If you have any questions or run into any issues, don’t hesitate to reach out in the comments below — I’m happy to help. You can easily copy any code snippet by clicking the copy icon in the top-right corner of each block.

    Summary

    We started by creating the paper and cutting its corner. Then, we set the clip-path property to define the shape and positioned the folded-corner element precisely over the clipped area. After that, we enhanced the styling with background gradients that match the paper’s tone, and wrapped up by polishing the effect for a clean, realistic look. 📄 ✨

    Wishing you the best of luck in all your creative endeavors! 😃

  • Keyframes in CSS Explained – Make Your Animations Stand Out

    Keyframes in CSS Explained – Make Your Animations Stand Out

    Hi there! 😃 In today’s post, we will analyze the amazing CSS @keyframes rule, an incredibly powerful tool for creating complex and visually stunning animations on web pages.

    Understanding keyframes in CSS

    In simple terms, keyframes define the animation’s behavior through a series of @keyframes which describes how the animation should change over time.
    Each keyframe represents a specific point in time, and CSS smoothly transitions between these keyframes to create fluid motion.

    With CSS keyframes, you can create various animations, from simple color changes to complex effects like movements and transformations. By combining keyframes with other CSS properties, we can control timing and duration and create dynamic and visually engaging websites.

    I will provide you with an example that will make this at-rule crystal clear. 🧐 Let’s grab a ☕ and move forward. 🚀 We’ve got this!

    Example of keyframes in CSS: The theory behind it

    The details of the animation will depend on the specific effect we want to achieve. Let’s say we want to create an animation where a square ⬛ transforms into a circle ⚫ and then back into a square ⬛ again. Additionally, the color 🌈 of the shape changes during the animation, and there are some movements and transitions involved. It might seem massive and confusing, 😰 but we can deal with it step by step together! 🛠

    For our animation to be smooth and realistic, we need to define the @keyframes. The first keyframe serves as the starting point (0%), followed by intermediate points and the final keyframe (100%) that marks the end of the animation and returns to the starting point.

    😃The following code snippet provides a quick example of a keyframes structure in action.

    @keyframes name-of-keyframe {
      0% {
        ... /* this is the starting point */
      }
      50% {
        ... /* this is one intermediate point */
      }
      100% {
        ... /* this is the ending point */
      }
    }
    CSS

    To create these keyframes, you’ll need to use CSS animation properties, such as animation-name, animation-duration, animation-iteration-count and animation-delay. The animation-name property connects the animation to its keyframes, while the other properties control how long it should last, how it should behave over time, and whether it has a delay before starting..

    Once you’ve defined your keyframes and animation properties, CSS will automatically insert transitions between the keyframes to create smooth changes. This means you don’t need to worry about manual calculations. CSS takes care of it for you. Cool huh!? 😎 ✨

    Example of keyframes in CSS: Practical implementation

    As we already mentioned, we will work with a box. We need to start with the HTML code and create a <div> element that has the class name .box.

    <div class="box"></div>
    HTML

    Then, in our CSS code, we define the basic properties. It should be a square with a width and height of 200 pixels, and we’ll keep the background-color simple by using black for now.

    .box {
      width: 200px;
      height: 200px;
      background-color: black;
    }
    CSS

    This is what is rendered on the screen for now. A simple black box.

    Applying animation effects to the elements

    To add animation effects to our <div> element, we need to define the animation properties of our class .box. We set the animation-name, which is the animation’s name, then the animation-duration, which is the time the animation takes to complete, and finally, the animation-iteration-count which declares how many times the animation should repeat.

    .box {
      ...
      animation-name: my-animation;
      animation-duration: 10s;
      animation-iteration-count: infinite;
    }
    
    /* ----- MY ANIMATION ----- */
    @keyframes my-animation {}
    CSS

    Then, we create our animation using the @keyframes rule, where we can specify the values of the CSS properties at different points during the animation. The most crucial step is to link the animation to our element. We do this by using the animation-name property and assigning it to the same name we defined earlier. In our case my-animation.

    Transforming colors and shapes with animation

    We will start our transformations by working with colors and shapes using the background-color and border-radius properties.

    /* ----- MY ANIMATION ----- */
    @keyframes my-animation {
      0% {
        background-color: pink;
      }
      25% {
        background-color: purple;
        border-radius: 50%;
      }
      50% {
        background-color: black;
        border-radius: 50%;
      }
      75% {
        background-color: purple;
        border-radius: 50%;
      }
      100% {
        background-color: pink;
      }
    }
    CSS

    As we can see, it begins with a pink square at starting point: 0%. Then, it transitions to purple (25%) where it starts becoming cyclic. After that, it turns to black (50%) and remains a full circle. Then (75%) shifts to purple again, where it starts transitioning back to a square. Finally (ending point: 100%) turns to a pink square.

    Keyframes in CSS: A pink square transitions into a purple circle, then a black circle, and back to a pink square using CSS @keyframes, background-color, and border-radius properties.

    Animating Element Position

    The next step is adjusting the position of our box. To do this, we need to adjust the positioning of the HTML element associated with our .box class and set it to position: relative, allowing it to move within its container.

    Now, let’s animate its movement:

    • The animation starts at the top-left corner 0%.
    • At 25%, it moves diagonally by changing both the top and left values.
    • At 50%, it continues moving horizontally to the left.
    • At 75%, it moves diagonally back by adjusting top and left again.
    • Finally, at 100%, it returns to its original position at the top-left corner.
    .box {
      ...
      position: relative;
    }
    
    /* ----- MY ANIMATION ----- */
    @keyframes my-animation {
      0% {
        ...
        left: 0px;
        top: 0px;
      }
      25% {
        ...
        left: 200px;
        top: 100px;
      }
      50% {
        ...
        left: 300px;
      }
      75% {
        ...
        left: 200px;
        top: 100px;
      }
      100% {
        ...
        left: 0px;
        top: 0px;
      }
    }
    CSS
    CSS animations: A pink square moves from the top left while transforming into a purple circle, then shifts left and becomes a black circle, before returning to the top left as a pink square using CSS @keyframes, top, and left properties.

    Animating Element Rotation

    We can also make our box a little bit playful! 🤹‍♀️ We will add some extra moves with the amazing transform CSS property.

    We will apply a rotation only between 25% and 75%.

    • The animation starts with no rotation at 0%.
    • At 25%, the element remains unrotated with transform: rotateY(0deg).
    • At 50%, it completes a full rotation with transform: rotateY(360deg)
    • At 75%, it returns to its original position with transform: rotateY(0deg).
    • Finally, at 100%, the cycle resets, ready to repeat.
    /* ----- MY ANIMATION ----- */
    @keyframes my-animation {
      0% {
        ...
      }
      25% {
        ...
        transform: rotateY(0deg);
      }
      50% {
        ...
        transform: rotateY(360deg);
      }
      75% {
        ...
        transform: rotateY(0deg);
      }
      100% {
        ...
      }
    }
    CSS
    CSS animations with @keyframes: This image shows a pink square moving from top left and turning to a purple circle, then moving only to left by rotating at axis-Y and turning to a black circle and then again moving back to top left and turning to a pink square using the CSS @keyframes property and the css transform: rotateY() property.

    Now, let’s try to make it even more impressive by enriching the starting and ending phases.

    • At 0%, we add transform: rotateX(360deg).
    • At 25%, it resets with transform: rotateX(0deg), creating a rotation along the X-axis from 0% to 25%.
    • At 50% and 75%, we maintain a non-rotating phase with transform: rotateX(0deg).
    • Finally, at 100%, we add transform: rotateX(360deg), creating another rotation from 75% to 100%.
    /* ----- MY ANIMATION ----- */
    @keyframes my-animation {
      0% {
        ...
        transform: rotateX(360deg);
      }
      25% {
        ...
        transform: rotateX(0deg);
      }
      50% {
        ...
        transform: rotateX(0deg);
      }
      75% {
        ...
        transform: rotateX(0deg);
      }
      100% {
        ...
        transform: rotateX(360deg);
      }
    }
    CSS
    CSS Keyframes: This image shows a pink square that rotating to axis-X from top left and turning to a purple circle, then moving only to left by rotating at axis-Y and turning to a black circle and then again rotating to axis-X going back to top left and turning to a pink square using the CSS @keyframes property and the css transform: rotateY() property.

    Inserting content into the animated element

    We can also add any content we want (text, image, emoticon, etc.) inside our HTML element. For our example, I use an emoticon and set its font-size to 100px.

    <div class="box">
      <div class="emoticon">🪐</div>
    </div>
    HTML
    .box .emoticon {
      font-size: 100px;
    }
    CSS
    CSS keyframes: This image shows a pink square with a perfectrly centered emoticon (Ringed planet) inside it, that rotating to axis-X from top left and turning to a purple circle, then moving only to left by rotating at axis-Y and turning to a black circle and then again rotating to axis-X going back to top left and turning to a pink square using the CSS @keyframes property. We create this animation setting the CSS animation-name, CSS animation-duration and CSS animation-iteration-count properties.

    Delaying the animation start

    We can add a delay to our animation by setting the animation-delay CSS property. This delay momentarily pauses the animation before it begins, creating a smoother and more controlled start.

    For instance, if we set the delay to 2s, the animation will start after 2 seconds. However, please note that this delay occurs only once when we first see the animation. To observe the effect again, we may need to refresh the page. 

    .box {
      ...
      animation-delay: 2s;
    }
    CSS

    You can see the animation with the delay effect below. It starts with its initial black background-color and after the 2s it starts the transitions we created with the @keyframes. 😉

    CSS Keyframes: This image shows a pink square with a perfectrly centered emoticon (Ringed planet) inside it, that rotating to axis-X from top left and turning to a purple circle, then moving only to left by rotating at axis-Y and turning to a black circle and then again rotating to axis-X going back to top left and turning to a pink square using the CSS @keyframes property. This animation has a starting delay of 2 seconds. We do so using the CSS animation-delay property.

    Using the @keyframes shorthand for animations

    By using shorthand, you can save space and time while improving your code’s readability. Instead of writing multiple properties separately, you can define them all using just the animation shorthand.

    .box {
      animation-name: my-animation;
      animation-duration: 10s;
      animation-iteration-count: infinite;
      animation-delay: 2s;
      
      /* Here’s the shorthand version */
      animation: my-animation 10s 2s infinite;
    }
    CSS

    Complete CSS animation code (copy & paste ready)

    Below is the full code referenced in this blog post. Feel free to copy and use it in your own projects.

    If you have any questions or encounter any issues, don’t hesitate to ask for help. You can easily copy the desired code snippet by clicking on the copy icon, located in the top-right corner of each snippet.

    We’d love to hear your thoughts! If you have feedback or questions, drop a comment below.

    <div class="box">
      <div class="emoticon">🪐</div>
    </div>
    HTML
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html,
    body {
      height: 100vh;
    }
    
    .box {
      width: 200px;
      height: 200px;
      background-color: black;
      
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
      
      animation: my-animation 10s 2s infinite;
    }
    
    .box .emoticon {
      font-size: 100px;
    }
    
    /* ----- MY ANIMATION ----- */
    @keyframes my-animation {
      0% {
        left: 0px;
        top: 0px;
        background-color: pink;
        transform: rotateX(360deg);
      }
      25% {
        left: 200px;
        top: 100px;
        background-color: purple;
        border-radius: 50%;
        transform: rotateY(0deg) rotateX(0deg);
      }
      50% {
        left: 300px;
        background-color: black;
        border-radius: 50%;
        transform: rotateY(360deg) rotateX(0deg);
        visibility: visible;
      }
      75% {
        left: 200px;
        top: 100px;
        background-color: purple;
        border-radius: 50%;
        transform: rotateY(0deg) rotateX(0deg);
      }
      100% {
        left: 0px;
        top: 0px;
        background-color: pink;
        transform: rotateX(360deg);
      }
    }
    CSS
  • CSS Last Child Selector – The Most Valuable Selector

    CSS Last Child Selector – The Most Valuable Selector

    Hello everyone! In this post, we will explore the CSS last child selector, one of the most useful selectors in CSS. We use the CSS last-child selector when we want to target and modify only the last element of the same type. By types of elements, we mean HTML tags like paragraphs (<p>), headings (<h1>, <h2> …), lists (<li>) and others.

    Typically, we use the CSS last-child selector with list items <li>, but it can be applied to any type of element. I have prepared some examples to help you understand how to use the selector based on your needs.

    How to use the CSS last child selector for list elements

    In the example below, the HTML structure includes an ordered list (<ol>) with three list items (<li>), all colored indigo.

    <ol>
      <li>First item</li>
      <li>Second item</li>
      <li>Last item</li>
    </ol>
    HTML
    An ordered list with three items all colored indigo.

    We continue by adding the CSS rule, which selects only the last item and applies a red color to it.

    /* Applies indigo color to all list items */
    li {
      color: indigo; 
    }
    
    
    /* Applies red color only to the last list item */
    li:last-child {
      color: red; 
    }
    
    
    /*       OR       */
    
    li:nth-last-child(1) {
      color: red;
    }
    CSS
    An ordered list with three items all colored indigo apart from the last one which has color red as we used the: CSS last child selector. :last-child OR :nth-last-child(1)

    When using the :last-child selector in CSS, it targets the last element within its parent. When we add a new item to the end of the list, this new item becomes the last child of the list, and the previous first item now becomes the second one from the end. This change causes the :last-child selector to now target the newly added item instead.

    Applying the CSS last child selector to non-list elements

    In the following example, we will use the last child selector for paragraphs. The HTML structure contains a <div> which enclose three paragraph (<p>) elements. By default, all HTML elements have black text color, so paragraphs will appear in black.

    <div>
      <p>First paragraph</p>
      <p>Second paragraph</p>
      <p>Last paragraph</p>
    </div>
    HTML
    Three paragraphs all colored by default black.

    By setting the CSS last child selector, it selects the last <p> element and applies the text color red to a white background. As a result, the last paragraph appears in red with a white background, while the subsequent paragraphs keep their default styling.

    /* Applies red color and background white
       to the last paragraph
    */
    p:last-child {
      color: red;
      background-color: white;
    }
    
    /*       OR       */
    
    p:nth-last-child(1) {
      color: red;
      background: white;
    }
    
    CSS
    Three paragraphs all colored by default black, apart from the last one which has color red and background color white as we used the last child css selector.

    Using the last child selector across multiple parent containers

    In this example, we will examine another case, as many times, we have multiple HTML elements to work with within a document. Here, we have two div elements, each containing two paragraph (<p>) elements colored black.

    <div>
      <p>First paragraph inside the first div</p>
      <p>Last paragraph inside the first div</p>
    </div>
    <div>
      <p>First paragraph inside the second div</p>
      <p>Last paragraph inside the second div</p>
    </div>
    HTML
    Four paragraphs all colored black

    The CSS rule div p:last-child selects the last <p> element within any <div> and applies the specified styles to it.

    /* Applies red color and white background
       to the last paragraph inside each div
    */
    div p:last-child {
      color: red;
      background-color: white;
    }
    
    /*       OR       */
    
    div p:nth-last-child(1) {
      color: red;
      background-color: white;
    }
    
    CSS

    As a result, the last paragraph of each <div> will display in red with a white background.

    Four paragraphs all colored by default black, apart from the second and last one which has color red and background color white.

    Meanwhile, the remaining paragraphs will keep their default styles. This demonstrates how the :last-child selector can be used to style the last element within several parent containers. 😉

    Selecting the last child inside the first parent element

    In this example, we have two <div> elements, each containing two paragraph <p> elements colored black as in the previous one, but this time, we will examine how we can select only the last paragraph inside the first div.

    <div>
      <p>First paragraph in the first div</p>
      <p>Second paragraph in the first div</p>
    </div>
    <div>
      <p>First paragraph in the second div</p>
      <p>Second paragraph in the second div</p>
    </div>
    HTML
    Four paragraphs all colored black

    The CSS rule div:first-child p:last-child selects only the first paragraph <p> within the first div and applies the specified styles to it.

    /* Applies red color and white background
       to the last paragraph of the first div
    */
    
    div:first-child p:last-child {
      color: red;
      background-color: white;
    }
    
    /*       OR       */
    
    div:nth-child(1) p:nth-last-child(1) {
      color: red;
      background-color: white;
    }                    
    CSS

    Therefore, the last paragraph in the first div will be shown in red with a white background, while the second paragraph will remain in its default black color.

    Four paragraphs all colored by default black, apart from the second one which has color red and background color white.

    The second div, is not influenced at all by the styles applied to the first div, demonstrating selective styling implementation. 😉 This method is particularly useful for assigning specific styles to elements in complex layouts.

    Avoiding the last child with the :not(:last-child) selector

    Finally, we will examine the opposite case and how to avoid selecting the last child element. In this example, the HTML structure consists of an ordered list <ol> housing three list <li> elements.

    <ol>
      <li>First item</li>
      <li>Second item</li>
      <li>Last item</li>
    </ol>
    HTML
    An ordered list with three items all colored by default, black.

    The CSS rule ol li:not(:last-child) selects all <li> elements within the ol apart from the last one and apply the specified styles to them.

    /* Applies red color to all paragraphs
       except for the last one
    */
    
    ol li:not(:last-child) {
      color: red;
    }
    CSS

    As a result, all list items except for the last one will be pictured in red, while the last list item will retain its default color, black.

    An ordered list with three items. The last item has by default color black while the other two items have color red as we used the css  not last child selector.

    Alternatively, we can use the CSS nth-last-child selector like this

    ol li:not(:nth-last-child(1)) {
      color: red;
    }
    CSS

    This selector serves a similar purpose, but it is more flexible as it allows for the selection of any child based on its position.

  • What Does span Do in HTML? Unlocking Its Full Potential

    What Does span Do in HTML? Unlocking Its Full Potential

    The <span> tag is a powerful yet often overlooked element in HTML. It’s commonly used for adding style and interactivity, but what does span do in HTML exactly? In this guide, we’ll explore its full potential and show you how to use it effectively on your web pages.

    What is a span tag?

    We use HTML <span> tag to target and manipulate specific 🍰 parts within an HTML 🎂 element without affecting the layout of surrounding elements. In simple terms, it acts as an inline container for styling small portions 🍰🍰 of text.

    <span>....</span>
    HTML

    Why and when to use span in HTML?

    Since <span> does not introduce additional structure or spacing, it is ideal for precise, targeted styling such as:

    • Highlighting text, changing colors, or formatting specific letters, words, or even a small paragraph.
    • Wrapping text for CSS animations or visual effects.

    The difference between span and div

    While both <div> and <span> are used for grouping and styling elements, they behave differently in terms of layout and structure. The table below highlights their key differences:

    span
    div
    inline element
    block element
    stays within same line
    starts new line and takes full width
    does not affect layout
    breaks the normal text flow

    HTML span using inline styling

    Below, I’ve prepared an example to illustrate how we use <span>, along with an image to help you visualize it better. 🤓
    In this example, we use <span> to change the style of multiple parts of the text within a <p> element. The <span> tag wraps around specific words we want to highlight or style differently without affecting the entire line.

    <p>
      My sandwich has 
      <span style="color:yellow">yellow</span> cheese,
      <span style="color:red">red</span> tomatoes and
      <span style="color:green">green</span> fresh lettuce!!
    </p>
    HTML
    What does span do in HTML: Text displaying "My sandwich has yellow cheese, red tomatoes and green fresh lettuce". Each color word is written with its color as we set the span HTML tag.

    Styling span in CSS: Best practices

    We can also style <span> elements beyond CSS inline styling. Below, I include an example and an image to picture it.
    In this example, we place the <span> element within a <div> element. The CSS code applies specific styling (color, font style, and text-decoration) only to the text inside the <span> tag, allowing us to highlight or change the look of a selected part without affecting the rest of the text.

    <div>
      My sandwich is <span>amazing!!</span>
    </div>
    HTML
    div > span {
      color: red;
      font-style: italic;
      text-decoration: underline;
    }
    CSS
    What does span do in HTML: Text displaying  "My sandwich is amazing". The word amazing is styling differently than the rest sentence as we set the span HTML tag.

    🔖 Here’s a trick: With div > span, we’re styling only <span> elements that are direct children of a <div>. This way, we target only this specific span, without affecting other <span> elements in the code! 🔴

    Curious to learn more about how to focus on the exact elements you want? Look at my CSS selectors post — it’s all about precision! 🎯

    Targeting letters one by one using span in CSS

    Want to take it a step further? We can make it even more impressive by styling each letter of one word individually!! 🧨 ✨

    <h1>
      My sandwich is 
      <span class="letter-a">a</span>
      <span class="letter-m">m</span>
      <span class="letter-a">a</span>
      <span class="letter-z">z</span>
      <span class="letter-i">i</span>
      <span class="letter-n">n</span>
      <span class="letter-g">g</span>
      <span class="exclamation-mark">!!</span>
    </h1>
    HTML
    .letter-a {
      color: deeppink;
    }
    
    .letter-m {
      color: green;
    }
    
    .letter-z {
      color: indigo;
    }
    
    .letter-i {
      color: purple;
    }
    
    .letter-n {
      color: blue;
    }
    
    .letter-g {
      color: darkorchid;
    }
    
    .exclamation-mark {
      color: magenta;
    }
    CSS
    What does span do in HTML: Text displaying  "My sandwich is amazing". The word amazing is styling differently than the rest sentence as we set the span HTML tag.

    Learn how to use HTML span – the fun way

    What about making our learning more fun and using the metaphor of a sandwich to represent how the <span> elements are surrounded by their container element (HTML opening and closing tags)!

    Here’s the idea 💡

    Sandwich with span as the filling 🍔

    Imagine an illustration where a slice of text is styled like a sandwich with:

    • Bread Slices: Representing HTML tags surrounding the <span> element.
    • Fillings (cheese, tomato, lettuce): Representing the words or phrases wrapped in the <span> elements, styled differently to stand out, just like all these fillings inside a sandwich.

    This metaphor will show the positioning of <span> clearly! 🥳

    A cheeseburger with cheese, tomato and lettuce inside.
  • Opacity And Saturate: Empower CSS Filter For Unique Images

    Opacity And Saturate: Empower CSS Filter For Unique Images

    Welcome to a practical guide on using the CSS filter to modify how your images 📸 look. In this post, we’ll work on how to adjust the opacity and saturate (color intensity) CSS filters. These two methods will help us handle how images are rendered on our website, particularly concerning their colors and transparency.

    Let’s get into the world of CSS 👩‍💻 and customize the images to suit our needs.

    CSS filter: opacity

    The CSS property filter: opacity() helps you control how colorful an image appears against its background. You can set it anywhere between 0% and 100%. At 0%, the image turns grayscale, losing all its colors and showing only the background color. If you set it to 100%, the image displays its true, unaltered colors. Values in between change how colorful the image appears.

    🔖 For setting CSS opacity, you can use either a percentage of 0% and 100% or a decimal value between 0 and 1, both options ending up in the same outcome. 🫧😃

    Example

    Starting with the HTML code which includes three <img> elements. All images share the same .image class. Additionally, the second image includes one more class the .opacity-image2 while the third image includes the .opacity-image3 class.

    <body>
      <img class="image"/>
      <img class="image opacity-image2"/>
      <img class="image opacity-image3"/>
    </body>
    HTML

    In the CSS code the .image class introduces a non-repeated image that covers the entire background area of the element. Its dimensions are 300 pixels in width and 500 pixels in height.

    We move forward with the other two classes. The .opacity-image2 class targets the second image and applies a visual effect controlling transparency. An opacity of 80% means the element will be slightly transparent, allowing some background to show through.

    The .opacity-image3 class is meant for the third image, ensuring the same visual change happens there too. In this case, an opacity of 20% means the element will be mostly transparent, allowing more background to show through.

    .image {
      background-image: url(https://images.unsplash.com/photo-1501829385782-9841539fa6bf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3024&q=80);
      background-repeat: no-repeat;
      background-size: cover;
      width: 300px;
      height: 500px;
    }
    
    .opacity-image2 {
      filter: opacity(80%);
    }
    
    .opacity-image3 {
      filter: opacity(20%);
    }
    CSS

    * In this case, the background is black, so our effect turns to dark shades.

    Three identical photos of a red car with old buildings in Verona city, Italy. The first photo displays original colors. The second has opacity set to 80%, and the third has opacity set to 20%. The white background affects the opacity.
    Original Photo by Jonatan Hernandez on Unsplash – opacity(80%) – opacity(20%)

    * In this case, the background is white, so our effect turns to bright shades.

    This image shows three identical photos. All photos shown a red car with old buildings as a background.This photo was captured in Verona city Italy. The first photo has the original colors. The second has set the filter: opacity(80%); CSS property while the third has set the filter: opacity(20%);. Additionally the background has color white which affects the opacity.
    Original photo by Jonatan Hernandez on Unsplash – opacity(80%) – opacity(20%)

    * In this case, the background is purple (a random color for this specific example, as you can pick any color you prefer), so our effect will blend the original element’s colors and the purple background color, influenced by the 80% opacity.

    Red car with old buildings in Verona city, Italy, demonstrating CSS opacity filter effects. Three photos: original colors, 80% opacity, and 20% opacity against a purple background.

    Original photo by Jonatan Hernandez on Unsplash – opacity(80%) – opacity(20%)

    CSS filter: saturate

    Saturation is about how bright and vivid colors are in a picture. The CSS property filter: saturate() helps you adjust this. The values vary from 0% to 100%. If you choose 0%, the colors disappear and become black and white (grayscale). With a value of 100%, it stays as colorful as it was.

    Keep in mind that going over 100% makes colors appear highly vibrant. Specifying a saturation of 200% is technically allowed, but that’s not the usual way.

    🔖 To specify a percentage for saturation, you can use values like 10% or 0.1, they mean the same thing. It’s up to you. 🫧😃

    Example

    Starting with the HTML code which includes three <img> elements. All images share the same .image class. Additionally, the second image includes one more class the .saturate-image2 while the third image includes the .saturate-image3 class.

    <img class="image"/>
    <img class="image saturate-image2"/>
    <img class="image saturate-image3"/>
    
    HTML

    In the CSS code, the .image class introduces a non-repeated image that covers the entire background area of the element. Its dimensions are 300 pixels in width and 500 pixels in height.

    We continue with the other two classes. The .saturate-image2 class targets the second image and applies a visual effect controlling saturation. A saturation of 200% will be doubled compared to the original colors of the image, resulting in a sharp visual effect.

    The .saturate-image3 class focuses on the third image and applies the same visual effect. In this case, a saturation of 20% means the image will have its colors slightly desaturated, resulting in a toned-down appearance compared to the original colors. The saturation level will be reduced to one-fifth of the original, giving a less intense color effect.

    .image {
      background-image: url(https://images.unsplash.com/photo-1501829385782-9841539fa6bf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3024&q=80);
      background-repeat: no-repeat;
      background-size: cover;
      width: 300px;
      height: 500px;
    }
    
    .saturate-image2 {
      filter: saturate(200%);
    }
    
    .saturate-image3 {
      filter: saturate(20%);
    }
    CSS

    The results are shown in the images below.

    Red car with old buildings in Verona city, Italy
    default-saturate(100%) Original photo by Jonatan Hernandez on Unsplash
    Vibrant red car against old buildings in Verona city, Italy, with CSS saturate filter set to 200%.
    saturate(200%)
    Red car against old buildings in Verona city, Italy, with CSS saturate filter set to 20%, resulting in a more grayish appearance compared to the original photo.
    saturate(20%)
  • CSS Filters: Adjust Contrast and Brightness

    CSS Filters: Adjust Contrast and Brightness

    Welcome to a practical guide on using CSS filter to modify the appearance of your 📸 images. In this post, we’ll focus on adjusting the contrast and brightness CSS filters. These techniques will allow us to control how images are displayed, specifically regarding their levels of light and darkness.

    Let’s dive into the world of CSS 👩‍💻 and customize the images to suit our preferences.

    CSS property filter: contrast

    The CSS property filter: contrast() is used to control the difference between the light and dark parts of an image. The contrast property accepts values from 0% to infinity. A value of 100% (or 1) is normal contrast. A higher value, increases the difference between light and dark areas, making the image appear more vivid and clearer. On the contrary, a lower value decreases the distinction, resulting in a softer and more blended appearance.

    Always remember, if you want your picture to be clear and detailed, you can try using higher contrast values. This way, you find the right mix between making the picture softer and keeping it sharp and clear. But if you set the contrast too low, the picture might not show the different parts or details well. It might look a bit boring or like it has a gray layer on top. 🙁

    🔖 You can express contrast either as a percentage (10%) or in decimal form (0.1) – both methods produce identical results. The choice between the two is entirely based on your personal liking. 🫧😃

    CSS Contrast filter example

    I crafted an example for you.

    Starting with the HTML code which includes three <img> elements. All images share the same .image class. Additionally, the second image includes one more class the .contrast-image2 while the third image includes the .contrast-image3 class.

    <img class="image"/>
    <img class="image contrast-image2"/>
    <img class="image contrast-image3"/>
    HTML

    In the CSS code the .image class introduces a non-repeated image that covers the entire background area of the element. Its dimensions are 300 pixels in width and 500 pixels in height.

    We move forward with the other two classes. The .contrast-image2 class targets the second image and applies a visual effect that controls the contrast. A value of 150% would make the differences between light and dark elements in the image more pronounced, resulting in a sharper and more defined visual appearance.

    The .contrast-image3 class targets the third image and applies the same visual effect. In this case, a value of 20% reduces the contrast, making it less distinct and blending the light and dark areas.

    .image {
      background-image: url(https://images.unsplash.com/photo-1501829385782-9841539fa6bf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3024&q=80);
      background-repeat: no-repeat;
      background-size: cover;
      width: 300px;
      height: 500px;
    }
    
    .contrast-image2 {
      filter: contrast(150%);
    }
    
    .contrast-image3 {
      filter: contrast(20%);
    }
    CSS

    In the images below we can see the results.

    CSS property filter: brightness

    The CSS filter: brightness is a property that adjusts the intensity of light in an image. It influences how bright or dark the visual element appears to the viewer. A value of 100% (or 1) represents the origin brightness. Values less than 100% darken the element, while values greater than 100% brighten it. For example, 50% would make the element half as bright, and 200% would make it twice as bright while a brightness of 0% gives only a black color.

    🔖 You have the choice to indicate brightness using percentages, such as 10%, or in decimal form like 0.1, both have the same results. It’s completely up to your preference. 🫧😃

    CSS Brightness filter example

    Here is an example for you.

    Starting with the HTML code which includes three <img> elements. All images share the same .image class. Additionally, the second image includes one more class the .brightness-image2 while the third image includes the .brightness-image3 class.

    <img class="image"/>
    <img class="image brightness-image2"/>
    <img class="image brightness-image3"/>
    HTML

    In the CSS code the .image class introduces a non-repeated image that covers the entire background area of the element. Its dimensions are 300 pixels in width and 500 pixels in height.

    We proceed with the other two classes. The .brightness-image2 class targets the second image and applies a visual effect that controls the brightness making the element 1.5 times brighter than its normal appearance.

    The .brightness-image3 class targets the third image and applies the same visual effect. In this case, a brightness of 20% means the element will be darkened, making it one-fifth as bright as its original state.

    .image {
      background-image: url(https://images.unsplash.com/photo-1501829385782-9841539fa6bf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3024&q=80);
      background-repeat: no-repeat;
      background-size: cover;
      width: 300px;
      height: 500px;
    }
    
    .brightness-image2 {
      filter: brightness(150%);
    }
    
    .brightness-image3 {
      filter: brightness(20%);
    }
    CSS

    In the images below we can see the results.

  • Using CSS Opacity for Transparency

    Using CSS Opacity for Transparency

    The CSS opacity filter is a property that allows you to adjust the transparency of an element on a website. Opacity values range from 0.0 to 1 (default), where opacity: 0.0 indicates complete transparency and opacity: 1 indicates complete intransparency (opaqueness). 👻💻

    How to change opacity on hover?

    You can create visually appealing effects by overlapping the elements’ colors. Moreover, you can make it more dynamic 💥 by animating it with CSS transitions. For instance, you can create an effect when an element is clicked or hovered over.

    Let’s say, for example, we have a red box like this

    A magenta box on an orange background

    and would like to change its opacity on hover. How would we do that? We would add the following code:

    .box:hover {
      opacity: 0.5;
    }
    CSS

    As a result, when hovering over our element, it would change its opacity like this

    See the Pen
    Opacity box no transition
    by George (@GeorgeLin)
    on CodePen.

    Some may think this is not ideal; changing the opacity is cool, but it feels like we can do better. A straightforward way to improve the opacity change is just to add a transition effect and make it look much cooler:

    .box {
      transition: opacity 0.5;
    }
    .box:hover {
      opacity: 0.5;
      transition: opacity 0.5;
    }
    CSS

    We apply the same transition to both the .box class and its hover state to ensure a consistent effect when hovering on or off the element. This approach produces the following result:

    See the Pen
    Opacity box with transition
    by George (@GeorgeLin)
    on CodePen.

    Pretty cool, right?

    Exploring CSS Opacity and Its Impact on Background Colors in HTML

    Depending on the opacity value, the child’s hues are influenced by the parent’s colors. Hues become more vibrant as the opacity/transparency increases. If the child element has a low opacity value, its colors will be more similar to the parent’s background-color. Conversely, if the child element has a high opacity value, its colors will be less similar to the parent’s color.

    In the following examples I prepared, we can see how this CSS property works. 🧐 So, let’s take a closer look.

    Each example has a pair of HTML elements, one with .parent class and one with the .child class. Child elements have the same colors (color: black and  background-color: white) while parent elements have different background-color each time (gray and orange).

    CSS Opacity: Effects on HTML Elements with Gray Backgrounds

    As we can see, the resulting color of our child element will be a mix of white, influenced by the grey ⬜ background color.

    .parent {
      background-color: grey;
    }
    
    .child {
      color: black;
      background-color: white;
      opacity: 0.1; // depending on the value //
    }
    CSS
    An image showing a table with 10 blocks. Each block shows the CSS opacity filter starting from 0.1 to 1

    CSS Opacity: Effects on HTML Elements with Orange Backgrounds

    As observed, the child’s resulting color will be a blend of white, affected by the orange 🟧 background color.

    .parent {
      background-color: orange;
    }
    
    .child {
      color: black;
      background-color: white;
      opacity: 0.1; // depending on the value //
    }
    CSS
    An image showing a table with 10 blocks. Each block shows the CSS opacity filter starting from 0.1 to 1.

    CSS opacity offers a powerful way to control transparency, influencing how the background colors of parent elements interact with those of child elements. Understanding these interactions can help create visually appealing designs where colors harmoniously blend across different layers. 🌈 ✨