Category: In the Browser

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

  • 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! 🎉 👩‍💻

  • How To Make CSS Reflection – The Easy Way

    How To Make CSS Reflection – The Easy Way

    Hello there! Today, we will explore a simple yet valuable topic. How do you create a CSS reflection using only one property? Let’s unlock the amazing –webkit-box-reflect property and effortlessly craft repeatable elements with minimal 😉 code! 🥳

    Simple CSS Reflection

    We will start by creating two identical boxes. We set 200 pixels width and 100 pixels height and also add a solid black 2 pixel border. Continuing, we give our boxes an indigo background-color.

    <div class="box box--adjacent"></div>
    <div class="box box--spaced"></div>
    HTML

    Next, we proceed with our -webkit-box-reflect where we create the reflection of each box, ensuring that their direction remains the same (right direction). However, the second reflection is located 150 pixels from the original box. That way, we can explore both direction and distance.

    .box {
      width: 200px;
      height: 100px;
      border: 2px solid black;
      background-color: indigo;
      &--adjacent {
        -webkit-box-reflect: right;
      }
      &--spaced {
        -webkit-box-reflect: right 150px;
      }
    }
    SCSS

    In the images provided, we can see two boxes along with their corresponding reflections. In the first image, the box’s reflection is located exactly on the right-hand side, adjacent to the original box, while in the second image, the box also has a reflection on the right-hand side but is positioned 150 pixels away from the original box.

    CSS reflection. An image showing a box reflection on the right-hand side of the origin box.
    -webkit-box-reflect: right
    CSS reflection. An image showing a box reflection on the right-hand side positioned 150 pixels away from the original box.
    -webkit-box-reflect: right 150px

    CSS Reflection With Fade-Out Effect

    It is essential to know that we can use the -webkit-box-reflect CSS property to create a reflection effect that accurately mirrors the original element’s appearance and style. We can further improve this effect and make it seem more authentic by using the linear-gradient CSS property, which gives us the impression of a gradual fade-out. This technique is pretty cool! 😎

    We will start with our HTML code snippet, creating again two boxes with identical characteristics. We set 200 pixels width and 100 pixels height. Next, we give both boxes an indigo background-color.

    <div class="box box--top"></div>
    <div class="box box--bottom"></div>
    HTML

    Next, we proceed with our -webkit-box-reflect where we create the reflections of each box, ensuring that they have the same direction (below). However, the second reflection has a linear-gradient that creates a fade-out effect from top to bottom.

    .box {
      width: 200px;
      height: 100px;
      background-color: indigo;
      &--simple-reflection {
        -webkit-box-reflect: below 10px;
      }
      &--fade-out-reflection {
        -webkit-box-reflect: below 10px
        linear-gradient(
          rgba(0,0,0,0.0), 
          rgba(0,0,0,0.2),
          rgba(0,0,0,0.4),
          rgba(0,0,0,0.6),
          rgba(0,0,0,0.8)
        );
      }
    }
    SCSS

    When adjusting the reflection vertically, we need to use above and below instead of top and bottom.

    Below, you can observe two boxes along with their related reflections. In the first image, the box’s reflection is located 10 pixels above the original box. In the second image, the reflection stands again 10 pixels above the origin box, but this time has the fade-out effect. It looks nice, doesn’t it?

    CSS reflection. An image showing a box reflection 10 pixels below the original box.
    -webkit-box-reflect: below 10px
    CSS reflection. An image showing a box reflection positioned 10 pixels below the original box. It has a linear-gradient(
      rgba(0,0,0,0.0), 
        rgba(0,0,0,0.2),
        rgba(0,0,0,0.4),
        rgba(0,0,0,0.6),
        rgba(0,0,0,0.8)) that creates the fade-out effect.
    -webkit-box-reflect: below 10px linear-gradient(rgba(…))

    If you are interested in diving deeper, there is an amazing post about CSS text reflection that you might enjoy reading!

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

  • HTML Lists Made Easy: Ordered, Unordered, and Special Lists

    HTML Lists Made Easy: Ordered, Unordered, and Special Lists

    Is our life surrounded by lists ✔ or do we live in a perfect world where taking care of things is unnecessary? Are we able to deal with our problems without organizing them? Are we ready to work on something difficult or, even worse, build something from scratch without using a list of steps? Do lists improve our life balance and make us more methodical? In this post, we will see how to create HTML lists and make our work and, by extension, our lives easier. 😃 ✨

    Do lists improve our life balance and make us more methodical?

    HTML Lists

    A list is defined as a group of matching items. By default, the list’s direction is vertical. There are three categories of HTML lists: ordered, unordered, and description lists.

    Ordered list

    An ordered list, by default, uses numbers as markers for each list item. We have the ability to manipulate the list-style-type property and select the desired style.

    <ol style="list-style-type:decimal;"> <!-- decimal is for example, you can choose any list-style-type you prefer -->
      <li>France</li>
      <li>Germany</li>
      <li>Poland</li>
    </ol>
    HTML

    Below, you can see the types off the ordered list.

    Unordered list

    An unordered list, by default, uses a dot like a small black music disk, as its marker (list-style-type: disk;). We can manipulate the list-style-type property and choose the willing style.

    <ul style="list-style-type:disk;"> <!-- disk is for example, you can choose any list-style-type you prefer -->
      <li>Greece</li>
      <li>Italy</li>
      <li>Spain</li>
    </ul>
    HTML

    💡Combination of lists

    Combine ordered & unordered lists and make them more POWERFUL.

      <li>France
        <ul style="list-style-type:circle">
          <li>Paris
            <ul style="list-style-type:quare">
              <li>Eiffel Tower</li>
              <li>Louvre Museum</li>
            </ul>
          </li>
          <li>Lyon</li>
          <li>Marseille</li>
        </ul>
      </li>
      <li>Germany</li>
      <li>Poland
        <ol style="list-style-type:lower-alpha">
          <li>Krakow</li>
          <li>Gdansk</li>
        </ol>
      </li>
      <li>Belgium
        <ol style="list-style-type:decimal-leading-zero">
          <li>Brussels</li>
          <li>Bruges
            <ul style="list-style-type:square">
              <li>Canals</li>
              <li>City Hall</li>
            </ul>
          </li>
          <li>Lier</li>
        </ol>
      </li>
    </ol>
    HTML

    Description list

    Description lists are used when we want to show terms followed by their descriptions. It consists of <dl> tag (a description list) which requires two elements. The first one is a <dt> tag, the description term element, and the second one is the <dd> tag, the description element.
    Keep in mind, that the <dd> element has somemargin-left set by default.

    📝 We must pay attention to the correct order, as <dt> tag always comes before <dd> tag.

    <dl>
      <dt><b>HTML</b></dt>
      <dd>The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser.</dd>
      <dt><b>CSS</b></dt>
      <dd>Cascading Style Sheets is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML.</dd>
    </dl>
    HTML

    💎 Special lists

    Image list

    In HTML lists, instead of marks, we can also use an image. Although it is really demanding, if we choose the appropriate image and do all the necessary modifications we can achieve a really amazing result. We have to replace the marks and create a list by adding the image’s URL to the list-style-image property.

    
    <ul
     style="list-style-image:url('https://images.unsplash.com/photo-1464820453369-31d2c0b651af?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=20&q=80');"
    >
      <li>Choose</li>
      <li>the</li>
      <li>appropriate</li>
      <li>image :-)</li>
    </ul>
    
    HTML

    List with one emoticon

    We can also make HTML lists using the same emoticon by adding it to the content property.

    <ol class="emoticon-face">
      <li>HTML</li>
      <li>CSS</li>
      <li>JAVASCRIPT</li>
    </ol>
    HTML
    .emoticon-face {
      list-style: none;
      
      li:before {
        content: "🥰";
        margin-right: 10px;
      }
      
      & li:not(:last-child) {
        margin-bottom: 10px;
      }
    } /* end of emoticon-face */
    SCSS

    List with different emoticons

    We can use more than one emoticon by setting the nth-child property and thus utilize different emoticons to content property each time.

    <ul class="emoticons-weather">
      <li>Sunny days</li>
      <li>Cloudy days</li>
      <li>Rainy days</li>
      <li>Snowy days</li>
    </ul>
    HTML
    .emoticons-weather {
      list-style: none;
    }
    /* We use :not(:last-child) selector because we want to add distance 
    *  between all list items apart from the last one.   
    */
    li:not(:last-child) {
      margin-bottom: 4px;
    }
    .emoticons-weather li {
      &:before {
        margin-right: 8px;
      }
      &:nth-child(1):before {
        content: "🌞";
      }
      &:nth-child(2):before {
        content: "🌥";
      }
      &:nth-child(3):before {
        content: "☔";
      }
      &:nth-child(4):before {
        content: "⛄";
      }
    } /* end of emoticons-weather li */
    SCSS

    🤗 I chose these lovely emoticons above for my examples, but you can select any emoticon you want! All you have to do is copy and paste them into the content property.

    Horizontal list

    By default, HTML lists are vertical & list items have display: block;.Because of their blocked display, each list item takes up the available screen width and starts in a new line. In order to use them in a horizontal direction, we must implement them by changing the list-style property to none list-style: none; and setting the display property to inline-block display: inline-block;. This way, they occupy the same space as their size, and we can put them, side by side, on the same line.


    💡 It is common to use horizontal lists in order to make navbars.

    <ul class="horizontal-direction">
      <li>Home</li>
      <li>Our work</li>
      <li>About us</li>
      <li>Contact</li>
    </ul>
    HTML
    .horizontal-direction {
      background-color: orange;
      max-width: 350px;
      list-style: none;
      & li {
        display: inline-block;
      }
      & li:not(:last-child) {
        padding-right: 10px;
      }
    } /* end of horizontal-direction */
    SCSS
  • 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
  • The CSS nth-of-type Selector – How To Target Elements By Type

    The CSS nth-of-type Selector – How To Target Elements By Type

    Hello there! Today, we’ll explore how the CSS nth-of-type() selector works. This powerful CSS selector allows us to target every nth child HTML element of a specific type within its parent container.

    Below, I prepared some examples to analyze this amazing pseudo-class thoroughly.

    Basic HTML structure

    We begin with the HTML structure. We create an <article> HTML element. It contains two headings (<h1> , <h2>) elements. Also, we include some <p> and <div> HTML elements. The example may seem slightly complicated, but it is the right way to understand just how useful 🛠 tool this selector can be. 😃
    For now, we’ll maintain the default styling with a white background and black text.

    🔖 Please note that this HTML code snippet will be used across all examples.

    <article>
      <h1>Explore India</h1>
      <h2>India's Top Ten Attractions</h2>
      <p>Here goes the 1st paragraph about India's attractions</p>
      <p>Here goes the 2nd paragraph about India's attractions</p>
      <h2>India's Food</h2>
      <div>Here goes text about India's cusine</div>
      <div>Here goes text about India's cusine</div>
    </article>
    HTML

    Below, we can see what is rendered on the screen 💻 for now. It’s just a simple article in a newspaper 🗞 or on the internet. At least as articles were once upon a time 😂 just text, no images, and no special styling. Don’t worry; it works well for us!

    An article HTML element with <h1>, <h2>, <p>, and <div> elements, all displaying default styling, illustrating the CSS nth-of-type() selector in action.

    Targeting a specific HTML element

    Now let’s explore how this selector works. We start by using the nth-of-type() selector, preceded by h2, and placing the number 1 inside the parentheses (). This targets the very first <h2> element within our <article> parent.

    article h2:nth-of-type(1) {
      color: white;
      background-color: magenta;
    }
    CSS

    In the following image, we can see the result of the applied nth-of-type selector.

    An article HTML element containing <h1>, <h2>, <p>, and <div> elements. All have default styling, except the first <h2> element, which has a magenta background and white text, styled using the h2:nth-of-type(1) CSS selector.

    Targeting all elements of a specific type

    We continue and dive a bit deeper! We place h2 before our nth-of-type() selector, but this time, we add the n inside the parentheses (). By doing so, we target all <h2> elements within our parent element.

    The n in nth-of-type(n) enables the selector to match elements at any position among siblings. This means you can apply styles to all matching elements without specifying an exact position, ensuring that every instance is included.. 😎

    article h2:nth-of-type(n) {
      color: white;
      background-color: magenta;
    }
    CSS

    In the image below, we notice the styling applied to all <h2> HTML elements after using the nth-last-of-type(n) selector.

    An article HTML element containing <h1>, <h2>, <p>, and <div> elements. All have default styling except the <h2> elements, which have a magenta background and white text, styled using the h2:nth-of-type(n) CSS selector.

    Targeting elements of various types

    Additionally, we have the option to use the nth-of-type() selector to target more than one different type of HTML elements. In our case, we set the p:nth-of-type(1) to target the first <p> element and also the div:nth-of-type(2) to target the second <div> element.

    article p:nth-of-type(1),
    article div:nth-of-type(2) {
    	 color: white;
    	 background-color: magenta;
    }
    CSS

    This is how it currently looks on the screen 💻. Cool huh? 😃 ✨

    An article HTML element containing <h1>, <h2>, <p>, and <div> elements. All have default styling except the first <p> and the second <div> elements, which have a magenta background and white text, styled using the p:nth-of-type(1) and div:nth-of-type(2) CSS selectors.
  • Drop-shadow VS Box-shadow: What is the difference?

    Drop-shadow VS Box-shadow: What is the difference?

    Greetings 😃 In today’s session, we’re about to explore 🔎 the fascinating world of the CSS shadow properties. Get ready to unlock 🗝 the secret between CSS drop-shadow and box-shadow properties!

    I created a beautiful, playful bee 🐝 to make our example understandable and funny! You can check out my “bee code” at the end of my post and see how I created it. You are welcome to use it wherever suits you!

    CSS box-shadow

    Now, let’s focus on our shadows! In the following example, we see the effect of the box-shadow property. Our shadow is applied to the two sides of our box (bee-wrapper). The code I provided creates a gray shadow with the following characteristics:

    .bee-wrapper {
      ...
      /* offset-X | offset-Y | blur-radius | spread-radius | color */
      box-shadow: 10px 10px 10px 0 #5b5959;
    } 
    CSS
    • offset-X: adds 10 pixels to the right of the element.
    • offset-Y: adds 10 pixels below the element.
    • blur-radius: A blurry shadow with a radius of 10 pixels. (You are free to use a higher blur radius for a softer, more diffuse shadow, or you might opt for a lower blur radius to create a sharper, crisper shadow).
    • spread-radius: The shadow doesn’t expand or contract beyond the blur radius as I set it to zero. (I chose not to add spread, but it’s totally up to you if you want to add it.
      🔖 Keep in mind that the spread-radius parameter determines whether the shadow expands or reduces in size. A positive value will enlarge the shadow, whereas a negative value will shrink it).
    • color: The color of the shadow is a shade of gray (#5b5959).
    Comparison between box-shadow and drop-shadow CSS properties.This is the box shadow property

    CSS drop-shadow

    In this case, on the other hand, we can see that by setting the filter: drop-shadow() property the shadow applies smoothly to all parts of our content inside the box (bee-wrapper). Cool 🥳 ha! So, let’s see our code in more detail and how the shadow will now be rendered on the screen through our lovely bee!

    .bee-wrapper {
      ...
    /* offset-X | offset-Y | blur-radius | color */
    filter: drop-shadow(10px 10px 5px #5b5959);
    }
    CSS
    • filter: The filter property in CSS applies various graphical effects, including blurs, color adjustments, and, in this instance, shadows, to an element.
    • offset-X: This specifies the horizontal offset of the shadow from the element. It’s 10 pixels to the right, creating a shadow on the right side of the element.
    • offset-Y: This specifies the vertical offset of the shadow from the element. It’s 10 pixels below the element, creating a shadow below it.
    • blur-radius: A moderately blurry shadow with a radius of 5 pixels. (A higher value would create a more diffused and softer shadow, while a lower one would create a sharper shadow).
    • color: The shadow’s color is a shade of gray (#5b5959).

    🔖 It is worth mentioning that spread-radius property does not appear here.

    Comparison between drop-shadow and box-shadow CSS properties. This is the drop shadow property

    Complete bee code

    Below, I include my HTML and CSS bee code.

    <div class="bee-wrapper">
      <div class="face">
        <div class="antenna antenna--left"></div>
        <div class="antenna antenna--right"></div>
        <div class="bee-eye bee-eye-left"></div>
        <div class="bee-eye bee-eye-right"></div>
        <div class="bee-mouth"></div>
      </div>
      <div class="wing wing--left"></div>
      <div class="wing wing--right"></div>
      <div class="sting"></div>
      <div class="bee-body">
        <div class="stripes"></div>
      </div>
    </div>
    HTML
    Expand
    :root {
      --bg-color: #cdc6c7;
    }
    
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html,
    body {
      height: 100%;
    }
    
    body {
      min-width: 300px;
      background-color: var(--bg-color);
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
    }
    
    // BEE
    .bee-wrapper {
      position: relative;
      width: 200px;
      height: 300px;
      
                /* BOX-SHADOW */
      /* uncomment to see the box-shadow effect
      * offset-x | offset-y | blur-radius | spread-radius | color
      */
      // box-shadow: 10px 10px 10px 0 #5b5959;
      
                /* DROP-SHADOW */
      /* uncomment to see the drop-shadow effect
      * x-offset | y-offset | blur | color
      */
      // filter: drop-shadow(10px 10px 5px #5b5959);
      
      .face {
        position: absolute;
        width: 60px;
        height: 60px;
        background-color: #ecaf2f;
        box-shadow: inset 0 0 10px #dda22c;
        border-radius: 50%;
        z-index: 20;
        top: 100px;
        left: 80px;
        .antenna {
          position: absolute;
          width: 2px;
          height: 50px;
          background-color: black;
          &--left {
            top: -48px;
            left: 12px;
            transform: rotate(-20deg);
            &:after {
              content: "";
              position: absolute;
              width: 6px;
              height: 6px;
              background-color: black;
              border-radius: 50%;
              top: -2px;
              left: -2px;
            }
          }
          &--right {
            top: -48px;
            right: 12px;
            transform: rotate(20deg);
            &:after {
              content: "";
              position: absolute;
              width: 6px;
              height: 6px;
              background-color: black;
              border-radius: 50%;
              top: -2px;
              right: -2px;
            }
          }
      }
        .bee-eye {
          position: absolute;
          width: 20px;
          height: 20px;
          border-radius: 50%;
          box-shadow: 3px 3px 0 0 black;
          transform: rotate(230deg);
          &.bee-eye-left {
            top: 14px;
            left: 4px;
          }
          &.bee-eye-right {
            top: 14px;
            right: 4px;
          }
      }
        .bee-mouth {
          position: absolute;
          width: 32px;
          height: 32px;
          border-radius: 60%;
          box-shadow: 3px 3px 0 0 black;
          top: 12px;
          left: 14px;
          transform: rotate(45deg);
        }
      }
    
      .wing {
        position: absolute;
        width: 40px;
        height: 80px;
        &--left {
          background-image: linear-gradient(to bottom right, #f0f1d2, #dda22c);
          border-radius: 30px 80px 60px 80px;
          transform: rotate(352deg);
          top: 130px;
          left: 45px;
        }
        &--right {
          background-image: linear-gradient(to bottom left, #f0f1d2, #dda22c);
          border-radius: 80px 30px 80px 60px;
          transform: rotate(8deg);
          top: 130px;
          left: 135px;
        }
      }
    
      .bee-body {
        position: absolute;
        width: 60px;
        height: 160px;
        border-radius: 50%;
        background-color: #ecaf2f;
        box-shadow: inset 0 0 8px #dda22c;
        overflow: hidden;
        top: 100px;
        left: 80px;
          .stripes {
          position: absolute;
          width: 80px;
          height: 22px;
          background-color: black;
          border-radius: 10px;
          top: 44px;
          left: 50%;
          transform: translate(-50%);
          &:before {
            content: "";
            position: absolute;
            width: 80px;
            height: 20px;
            background-color: black;
            border-radius: 10px;
            top: 38px
          }
          &:after {
            content: "";
            position: absolute;
            width: 80px;
            height: 20px;
            background-color: black;
            border-radius: 10px;
            top: 72px;
          }
        }
      }
    
      .sting {
        position: absolute;
        width: 0;
        height: 0;
        border-left: 10px solid transparent;
        border-right: 10px solid transparent;
        border-top: 24px solid black;
        top: 250px;
        left: 100px;
      }
    }
    CSS
    Expand
  • The CSS Sepia Filter – Learn How to Make Vintage Images

    The CSS Sepia Filter – Learn How to Make Vintage Images

    In the world of web design, adding a touch of nostalgia or a vintage vibe to images can significantly enhance a website’s aesthetic. One timeless technique to achieve this effect is the CSS sepia filter.

    Originating from sepia ink, which has been used for centuries, this filter gives photos a warm, brownish tone reminiscent of old photographs, lending a sense of history and charm to the visuals.

    An image with 0% sepia maintains its original, true colors, while at 100% sepia, the image is entirely transformed into a warm, brownish tone as an aged photograph. As you increase the percentage, you intensify the sepia effect.

    Join me 😃 into the magic of the filter: sepia(%) CSS property and explore how it can be seamlessly integrated into your web design toolbox.

    History of the sepia ink

    🧐 Did you know that sepia ink is a brownish-black ink made from the ink sac of the common cuttlefish, Sepia? 🐙 They belong to the class Cephalopoda, which also includes squid, octopuses, and nautiluses.

    Historically, it has been used for writing and drawing, and it was particularly popular in the past for writing manuscripts and creating artwork.

    The ink took its name from the sepia cuttlefish, which was the source of the ink. The ink was widely used in antiquity and during the Renaissance*, and it has a characteristic warm, brownish hue which is why the CSS filter that replicates this effect is referred to as the “sepia” filter.

    (* The Renaissance was a period in European history that spanned roughly from the 14th century to the 17th century. It was characterized by a significant cultural, artistic, and intellectual rebirth after the Middle Ages. The term “Renaissance” is derived from the French word meaning “rebirth”).

    Use the CSS sepia filter to an image

    Below I craft an example as an easy way to explain this amazing CSS property to you! 😃 So, let’s proceed.

    We will start with our HTML and CSS code snippets.

    The HTML snippet contains two <img> elements. The first one has the .original-image class, indicating a standard image. The second one has also the .original-image and additionally the .with-sepia, implying it’s a styled image using the sepia filter, giving it a warm, brownish tone.

    <img class="original-image"/>
    <img class="original-image with-sepia"/>
    HTML

    The .original-image class is shared by both image elements, setting a background image, ensuring it doesn’t repeat and covering the specified width and height (400px by 500px).

    The .with-sepia class applied only to the second image, introduces a sepia filter of 80%, giving a warm, brownish tone.

    .original-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: 400px;
      height: 500px;
    }
    
    .with-sepia {
      filter: sepia(80%);
    }
    CSS

    By placing the two images side by side, we can effortlessly 🧐 perceive the contrast between them. It’s absolutely fascinating, isn’t it? 😃

    Enrich filter CSS sepia property with grayscale value

    In addition, we can add the filter: grayscale(%) CSS property. Together, these filters can create a unique and visually captivating presentation, capturing the essence of both the past and the present in a single frame.

    .with-sepia {
      filter: sepia(80%) grayscale(50%);
    }
    CSS

    Wow! 🌟 This is absolutely stunning! 😃

  • How to Make Grayscale Images with CSS

    How to Make Grayscale Images with CSS

    Transforming your images into stunning black-and-white tones with depth and texture has become easier than ever with the powerful grayscale filter. Adding a touch of gray to your images creates a modern vibe and turns them into art. To achieve this effect, we use the CSS filter: grayscale(%) property.

    The grayscale filter allows you to adjust the percentage of gray. When you increase the percentage, you boost the effect. An image with 0% grayscale maintains its authentic colors, while at 100% grayscale, the image is entirely transformed into a cold, grayish tone.

    Join me 😃 in the magic of the grayscale filter, explore how it can be merged harmoniously, and enhance your techniques.

    Monochrome – Grayscale method

    The grayscale method, or in other words, monochrome photography method, captures and displays different levels of light but not different colors. It includes all forms of black-and-white photography, which produces images with shades of gray ranging from black to white.

    It originated in 1820 when Nicephore Niepce, a French inventor and photographer, created the first photograph. In today’s world, monochrome photography is mainly used for applications other than photographs.

    Applying the grayscale filter to an image

    Below, I’ve prepared an example to help clarify this property for you! 😃 So, let’s move forward.

    We begin with our HTML and CSS code snippets.

    The HTML snippet includes three img elements. The first one has the .original-image class, indicating a standard image. The second and the third also have the .original-image plus the .with-grayscale class, meaning it’s a styled image using the grayscale filter, giving it a cold, grayish tone.

    <img class="original-image"/>
    <img class="original-image with-grayscale"/>
    <img class="original-image with-grayscale"/>
    HTML

    The .original-image class is applied to all image elements, setting a background image, ensuring no repetition, and covering the specified dimensions of 400px width and 500px height.

    The .with-grayscale class applied only to the second and third images, introduces a grayscale filter. In the second case, it is set at 50%, displaying our image with some grayish tones, while in the third case, it is set at 100%, decolorizing our image and declaring a completely gray tone.

    .original-image {
      background-image: url('...');
      background-repeat: no-repeat;
      background-size: cover;
      width: 400px;
      height: 500px;
    }
    
    .with-grayscale {
      filter: grayscale(50%);
    }
    
    .with-grayscale {
      filter: grayscale(100%);
    }
    
    CSS

    By positioning the three images side by side, we can clearly 🧐 observe the contrast among them. It’s absolutely captivating. Don’t you agree? 😃

  • How to Make A Blinking Animation Using CSS

    How to Make A Blinking Animation Using CSS

    Hey there! In this post, I will show you how to create a pair of blinking eyes effect using only CSS (SCSS), complete with a smooth blinking animation to bring the eyes to life. A cow’s pair of eyes! Yes, yes, you read it right! We are dealing with a hidden 🐮 cow! Below, you can see our new 🐄 friend, with flesh and bones.

    We are dealing with a hidden 🐮 cow!

    HTML Structure

    Firstly, let’s build our blinking eyes’ initial structure using only HTML:

    <div class="face">
      <div class="eye eye--left"></div>
      <div class="eye eye--right"></div>
      <div class="eyebrow eyebrow--left"></div>
      <div class="eyebrow eyebrow--right"></div>
    </div>
    HTML

    CSS Structure

    Secondly, we continue by enhancing our HTML with CSS, starting from the body. We use flexbox as we want to place our face container in the center of our screen.

    body {
      height: 100vh; // gives us the full screen-size height
      background-color: black;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    CSS

    CSS Face Structure

    Next, we add our face container styling in order to be ready for our eyes’ positioning. We set background-color: white; just for contrast since our eyes will be black. Afterward, we use display: grid; and place-items: center; because we want our container’s children, our eyes, to get centered.

    .face {
      position: relative;
      width: 300px;
      height: 200px;
      background-color: white;
      display: grid;
      place-items: center;
    }
    CSS

    Adding the eyes

    Afterward, we continue by creating our beautiful cow’s 👀 eyes.

    .face {
      ...
      .eye {
        position: absolute;
        width: 64px;
        height: 80px;
        background-color: black;
        border-radius: 30px;
        overflow: hidden;
        &:before { // iris
          content: "";
          position: absolute;
          width: 28px;
          height: 28px;
          background-color: white;
          border-radius: 50%;
        } // end of before 
        &--left { // position of left eye
          left: 200px;
          transform: rotate(-10deg);
          &:before { // position of left iris
            top: 8px;
            right: 12px;
          }
        }
        &--right { // position of right eye
          right: 200px;
          transform: rotate(10deg);
          &:before { // position of right iris
            top: 8px;
            left: 12px;
          }
        }
      } // end of eye 
    } // end of face
    SCSS

    Adding the blinking animation

    Now is the right time, drum 🥁 roll, to add our blinking animation 😉 .

    .face {
      ...
      .eye {
        ...
        &:after { // eyelid 
          content: "";
          position: absolute;
      
          /* takes the same width as the eye (width: 64px;)
          in order to cover the whole eye when blinking */
          width: 100%;
          /* we need 0 initial height - as our starting point before the 
          animation runs */
          height: 0%;
          background-color: white;
          border-radius: 30px;
          box-shadow: 1px -2px 2px 2px white;
      
          /* here we add our "blink" animation since this is the
          html part (:after - blinking) we want to make animate.
          We want the animation to repeat every 4 seconds forever.
          */ 
          animation: blink 4s infinite ease-in;
        } // end of after
      } // end of eye
    } // end of face
    
    /* we name our animation "blink". We set different height percentages
    as a way to set our eye lids STAY IN DIFFERENT PART each time */ 
    @keyframes blink {
      0% {
        height: 0%;
      }
      30% {
        height: 0%;
      }
      33% {
        height: 100%;
      }
      40% {
        height: 0%;
      }
      70% {
        height: 0%;
      }
      75% {
        height: 100%;
      }
      78% {
        height: 0%;
      }
      81% {
        height: 100%;
      }
      84% {
        height: 0%;
      }
      100% {
        height: 0%; 
      }
    }
    SCSS

    Hint 💡
    Moreover, you should apply box shadow (check code above) to make the blinking eyelid overflow the eyes’ borders. This way during the blinking animation it will cover the whole eye and make it more realistic!

    Adding the eyebrows

    Finally, we add the 🤨 eyebrows to complete the gaze.

    .face {
      ...
      .eye {
        ...
      } /* end of eye */ 
      
      .eyebrow {
        position: absolute;
        width: 70px;
        height: 20px;
        border: solid 4px black;
        border-color: black transparent transparent transparent;
        border-radius: 50%/20px 20px 0 0;
        top: 40px;
        &--left {
          left: 40px;
          transform: rotate(10deg);
        }
        &--right {
          right: 40px;
          transform: rotate(-10deg);
        }
      } /* end of eyebrow */
    } /* end of face */
    SCSS

    Complete code solution

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

    body {
      height: 100vh; // gives us the full screen-size height
      background-color: black;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .face {
      position: relative;
      width: 300px;
      height: 200px;
      background-color: white;
      display: grid;
      place-items: center;
      .eye {
        position: absolute;
        width: 64px;
        height: 80px;
        background-color: black;
        border-radius: 30px;
        overflow: hidden;
        &:before { // iris
          content: "";
          position: absolute;
          width: 28px;
          height: 28px;
          background-color: white;
          border-radius: 50%;
        } // end of before 
            &:after { // eyelid 
          content: "";
          position: absolute;
      
          /* takes the same width as the eye (width: 64px;)
          in order to cover the whole eye when blinking */
          width: 100%;
          /* we need 0 initial height - as our starting point before the 
          animation runs */
          height: 0%;
          background-color: white;
          border-radius: 30px;
          box-shadow: 1px -2px 2px 2px white;
      
          /* here we add our "blink" animation since this is the
          html part (:after - blinking) we want to make animate.
          We want the animation to repeat every 4 seconds forever.
          */ 
          animation: blink 4s infinite ease-in;
        } // end of after
        &--left { // position of left eye
          left: 200px;
          transform: rotate(-10deg);
          &:before { // position of left iris
            top: 8px;
            right: 12px;
          }
        }
        &--right { // position of right eye
          right: 200px;
          transform: rotate(10deg);
          &:before { // position of right iris
            top: 8px;
            left: 12px;
          }
        }
      } // end of eye 
      .eyebrow {
        position: absolute;
        width: 70px;
        height: 20px;
        border: solid 4px black;
        border-color: black transparent transparent transparent;
        border-radius: 50%/20px 20px 0 0;
        top: 40px;
        &--left {
          left: 40px;
          transform: rotate(10deg);
        }
        &--right {
          right: 40px;
          transform: rotate(-10deg);
        }
      } /* end of eyebrow */
    } // end of face
    
    @keyframes blink {
      0% {
        height: 0%;
      }
      30% {
        height: 0%;
      }
      33% {
        height: 100%;
      }
      40% {
        height: 0%;
      }
      70% {
        height: 0%;
      }
      75% {
        height: 100%;
      }
      78% {
        height: 0%;
      }
      81% {
        height: 100%;
      }
      84% {
        height: 0%;
      }
      100% {
        height: 0%; 
      }
    }
    SCSS
    Expand
  • Grayscale Backdrop Filter: What It Is and How to Use It

    Grayscale Backdrop Filter: What It Is and How to Use It

    Hello there! 😃 In CSS, we have a really helpful property called backdrop-filter. It’s like a special filter that applies grayish shades to the backgrounds. A useful tool when we want to focus on a part of the webpage without changing the actual element itself. Mm… 🤔 that is a little bit tricky! Isn’t it? Let’s grab a cup of coffee ☕ and move forward! We will clarify everything in this post about using a grayscale backdrop filter. 👩‍💻

    How the backdrop filter works

    So, how does this property really work? We will begin by setting the backdrop-filter property to grayscale(percentage), where the percentage determines the intensity of the grayscale effect, ranging from 0% to 100%. A value of 100% turns the color into a complete grayscale color, while a value of 0% will keep the element’s original color shades without any grayscale effect.

    This CSS property is not quite noticeable on black or white backgrounds because it adds shades of gray, and black is already the darkest shade while white is the lightest one. In simpler words, black and white will not get converted to grayscale because it already belongs to the darkest side of grayscale.

    For the filter to work properly, as a first step, we need to set the background-color property of the parent element. Afterward, we add the backdrop filter to the child elements we want to apply the grayscale effect. The backdrop filter will only affect the child element’s background color but not its content, text, images, etc.

    As a result, the parent element maintains its original background color, while the child element’s background is desaturated, creating a monochromatic, grayscale effect.

    Create a grayscale backdrop filter effect

    This HTML and CSS code above creates a <section> element (father) with a <p> element (child) inside. The section has a background color in a shade of pinkish peach, defined by the hex color code #f99bab. Initially, the paragraph inherits the same background color.

    <section>
      <p>...</p>
    </section
    HTML
    /* PARENT ELEMENT */
    section {
      background-color: #f99bab; // a shade of pinkish peach
    }
    
    /* CHILD ELEMENT */  
    p {
      backdrop-filter: grayscale(50%);
    }
    CSS

    We continue with the <p> element styled by a backdrop-filter with a grayscale value of 50%. This effect visually desaturates the paragraph’s background (the paragraph inherits its background from its parent, the section). This means that the colors where the backdrop filter is applied (the area behind the element) will be desaturated to 50%. As a result, a grayscale effect will be applied where the colors are halfway between their original color and grayscale while leaving the content unaffected.

    In simple terms, by setting this code, we create a parent element with a shade of pinkish peach-colored background, and its child element has a somewhat muted, desaturated appearance while at the same time retaining some of the original pink color.

    Α p html element inside a section element.  They both have the same background color, a shade of pinkish peach (#f99bab).
    without grayscale
    Α p html element inside a section element. Its background color is a shade of pinkish peach (#f99bab). The child emelent had the backdrop-filter: grayscale(50%) CSS property which means the background color is a shade of pinkish peach desaturated  by 50%.
    with grayscale(50%)

    A grayscale-themed table

    Below, I have included a table that visually presents some possible variations of the grayscale backdrop filter effect. 😃

    Α table with different grayscales in each row. We have a zero grayscale, a 20%, 40%, 60% and finally 100%.
  • How To Calculate CSS Line Height

    How To Calculate CSS Line Height

    Hello there! 😃 Today, we’ll explore how we can calculate the CSS line height property, which defines the height of a line on a webpage. Each line contains the main content (text) as well as the space above and below the content ↕ (line-height). While the default value for desktop browsers is 1.2, we should keep in mind that it can vary depending on the font family. We can also set a custom line-height according to our preferences.

    ⛔ Please note that the system does not accept negative values. The default value will be automatically used if a negative value is entered.

    Below, I have drafted a plan that might help you understand things better. Check it out:

    Below is an example to explain this CSS property more analytically.

    HTML structure for CSS line height

    We create an HTML document with four headings <h1> . All headings have the same content (text) but different classes, each with unique characteristics concerning the line-height.

    <h1 class="line-height-normal">Hello World</h1>
    <h1 class="line-height-1">Hello World</h1>
    <h1 class="line-height-2">Hello World</h1>
    <h1 class="line-height-negative-value">Hello World</h1>
    HTML

    CSS structure for CSS line height

    We will move forward with our CSS code and add the necessary characteristics. The first heading has a class with line-height: normal, the second one has a class with line-height: 200px, the third one has a class with line-height: 300px, and the fourth has a class with line-height: -300px (⛔ negative value). 

    I am using different background-color for each class to easily distinguish them and notice the results clearly. 😉

    .line-height-normal {
      background-color: #ffe6cc;
      line-height: normal;
    }
    
    .line-height-1 {
      background-color: #ffcc99;
      line-height: 200px;
    }
    
    .line-height-2 {
      background-color: #ffb366;
      line-height: 300px;
    }
    
    .line-height-negative-value {
      background-color: #E2964A;
      line-height: -300px;
    }
    CSS

    In the image that follows, we can see the differences between the four headings regarding their line-height ↕.

    Utilizing the line-height CSS property can significantly improve text clarity and visual appeal, creating a comfortable reading experience for users. ✨ 😃

  • All You Need to Know About the CSS nth child Selector

    All You Need to Know About the CSS nth child Selector

    The CSS nth child selector is an extremely useful tool for targeting and styling the nth element within a group of similar HTML elements on a webpage. It’s really handy for organizing your code and helps you pick elements based on their position in a sequence. It’s also part of a larger group of selectors.

    You can apply specific styles like color or size to just that nth item or to combinations with the nth item in a list or group, making it different from all the others.

    The “nth” is a mathematical term that refers to any position in a list or sequence, such as the first, second, third, and so on. A sequence is a set of numbers or elements that follow a particular pattern or rule.

    Below are examples of utilizing this selector. Let’s proceed and explore its uses in more detail. 🧐

    We will use the same HTML code snippet for all instances. We have seven child elements inside our body element. Each one represents an ice cream cone. Yes! You read correctly! An ice cream cone! Let’s make our learning a bit fun! The empty cones remain unaffected, while the cones that have ice cream strawberry 🍓 flavor 🍦 on top are styled based on the nth-child() selector.

    <div>First child element</div>
    <div>Second child element</div>
    <div>Third child element</div>
    <div>Fourth child element</div>
    <div>Fifth child element</div>
    <div>Sixth child element</div>
    <div>Seventh child element</div>
    HTML

    Let’s imagine that what you see below would be rendered on the screen if each div child element was actually a cone.

    The CSS nth-child() selector. Seven ice cream cones side by side.

    Styling the first element with nth-child(1)

    The nth-child(1) selector targets and styles only the first element. So, in our case, we add ice cream strawberry 🍓 flavor 🍦 only to the top of the first cone. All the other cones remain unaffected.

    div:nth-child(1) {
      background-color: pink;
    }
    CSS
    Seven ice cream cones one next to other. The first cone has ice cream strawberry flavor.  All the others remain unaffected. We set the css nth-child(1) selector.

    * If we want to target the third child, use :nth-child(3), and similarly for other positions.

    Seven ice cream cones one next to other. The third cone has ice cream strawberry flavor.  All the others remain unaffected. We set the css nth-child(3) selector.

    Styling only the last element with nth-last-child(1)

    The nth-last-child(1) selector targets and styles only the last element as it counts backward. That’s why we added the word last in our selector. So that it will pick the first (1) element from the end of the list.

    div:nth-last-child(1) {
      background-color: pink;
    }
    CSS
    Seven ice cream cones one next to other. The last cone has ice cream strawberry flavor.  All the others remain unaffected. We set the css nth-last-child(1) selector.

    * If we want to pick the fourth child from the end, we set the :nth-last-child(4), etc.

    Seven ice cream cones one next to other. The fourh cone counting backward has ice cream strawberry flavor.  All the others remain unaffected. We set the css nth-last-child(4) selector.

    Select all elements apart from the last one

    Combing the nth-last-child selector with the not pseudoclass, we can target and style all elements apart from the last one. That’s why, in this example, we added the word :not and then the word last in our selector, indicating we wanted all but not the last one (1). 🤔

    div:not(:nth-last-child(1)) {
      background-color: pink;
    }
    CSS
    Seven ice cream cones one next to other. The last one remain unaffected.  All the others have ice cream strawberry flavor. We set the css :not(:nth-last-child(1)) selector.

    *If we want to pick all elements apart from the first one we can erase the last word and set the :not(:nth-child(1)) etc.

    Seven ice cream cones one next to other. The first one remain unaffected.  All the others have ice cream strawberry flavor. We set the css :not(:nth-child(1)) selector.

    Target every odd child element

    Using the word “odd” in our nth-child selector we can target and style every other element, specifically the 1st, 3rd, 5th, 7th and so on

    *Odd numbers are those that, when divided by 2, always leave a remainder of 1.

    div:nth-child(odd) {
      background-color: pink;
    }
    CSS
    Seven ice cream cones one next to other. The 1st, 3rd, 5th and last have ice cream strawberry flavor.  All the others remain unaffected. We set the css nth-child(odd) selector.

    Target every even child element

    Just as we did for odd elements, we can also target even elements by using the keyword “even” in our nth-child selector. This allows us to style every second element, specifically the 2nd, 4th, 6th, and so on

    *Even numbers are those that can be divided by 2 and leave no remainder.

    div:nth-child(even) {
      background-color: pink;
    }
    CSS
    Seven ice cream cones one next to other. The 2nd, 4th and 6th clouds  have ice cream strawberry flavor. All the others remain unaffected. We set the css nth-child(even) selector.

    Advanced math tricks with CSS nth child selector

    The nth-child() selector offers great flexibility, allowing us to create combinations that target multiple child elements simultaneously.

    Selecting elements based on multiplying by the nth-child()

    By using nth-child(3n) we target every third element. In this example, the 3rd element is the first one selected. From there, we move three steps forward forward to the next target, which is the 6th element.

    The n in 3n starts at 0 and increases incrementally with each integer (0, 1, 2, 3…).
    By multiplying n by 3 we target elements at positions that are multiples of 3 (0, 3, 6, 9…).

    div:nth-child(3n) {
      background-color: pink;
    }
    CSS
    Seven ice cream cones one next to other. The 3rd and 6th have ice cream strawberry flavor. All the others remain unaffected. We set the css nth-child(3n) selector.

    Select elements based on multiplies with the nth-child(), counting backward

    When using the nth-last-child(3n)selector we target every third element, but this time, it starts counting from the end. In our case, we have seven elements. Starting counting from the 7th one, the 5th element is the first we pick. Then we continue by taking three steps backward to the second choice, which is the 2nd element.

    The n in 3n starts at 0 and increases incrementally with each integer (0, 1, 2, 3…).
    The number 3 we assign to n shows multiplies of it, but we also have the last word in our selector, which indicates we start counting from the last child towards the first one.

    div:nth-last-child(3n) {
      background-color: pink;
    }
    
    
    CSS
    Seven ice cream cones one next to other. The 3rd and 6th have ice cream strawberry flavor. All the others remain unaffected. We set the css nth-child(3n) selector.

    Pick elements that appear after a specific point

    This one seems more complicated. 😰 Let’s analyze it a bit more! 🧐

    When using the nth-child(n + 3) selector, we are targeting the third element and all subsequent elements. If we use n + 5, it would start counting from the fifth element onward, including the fifth element itself, and continue until the end.

    The (n) starts at 0 and increases incrementally with each integer (0, 1, 2, 3…).
    The (+ 3) means that the selector starts matching from the 3rd element onward.

    Here’s how n works under the hood:

    n=0 selects the 3rd element (0 + 3 = 3).
    n=1 selects the 4th element (1 + 3 = 4).
    n=2 selects the 5th element (2 + 3 = 5).
    n=3 selects the 6th element (3 + 3 = 6).
    n=4 selects the 7th element (4 + 3 = 7).

    div:nth-child(n + 3) {
      background-color: pink;
    }
    
    
    CSS
    Seven ice cream cones one next to other. The two first remain unaffected.  From the third all the others till the end have ice cream strawberry flavor. We set the css nth-child(n + 3) selector.

    Select elements that appear up to a specific point

    Using nth-child(-n+3), the selector targets elements up to a specific point, setting a limit to which element will stop. In our case, it picks and styles all the elements up to and including the third element.

    The (n) represents 0.
    The (-n) by itselft is not used in CSS selectors because it does not correspond to any valid child positions. However when combined with a positive number (e.g. :nth-child(-n + 3)), it allows to select the child elements up to and including the specified position, such as the 3rd child.
    The (+ 3) part of the selector means that the selector matches elements up to and including the 3rd element..

    Here’s how n works in practice:

    n=0 selects the 3rd element (-0 + 3 = 3).
    n=1 selects the 2nd element (-1 + 3 = 2).
    n=2 selects the 1st element (-2 + 3 = 1).
    n=3 selects no child element (-3 + 3 = 0). 😮

    div:nth-child(-n + 3) {
      background-color: pink;
    }
    
    
    CSS
    Seven ice cream cones one next to other. The three first have ice cream strawberry flavor.  From the fourth and all the other cones till the end remain unaffected. We set the css nth-child(n - 3) selector.

    :nth-child(n + X) targets every element from the Xth onward, ensuring the Xth element is also included.
    WHILE
    :nth-child(-n + X) targets every element up to and including the Xth element.

    Pick a range of elements

    If we combine the previous two types of the nth-child selector, we can pick and style a portion of elements. In our example, all elements between the 3rd and the 6th element. Both the start (3rd element) and the end (6th element) are included.

    The :nth-child(n + 3) selector targets all children starting from the third element onward.

    The :nth-child(-n + 6) selector targets all children up to the sixth element.

    div:nth-child(n + 3):nth-child(-n + 6) {
      background-color: pink;
    }
    
    
    CSS
    Seven ice cream cones one next to other. From the third cone up to sixth first have ice cream strawberry flavor. All the other cones (1st, 2nd, 7th) remain unaffected. We set the css nth-child(n + 3):nth-child(-n + 6) selector.

    Select the first element and then every third element

    The n starts at 0 and increases by 1 with each step, representing all integer values (0, 1, 2, 3, …)

    Using the expression (n + 1) we determine which elements we want to pick.

    By applying the multiplier 3n, we instruct CSS to target every third element in the sequence.

    Combining all those charectistics we get:

    When n is 03n + 1 => 3 * 0 + 1 = 1, so it selects the 1st child.
    When n is 13n + 1 => 3 * 1 + 1 = 4, so it selects the 4th child.
    When n is 23n + 1 => 3 * 2 + 1 = 7, so it selects the 7th child, etc.

    div:nth-child(3n + 1) {
      background-color: pink;
    }
    
    
    CSS
    Seven ice cream cones one next to other. From the third cone up to sixth first have ice cream strawberry flavor. All the other cones (1st, 2nd, 7th) remain unaffected. We set the css nth-child(n + 3):nth-child(-n + 6) selector.

    Choose the first element and then every third element, counting backward

    Same as before, but this time, we start picking elements from the end, as indicated by the word last in our selector.

    The n starts at 0 and increases by 1 with each step, representing all integer values (0, 1, 2, 3, …)

    Using the expression (n + 1) we determine which elements we want to pick.

    By applying the multiplier 3n, we instruct CSS to target every third element in the sequence.

    Let’s see how that works:

    When n is 03n + 1 => 3 * 0 + 1 = 1, so it selects the 1st child from the end which is the 7th child.
    When n is 13n + 1 => 3 * 1 + 1 = 4, so it selects the 4th child.
    When n is 23n + 1 => 3 * 2 + 1 = 7, so it selects the 7th child from the end which is the 1th child.

    div:nth-last-child(3n + 1) {
      background-color: pink;
    }
    
    
    CSS
    Seven ice cream cones one next to other. From the third cone up to sixth first have ice cream strawberry flavor. All the other cones (1st, 2nd, 7th) remain unaffected. We set the css nth-child(n + 3):nth-child(-n + 6) selector.

    Choose the second element and then every third element

    The same logic applies as in the previous example, except that in this case, we are using subtraction with the selector 3n-1

    The n starts at 0 and increases by 1 with each step, representing all integer values (0, 1, 2, 3, …)

    Using the expression (n - 1) we determine which elements we want to pick.

    By applying the multiplier 3n, we instruct CSS to target every third element in the sequence.

    Now that we know what each part does, let’s dive in and see how CSS works when using this selector:

    When n is 0 then 3n - 1 => 3 * 0 - 1 = -1, so it selects no child. (I know 🥱)
    When n is 1 then 3n - 1 => 3 * 1 - 1 = 2, so it selects the 2nd child.
    When n is 2 then 3n - 1 => 3 * 2 - 1 = 5, so it selects the 5th child, etc.

    div:nth-child(3n - 1) {
      background-color: pink;
    }
    
    
    CSS
    Seven ice cream cones one next to other. The 2nd and 5th have ice cream strawberry flavor. All the other cones (1st, 3rd, 4th, 6th and 7th) remain unaffected. We set the css nth-child(3n + 1) selector.

    Using + or - in the selector may sometimes result in the same outcome. In our example, doing 3n+2 or 3n-1, ends up targeting the second element and then every third element onward. However, it’s important to understand that the underlying logic is different.

    Select the second element and then every third element, counting backward

    Same as before, but this time, we start picking from the end since we have the word last in our selector.

    The n starts at 0 and increases by 1 with each step, representing all integer values (0, 1, 2, 3, …)

    Using the expression (n - 1) we determine which elements we want to pick.

    By applying the multiplier 3n, we instruct CSS to target every third element in the sequence.

    Let’s dive in and see whats happening:

    When n is 0 then 3n - 1 => 3 * 0 - 1 = -1, so it selects no child.
    When n is 1 then 3n - 1 => 3 * 1 - 1 = 2, so it selects the 2nd child from the end.
    When n is 2 then 3n - 1 => 3 * 2 - 1 = 5, so it selects the 5th child from the end, etc.

    div:nth-last-child(3n - 1) {
      background-color: pink;
    }
    
    CSS
    Seven ice cream cones one next to other. The 3rd and 6th have ice cream strawberry flavor. All the others remain unaffected. We set the css nth-child(3n) selector.

    Select all elements

    Although the nth-child() selector is made in order to target specific positions or patterns among children, we can also use it to match all child elements at once by assigning the n inside the parenthesis ().

    The n lets the selector match elements at any position inside the parent element. This flexibility allows us to pick elements and apply styles without specifying an exact position. As a result, we can target all possible positions. 😎
    Additionally, we have all our cones full of delicious strawberry 🍓 flavor 🍦 ice cream! So, this is by far the sweetest choice!

    div:nth-child(n) {
      background-color: pink;
    }
    
    
    CSS
    Seven ice cream cones one next to other. All of them have ice cream strawberry flavor. We set the css nth-child(n) selector.

    I hope you enjoyed our little trip to the “ice cream CSS nth child” land!! 🎉 🎉 Keep going till next time!! 🍓 🍦 ✨