I’ve never been much for commenting my own code, until I realised how much it helps other developers understand my logic, approach and it shorten the time they spend on getting familiar with my projects.
CSS can be complex, especially when working on larger projects, and well-written comments can make it easier to maintain and modify your code over time.
Here are a few reasons why adding comments to your CSS is important:
When writing comments in your CSS code, it’s important to follow a few best practices to ensure that they are effective:
Here’s an example of a well-commented SCSS document according to my own standards.
/* Card used for archive page
----------------------------- */
.card {
// Layout
border-radius: .625rem;
height: 100%; /* Use full height of the parent */
background: var(--black-3); /* Set card bg color */
// Image inside the card
&__image {
display: block; /* Display as an block element */
}
// Description after the image
&__description {
font-size: .825rem; /* Override default <p> size */
}
// Btns in the end of the card
&__button {
// Layout
display: flex; /* Use flex to align the btns */
flex-direction: row; /* Align btns side-by-side */
justify-content: space-between; /* Space between start and end */
}
}
In the end of the day, it’s up to your whether you want to comment your code. If you “lone wolf” a project, I can understand why you don’t want to comment your code, but it will be wise, if you’re having more than one front-end developer on a project.