Write better CSS by borrowing ideas from JavaScript functions — Smashing Magazine
I like to think of writing CSS as writing functions that describe how your layouts respond to change. When we forget the principles of writing a good function, here are some of what can happen:
- We are losing time.
When we have to worry about side effects, changes take longer. - We create bugs.
My favorite example is an online store where the “Buy” buttons are hidden due to abuse of viewport units. - We build fewer features.
When changes are scary and time-consuming, they often don’t happen.
Let’s see how we can borrow best practices and ideas from writing good JavaScript functions for writing CSS that is easy to use, free of unwanted side effects, and resilient to change.
Avoid unwanted side effects
When you change something in your system, it shouldn’t surprise anything else. This is as true for CSS as it is for JavaScript functions.
Let’s look at this circled arrow icon as an example:
That looks good, but let’s say we want a narrower arrow icon:
Now the enclosing circle is squashed! This is an example of an unwanted side effect. Using a narrower arrow destroys the shape of the circle.
If we inspect the element in DevTools, we can see that the shape of the containing...
source: https://news.oneseocompany.com/2023/04/28/write-better-css-by-borrowing-ideas-from-javascript-functions-smashing-magazine_2023042844152.html
Your content is great. However, if any of the content contained herein violates any rights of yours, including those of copyright, please contact us immediately by e-mail at media[@]kissrpr.com.