CSS is broken

Sergio Zamarro
2 min readMar 23, 2017

--

Yes, CSS is broken. Anyone who has used CSS in a big project knows it. Some of the problems are these:

  • Global scope
  • Separation of technologies
  • Implicit styles
  • Dead code

How many !important do you have in your project?

162 !importans in 34 files!

I did a search on my project and found 162 !important in 34 files. This project has 4 contributors and more than 1200 commits. That is ridiculous and it’s caused by global styles

I write components so… Why global styles? If we have local styles we will not have this problem.

I love to write components in React since you can write the template and the logic in the same file… But where can I write the styles? Yes, in an external css file, but… Why separate the styles? Why do we need to write the styles in a css file?

Styled-components solve this problem, using styled-components (https://styled-components.com/) we can write styles in Javascript and create a component with those styles.

const Alert = styled.div`
color: white;
padding: 20px;
background-color: rgba(255, 130, 110, 0.77);
text-align: center;
font-weight: 700;
font-size: 18px;
box-shadow: rgba(0, 0, 0, 0.117647) 0px 1px 6px, rgba(0, 0, 0, 0.117647) 0px 1px 4px;
`

Forget methodologies and conventions such as BEM, ITCSS, SMACSS… and let’s create styled components!

How many times you spend a long time searching a css file for a view? This is because CSS is not explicit. When you create a component with styled-component you are forced to define the component styles in the same file, this is explicit. Explicit is better than implicit.

What about the dead code? How many times have you removed some html tags from the template but didn’t remove the styles? With styled-components, you will not have this problem, because if you remove the component you remove the styles!

Styled-components it’s not a hype or part of Javascript fatigue, styled-components is the future!

--

--