I'm a backend developer working on a webpage, building the CSS part. I like to write as less code as possible and reuse them throughout the site, a lot of pages.
I want to be able to reuse some CSS code. For example, if i have this code:
.box1{
background: blue;
height: 200px;
align-items: center;
}
and i have 5 other boxes or more that i need to use the code: "align-items: center;" for, does it make sense to have them such that
.box1, .box2, .box3, .box4, .box5{
align-items: center;
}
and
.box1{
background: blue;
height: 200px;
}
or declare a reusable class:
.box-align{
align-items: center;
}
The issue i have is, i have to reuse so many codes in different parts of the webpage, and i want to write as less code as possible, which i guess can have an advantage of reducing the size of the file, and make updating CSS easy and fast.
This also applies to colors too:
.box1{
color: red;
}
if i decide to declare a single class and reuse the class all over, i may have a code like this in my HTML:
<div class="box1 box-align-center box-green-color box-text-decoration ....."> </div>
<div class="box2 box-align-center box-green-color box-text-decoration ....."> </div>
<div class="box3 box-align-center box-green-color box-text-decoration ....."> </div>
but if i decide to make it clean like this:
<div class="box1"> </div>
<div class="box2"> </div>
<div class="box3"> </div>
then i'll have to do:
.box1, .box2, .box3, .box4, .....{
align-items: center;
color: red;
text-decoration: underline;
.......
}
the main issue is, these boxes will have different parents all over different pages. Does anyone have a clean code architecture for this? Just like you can declare a function, variable, or constant in OOP and reuse them all over your project.
PS: I see so many websites write CSS code and reuse them about 50 times, like having "text-align: center", and using or declaring it in about 50 classes when they could have been better optimized.