Author: Rita

  • 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? 😎

  • Is HTML a Programming Language? Find Out Now!

    Is HTML a Programming Language? Find Out Now!

    Understanding the role of a programming language is the key 🗝 to answering the question “Is HTML a programming language?”. 🧐

    To put it simply, the answer is no, HTML is not a programming language.

    HTML stands for HyperText Markup Language, a type of markup language used for creating structured content. But what does that even mean? Let’s break it down a bit.

    Markup Language

    A markup language is a system for creating structured text in a way that both humans and machines can read. It consists of tags or elements known as building blocks. These tags are responsible for the structure of the web page defining how its content is organized and formatted. Some examples of such elements would be <h1> for headers, <a> for links, <p> for paragraphs, <footer> for footers.

    HyperText

    The term “hypertext” is part of the name because it allows text to include links – called hyperlinks – to connect to other pages. Turning text into a hyperlink makes it easy to navigate between web pages.

    By combining hypertext and markup, we get HTML, which is a language that structures web pages while also adding links to make them somewhat dynamic by connecting to other resources.

    That’s pretty cool, but even though we have defined a “smart” language, the question still remains: Is HTML a programming language?

    Getting to the answer: Is HTML a programming language?

    We can undoubtedly say that a programming language gives precise instructions to a computer or other devices to perform specific tasks, process data and create interactivity. Programming languages can build everything from simple scripts to complex applications, empowering developers to create tools and software that automate and enhance systems.

    While HTML structures and displays content, it lacks the ability to process logic or perform tasks like a true programming language.

    A quick glimpse at how HTML is written

    Below, I’ve included a simple HTML code snippet with an image to illustrate how the HTML actually works. In this case, it only defines the structure of a heading, a paragraph, a link, and a footer. It doesn’t involve processing, which is the role of programming languages. 😉

    <h1>I am the Heading</h1>
    <p>I am a paragraph with text.</p>
    <a href="https://example.com">This is a link</a>
    <footer>I am the Footer</footer>
    HTML
    Is HTML a programming language?
An image with a heading tag, a paragraph tag, a link tag and a footer tag.

    To make websites dynamic or interactive, we have to combine HTML (for structure) with CSS (for styling) and Javascript (for functionality and behavior). The last one is an actual programming language. 😃 ✨

  • 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