Tag: CSS

Explore tips, tutorials, and deep dives into CSS — learn styling techniques, layout systems, animations, and best practices for modern web 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
  • 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!

  • 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.

  • The Truth About CSS: Is It Really a Programming Language?

    The Truth About CSS: Is It Really a Programming Language?

    Is CSS a programming language? If you’ve ever wondered, read the following post and discover 🔎 why you might reconsider asking that question!

    A programming language is a tool 🛠 that enables developers to instruct computers to perform tasks or calculations. Developers achieve this by writing code that tells the computer what to do step-by-step, much like giving commands to execute specific outcomes. It consists of rules that a computer can understand and execute. 😎

    CSS (Cascading Style Sheets) is not considered a programming language. It is a style sheet language that defines how HTML elements should be displayed on a webpage and controls their layout, colors, fonts, and spacing.

    Below, I have prepared an example to help you better understand what CSS does. Also, you’ll see an image displaying the HTML and CSS code snippet used in the example.

    As we already said, CSS contains rules. Each rule typically includes a selector (which identifies the HTML element to style) and the declaration block {...} (which specifies the styling properties and their values). In our example, h1 is the selector, and background-color: pink; , color: indigo; and font-size: 25px; are declarations.

    <h1>Enjoy working with CSS!!</h1>
    HTML
    h1 {
      background-color: pink;
      color: indigo;
      font-size: 25px;
    }
    CSS
    Is CSS a programming language - Text writing: Enjoy working with CSS

    Remember that if you want to include interactive functionality in your projects, programming languages are invaluable tools you can leverage. 🧨 ✨

  • 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. 🌈 ✨

  • CSS Word Spacing – How to Do It Right

    CSS Word Spacing – How to Do It Right

    Hey everyone! 😃 In this post, we’ll take a look at the CSS property called word spacing. It controls the horizontal distance between words in a text. By default, the CSS word spacing is set to 0px, which is known as normal (word-spacing: normal). If you increase the spacing value, the distance between words will get bigger, while decreasing it will make the spacing smaller.

    Let’s take a closer look at this simple yet useful CSS property to gain a better understanding of it.

    HTML structure for CSS word spacing

    Let’s begin by creating an HTML document. In it, we include three headings <h1> that display the text “Hello World”. The first heading has the class .spacing-normal, the second heading has the class .spacing-big, and the third heading has the class .spacing-small. All headings contain the same text, as it allows an easy comparison. 👌

    <h1 class="spacing-normal">Hello World</h1>
    <h1 class="spacing-big">Hello World</h1>
    <h1 class="spacing-small">Hello World</h1>
    HTML

    CSS structure for CSS word spacing

    For our CSS structure, we set the word-spacing property for all three classes and differentiate them by assigning different values.

    .spacing-normal {
      word-spacing: normal;
    }
    
    .spacing-big {
      word-spacing: 100px;
    }
    
    .spacing-small {
      word-spacing: -50px;
    }
    CSS

    The image below illustrates the effect of varying word spacing values on each of our headings. The first heading has normal word spacing, while the second has a larger distance between words due to the increased word spacing value. Similarly, the third heading has a narrower spacing resulting from the decreased value.

    🔖 It is important to avoid using word spacing values that are either too large or too small.

    In summary, this image illustrates how adjusting the spacing between words can affect the appearance of text. It’s amazing how even small changes can have a significant impact. Isn’t it? 😎

  • CSS Selectors nth-child VS nth-of-type And How To Use Them

    CSS Selectors nth-child VS nth-of-type And How To Use Them

    The amazing CSS selectors family! One of the most helpful tools we can use when styling our website. In today’s post, 😃 we’ll examine the unstoppable battle of nth-child() VS nth-of-type() selectors. They both allow you to target HTML elements based on their position within a parent, but they achieve that differently.

    The moment to clarify the key difference between them and how to use each effectively to get the styling we want has finally come! 🤓

    So, let’s proceed by analyzing and comparing these amazing pseudo-classes! 💻 ✨

    Preparing our HTML structure

    We begin with an HTML code snippet by creating a <section> element as the parent, which contains some <p> and <div> children.

    <section>
      <p>I'm the first paragraph</p>
      <div>I'm the first div</div>
      <p>I'm the second paragraph</p>
      <div>I'm the second div</div>
      <p>I'm the third paragraph</p>
      <div>I'm the third div</div>
    </section>
    HTML

    Below, we can see what is rendered on the screen for now. Three paragraphs and three div elements. Note that I have kept the default styling: black text on a white background.

    CSS nth child VS nth of type selectors. This image shows <p> and <div> as children inside a <section> element

    The nth-child selector

    The :nth-child() selector targets the HTML element declared inside the parentheses, regardless of its type. In the code snippet below, we pick the second child (which happens to be a div) by placing the number 2 inside the parentheses.

    section :nth-child(2) {
      color: white;
      background-color: magenta;
    }
    CSS

    The image below shows that the second child takes on our attributes.

    This image shows <p> and <div> sibling elements, with the second element styled differently using the nth-child(2) CSS selector

    The nth-of-type selector

    The :nth-of-type() selector targets HTML elements based on their type. In the following code snippet, we pick the second <p> element by placing the number 2 inside the parentheses () and specifying the type p before the selector.

    section p:nth-of-type(2) {
        color: white;
        background-color: magenta;
    }
    CSS

    The image below shows that the styling is now applied to the second paragraph instead of the second child.

    This image shows sibling <p> and <div> elements. The second <p> element is styled differently using the p:nth-of-type(2) CSS selector, with 2 inside the parentheses and p preceding the selector

    We can also try to do the same with our <div> elements. Let’s change the type, in front of our selector, from p to div. What would happen then?

    section div:nth-of-type(2) {
        color: white;
        background-color: magenta;
    }
    CSS

    We now notice that the styling is removed from the second paragraph and applied to the second div. Cool huh? 😎

    This image shows sibling <p> and <div> elements. The second <div> element is styled differently using the div:nth-of-type(2) CSS selector, with 2 inside the parentheses and div preceding the selector.

    By learning correctly when and how to use each selector, we can ensure that our CSS targets exactly the elements we intend and help us achieve more precise and effective styling. Happy coding! 🎉 👩‍💻

  • Make Your Work Easier by Using Variables in CSS

    Make Your Work Easier by Using Variables in CSS

    Hello there! Today, we will analyze the amazing variables in CSS, also known as custom properties or cascading variables. It is a powerful technique that allows us to store reusable values for colors, sizes, fonts, and even animations. These values can be set once and used as many times as we want throughout our project.

    Simply put, if we want to update a value throughout our CSS code, we only need to change it in one place instead of searching and modifying multiple CSS rules. This keeps our code clean and organized and our work more flexible, especially in larger projects. 😎

    How to declare CSS variables

    CSS variables are defined using the -- prefix and are typically declared in the :root pseudo-class. Below, we can see an example with two declared variables. The first one refers to the --primary-color (main color) of our project, while the second one concerns the --text-font-size (text’s font size).

    :root {
      --primary-color: purple;
      --text-font-size: 16px;
    }
    CSS

    By defining variables in the :root pseudo-class, we make them accessible globally. To limit a variable’s scope, we can define it locally, within a specific selector, like a class or ID.

    How to use CSS Variables

    We connect these two variables with our CSS styles, using the var() function. This step is crucial for ensuring our variables will work correctly. Simple but super useful! 🤓 ✨

    .body {
      background-color: var(--primary-color);
      font-size: var(--text-font-size);
    }
    CSS

    Global VS local scope

    Now, let’s examine what we mean by global and local variables and how knowing the difference between these two types of variables can help us write more flexible, organized, and efficient code.

    Global and local variables in CSS are essential for keeping styles easily managed. Global variables, defined in the :root selector, can be used in any CSS rule in the entire stylesheet, creating consistent layouts. In contrast, local variables, declared inside a class or ID selector, are scoped only to specific elements and their children, allowing for a more customized styling.

    Understanding variable scope with an example

    Global type

    We create two global variables named --primary-color and --secondary-color that can be applied everywhere in the stylesheet.

    /* Anything inside :root is accessible globally */
    :root {
      --primary-color: pink; 
      --secondary-color: gray;
    }
    
    body {
      background-color: var(--primary-color); /* Accessible here */
    }
    
    header {
      color: var(--secondary-color); /* Accessible here as well */
    }
    CSS

    Local type

    We create a local variable named --header-bg located inside the .header class. This specific variable can be applied only to the header and its descendants.

    .header {
      --header-bg: gray; /* Local variable */
      background-color: var(--header-bg); /* Accessible here */
    }
    
    .header h1 {
      color: var(--header-bg); /* Accessible here */
    }
    
    .footer {
      background-color: var(--header-bg); /* Not accessible here, will not work */
    }
    CSS

    Advanced CSS variable usage example

    For better understanding, I’ve prepared a more detailed example of CSS variables, showing how to apply them in a stylesheet and an image to help visualize the results. We intend to create three identical boxes side by side within a container, all styled using CSS variables.

    <div class="container">
      <div class="box">Box 1</div>
      <div class="box">Box 2</div>
      <div class="box">Box 3</div>
    </div>
    HTML
    /* Container characteristics */
    :root {
      --container-width: 600px;
      --container-height: 300px;
      --container-color: #3ab7b7; /* A shade of teal */
    }
    
    /* Box characteristics */
    :root {
      --box-width: 150px;
      --box-height: 150px;
      --box-color: #fec0ca; /* A light shade of pink */
      --box-title-weight: 800;
      --box-title-size: 25px;
    }
    
    
    /* Box & Container border */
    :root {
      --border-all-around: 5px solid;
    }
    
    body {
      width: 100%;
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .container {
      width: var(--container-width);
      height: var(--container-height);
      background-color: var(--container-color);
      border: var(--border-all-around);
      
      display: flex;
      justify-content: space-around;
      align-items: center;
    }
    
    .box {
      width: var(--box-width);
      height: var(--box-height);
      background-color: var(--box-color);
      font-size: var(--box-title-size);
      font-weight: var(--box-title-weight);
      border: var(--border-all-around);
      
      display: flex;
      justify-content: center;
      align-items: center;
    }
    CSS

    In the following image, we can see the layout we just created.

    variables in css: A container with three identical boxes inside.

    If we want to modify the height of our boxes, we can easily do it by changing only the --box-height variable. For example, let’s change it from 150px to 50px.

    In the image below, we can notice the difference.

    css variable: A container with three identical rectangles inside.

    Naming variables can be a really demanding task! Keep names simple, but ensure they are clear and meaningful.

    Combining multiple CSS variables

    When working with variables, we have the flexibility to add many values inside a CSS property. Let’s try to create a rainbow effect. We have to define variables for each rainbow color (--color-red, --color-orange, --color-yellow, --color-green, --color-blue, --color-indigo, --color-violet). Then, we’ll apply them in linear-gradient to create the rainbow effect. 🌈 🎉

    <div class="box"></div>
    HTML
    :root {
      --box-width: 800px;
      --box-height: 300px;
      --color-red: #ff0000;     /* Red */
      --color-orange: #ffa500;  /* Orange */
      --color-yellow: #ffff00;  /* Yellow */
      --color-green: #008000;   /* Green */
      --color-blue: #0000ff;    /* Blue */
      --color-indigo: #4b0082;  /* Indigo */
      --color-violet: #ee82ee;  /* Violet */
      --border-all-around: 5px solid;
    }
    
    .box {
      width: var(--box-width);
      height: var(--box-height);
      background: linear-gradient(
        to right,
        var(--color-red),
        var(--color-orange),
        var(--color-yellow),
        var(--color-green),
        var(--color-blue),
        var(--color-indigo),
        var(--color-violet)
      );
      border: var(--border-all-around);
    }
    
    CSS

    In the following image, we can see the effect! Nice, isn’t it!?

    CSS variable fallback: A rectangle with rainbow effect.

    CSS Variable Fallback

    We also have the flexibility to use fallbacks. But what is a fallback? 🤔
    A fallback acts as a default value when:

    1. A variable is not defined, or
    2. the value of a variable is not valid.

    Below are examples for each case to help you better understand it. 🧐
    Case 1 – A variable is not defined
    In the following example, the box will feature a dark gray background since --bg-color is not defined, resulting in the use of the fallback color.

    <div class="box"></div>
    HTML
    :root {
      --box-width: 300px;
      --box-height: 300px;
      /* --bg-color is not defined */
      --border-all-around: 5px solid;
    }
    
    .box {
      width: var(--box-width);
      height: var(--box-height);
      background-color: var(--bg-color, darkgray); /* Fallback to darkgray */
      border: var(--border-all-around);
    }
    CSS

    The image below displays the results.

    css variables: A box with darkgray background color and a title with 40 pixels font size.

    Case 2 – the value of a variable is not valid
    The box will have a dark gray background because --bg-color is set to an invalid value; “pinky” is not a valid way to specify the color pink, so the fallback will be used instead.

    :root {
      --box-width: 300px;
      --box-height: 300px;
      --bg-color: pinky; /* --bg-color is invalid */
      --border-all-around: 5px solid;
    }
    
    .box {
      width: var(--box-width);
      height: var(--box-height);
      background-color: var(--bg-color, darkgray); /* Fallback to darkgray */
      border: var(--border-all-around);
    }
    CSS
    css variables: A box with darkgray background color and a title with 40 pixels font size.

    Frequently Asked Questions 🤔

    Are spaces allowed in css variable names?​

    No, spaces are not allowed in CSS variable names. You can use hyphens (-), underscores (_), or camelCase instead.

    What are the valid characters for css variable names​?

    CSS variable names can include letters, numbers, underscores (_), hyphens (-), and must start with two dashes (--). They cannot contain spaces or special characters.

  • Capitalizing CSS property values – What is the right way?

    Capitalizing CSS property values – What is the right way?

    When capitalizing CSS property values, a key advantage is that CSS is not case-sensitive. 🪶 This means that no matter how you choose to capitalize your CSS values, either by using different letter cases, including lowercase, uppercase, or a combination of both, the browser will still understand all of them correctly.

    The letter case used for color names in CSS does not impact how the browser interprets them

    Here are some examples of this, using color properties:

    • Lowercase: You may use all lowercase letters for color names. For example, “red”, “green”, “blue”, “orange”, and “yellow” are all valid.
    .element {
      background-color: green;
    }
    CSS
    • Uppercase: Uppercase letters are also valid, so “RED”, “GREEN”, “BLUE”, “ORANGE” and “YELLOW” will be just fine.
    .element {
      color: GREEN;
    }
    
    CSS

    Combinations of both: You have the flexibility to use both uppercase and lowercase letters in color names. This lets you style color names according to your coding preferences or highlight specific parts of the color name.

    .element {
      border-color: LightGreen;
    }
    CSS

    No Impact on Interpretation: Regardless of the letter case used, the browser will correctly understand and apply the specified color. The choice of letter case is purely a matter of personal or coding style preference.

    .element {
      color: LiGhtgreeN;
    }
    CSS

    To sum it up, the letter case used for color names in CSS does not impact how the browser interprets them. 😃

    Whether you opt for all lowercase, uppercase, or a combination of both, it’s up to your coding style or your development team’s coding standards. The important thing to remember is that the browser will understand and apply the specified color, regardless of the letter casing used.

    That said, developers still often follow conventions. While browsers are forgiving of capitalization, one common standard practice is to use all lowercase for CSS property values.

  • CSS Letter Spacing – Working on The Correct Way

    CSS Letter Spacing – Working on The Correct Way

    Greetings! 😃 In today’s post, we will explore the CSS letter spacing property, which is responsible for the horizontal distance between letters. The default letter spacing is equivalent to 0px, known as normal (letter-spacing: normal). Increasing value results in bigger spacing.

    We will continue with some examples to understand better this easy but still useful CSS property.

    HTML structure for letter spacing

    We start with our HTML document by creating three <h1> headings with the text “Hello World”. All headings contain identical text to highlight the difference in letter spacing. The headings are styled differently, the first one uses  .spacing-normal class, the second one .spacing-small class, and the third one .spacing-big.

    <h1 class="spacing-normal">Hello World</h1>
    <h1 class="spacing-small">Hello World</h1>
    <h1 class="spacing-big">Hello World</h1>
    HTML

    CSS structure for letter spacing

    We continue our work with the CSS structure. We have assigned the letter-spacing property to all three classes but with different values. Even though the text is the same in all headings, they will appear different due to class variation.

    .spacing-normal {
      letter-spacing: normal;
    }
    
    .spacing-small {
      letter-spacing: 10px;
    }
    
    .spacing-big {
      letter-spacing: 50px;
    }
    CSS

    In the image provided, we can see the effect of varying letter spacing in css on the previously created headings. Increasing the letter spacing value results in greater distance between the letters, resulting in a more notable gap between them. The image clearly illustrates the relationship between letter spacing and letter distance.

    🔖 Note that letter spacing affects the distance between words, too.

    What to AVOID when using CSS letter-spacing property

    The letter spacing can support negative values. However, reducing the default letter spacing requires caution as it can negatively impact text legibility by causing letters to appear too close together. Here’s an example:

    .spacing-normal {
      letter-spacing: normal;
    }
    
    .spacing-negative {
      letter-spacing: -10px;
    }
    CSS