Level up your CSS with this one trick!

 I'm sure you have heard of people saying as long as you can make website look the way it should be, then it doesn't matter how you code them out. But there are a few concerns about this, we have not taken into account of:

  • Maintainability
  • Readability of code
  • Reusability of code
Let us say we want to create a layout like this:
For those people who are new to CSS, they might style their HTML and CSS as such:
Before CSS:
*,
*::before,
*::after {
    box-sizing: border-box;
}
body {
    display: grid;
    min-height: 100vh;
    margin: 0;
}
.box1 {
    background-color: green;
    color: greenyellow;
    margin: 3rem;
}
.box2 {
    background-color: blue;
    color: lightblue;
    margin: 3rem;
}
.box3 {
    background-color: red;
    color: lightgoldenrodyellow;
    margin: 3rem;
}

Before HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="better.css"></link>
    <link rel="stylesheet" href="style.css"></link>
    <title>Document</title>
</head>
<body>
    <div class="box1">This is as green box with light green box</div>
    <div class="box2">This is as blue box with light blue box</div>
    <div class="box3">This is a red box with light red box</div>
</body>
</html>

However, the problem with this is that, it is:
  • Low reusability. Imagine with another component in the website requires the same styling, but with different specification like different width, or requiring border.
  • Hard to maintain. Future maintainers would not know what properties does box1, box2 and box3 have to have such layouts.
However, all of these can be fixed if the CSS and HTML is written in a way that promotes reusability and maintainability. We can see that is applied in Bootstrap, as we can easily adjust the layout of the component by simply adding classes to the element. Taking that into mind, we can refractor our code to such a way.
After CSS:
*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    display: grid;
    min-height: 100vh;
    margin: 1rem;
}

.flex {
    display: flex;
    gap: var(--gap, 1rem);
}

.grid {
    display: grid;
    gap: var(--gap, 1rem);
}

.bg-green {background-color: green;}
.bg-blue {background-color: blue;}
.bg-yellow {background-color: red;}

.text-green {color: greenyellow;}
.text-blue {color: lightblue;}
.text-yellow {color: lightgoldenrodyellow;}


After HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="better.css"></link>
    <link rel="stylesheet" href="style.css"></link>
    <title>Document</title>
</head>
<body>
    <div class="grid">
        <div class="bg-green text-green">This is as green box with light green box</div>
        <div class="bg-blue text-blue">This is as blue box with light blue box</div>
        <div class="bg-yellow text-yellow">This is a red box with light red box</div>
    </div>
</body>
</html>

Isn't that much better? The improvement you can see are:
  • Improved clarity in HTML and CSS, you can easily understand how and what each classes do
  • Better usability, if you want to create a component of specific styling, you can easily call the class with that specific styling.

Comments

Popular posts from this blog

Support for the experimental syntax 'decorators' isn't currently enabled (5:3):

How to add environment variables into Heroku container

Running CQL scripts in Cassandra