DEV Community

Panu Viljamaa
Panu Viljamaa

Posted on • Originally published at Medium

Why CSS is Difficult

https://medium.com/@panuviljamaa/why-css-is-difficult-a2cdf0a04ca1

Top comments (2)

Collapse
 
alohci profile image
Nicholas Stimpson • Edited

CSS isn't difficult - you're making it difficult. Because you haven't understood the basic principles of HTML and CSS. HTML is a language for indicating the semantics of the content, not their styling. CSS is the language for your styling. Classes are part of HTML and therefore part of the semantics, so your red and pinkBG classes shouldn't ever exist. Your first step is to understand what it is about your HTML content that indicates why it should be red on pink. And why your code content should be navy. And why they are in contradiction. Then you can write selectors not only to bind the content to those styles, but also how to resolve that contradiction.

HTML, Selectors and CSS Declarations each have a rich syntax that work like three corners of a triangle. By limiting your selectors to a single class name, you're discarding one of those corners. Then you're wondering why creating a triangle with only two corners is so hard.

Collapse
 
panulogic profile image
Panu Viljamaa • Edited

CSS may often feel difficult. This article argues that it is difficult and gives a reason why. It is the way CSS-class combinations work: You can not change the order of their application in the place where you use them, in the HTML where you refer to multiple CSS-classes per DOM-element.

The order in which HTML refers to them has no effect, but it would be better if it did. Then the user of the style-sheet, as opposed to the author of the style-sheet could control the order in which CSS-classes are applied, per use.