Methodologies that helps with styling applications

A presentation at CloudTalk - Front-End Talks in March 2022 in Prague, Czechia by Ondřej Konečný

Slide 1

Slide 1

How to structure, scale and maintain CSS Methodologies that helps with styling applications 11. 03. 2022

Slide 2

Slide 2

A little bit of history

Slide 3

Slide 3

Space Jam

Slide 4

Slide 4

How we style web sites

Slide 5

Slide 5

Layout

Slide 6

Slide 6

Tables for everything

Slide 7

Slide 7

Today, we have layout properties ➔ Float (before flexbox come into game) ➔ Flexbox ➔ Grid ➔ Subgird ➔ etc.

Slide 8

Slide 8

So, CSS is improving every day

Slide 9

Slide 9

Writing good CSS is hard “CSS: the only language that is both so easy it’s not worth learning but also so hard that it’s not worth learning.”

Slide 10

Slide 10

So why is CSS so hard to maintain? Why? ➔ Deep nesting of selectors and high specificity ➔ Nesting Hell ➔ Cascade ➔ Inheritance ➔ Misunderstanding of how CSS works

Slide 11

Slide 11

Let’s take a quick simplest example https://codepen.io/ondrejko/pen/xxpKRyo

Slide 12

Slide 12

That means that order in the resulting file matters! ≠

Slide 13

Slide 13

Can you check for e.g. selector order in the resulting CSS file?

Slide 14

Slide 14

Because there is thousands of lines Current Dashboard CSS file with 8200 line of code:

Slide 15

Slide 15

Deep nesting of selectors and high specificity HTML: CSS:

Slide 16

Slide 16

The link color has not changed What is in CSS and what blocks me?

Slide 17

Slide 17

What are the options? Selector overload (or refactor code or use !important) Such a selector can be really hard to modified

Slide 18

Slide 18

Nesting Hell

Slide 19

Slide 19

You would say, that is not so bad, but.. Output in style.css:

Slide 20

Slide 20

“Nesting selectors” is a good friend but a bad lord It is alway good approach to stay simple as possible

Slide 21

Slide 21

Takeaway: Avoid nesting as much as possible If you need to nest selectors in the third level, something is wrong with design/usage of the component and we call it “Design smell”.

Slide 22

Slide 22

Cascade

Slide 23

Slide 23

We often end up like this

Slide 24

Slide 24

Our current Dashboard

Slide 25

Slide 25

What we can do? Use some good approaches

Slide 26

Slide 26

ITCSS

Slide 27

Slide 27

ITCSS

Slide 28

Slide 28

ITCSS Settings — Space for preprocessors with variables such as colors, design tokens, typography, grid. Tools — Layer with mixins, functions, media queries. Generic — Here we insert styles for third party libraries such as normalize, reset or any others Elements — Selectors for bare HTML elements such as h1, p, article, a Objects — Class definitions for layout, grid, indentation - reusable non-decorative styles. Components — Specific components across the project - accordion, buttons, breadcrumbs, tooltip. Utilities — Class utilities that are designed to affect one particular CSS property and are in most cases written with the utmost importance. Utilities and helper classes with ability to override anything which goes before in the triangle.

Slide 29

Slide 29

ITCSS structure is great for any project and it is easy to use

Slide 30

Slide 30

BEM

Slide 31

Slide 31

What is important to realize? “The more experienced developer you are, the more you prefer code readability to efficiency”

Slide 32

Slide 32

Why BEM? ➔ Find and write CSS rules in a large project is easy. ➔ Organize rules for media queries and reusable libraries. ➔ Reduce the complexity and nesting of your CSS selectors. ➔ Have a consistent approach to positioning elements on the page. ➔ Have a consistent approach to changing the look of HTML. ➔ Have a consistent approach to composing larger components from smaller components. ➔ A unified approach that is easy to explain to newcomers ➔ It keeps the world of CSS safe from mess and clutter.

Slide 33

Slide 33

BEM is G. R. E. A. T

Slide 34

Slide 34

G. R. E. A. T G for Global BEM is one of the most recognized naming conventions out there. So if you are introducing a new team member to your BEM project, there’s a good chance they already know the convention, which reduces initial friction and allows them to be productive since day 1.

Slide 35

Slide 35

G. R. E. A. T R for Readable Thanks to descriptive class names given to basically every element, the stylesheet is easy to read on its own. Not only selectors look better, they also work faster than deeply nested ones.

Slide 36

Slide 36

G. R. E. A. T E for Expandable As the specificity of CSS selectors is minimal, adding another variation is very simple. Single modifier class should be enough — no more ‘at least equal selector weight’ toil.

Slide 37

Slide 37

G. R. E. A. T A for Adaptable Sharing the philosophy of modularity, BEM naturally works fine with frameworks. Also, styling is independent of elements type and nesting, making it less prone to break when tackling with document structure.

Slide 38

Slide 38

G. R. E. A. T T for Tough There are only two hard things in Computer Science: cache invalidation and naming things. When you start following BEM (fully and honestly), you’ll probably find yourself struggling with it constantly. Paradoxically, it’s a good thing: ➔ finding proper block names makes the code clean and legible to others (your future self included) ➔ ➔ reusing existing blocks avoiding multi-level nesting makes you rethink document structure

Slide 39

Slide 39

Tailwind, and why I would consider not using it

Slide 40

Slide 40

Tailwind is good for

Quickly prototyping

Safety - there is nothing in design that is not in config

Small projects like personal sites, blog sites etc.

Slide 41

Slide 41

Tailwind is (IMHO) not good for large scaled projects - Styling and HTML are Mixed

It Takes Time to Learn (not necessarily a disadvantage)

Lack of Important Components (not so much components)

Components aren’t provided by default

Unreadable class names

It’s Inconsistent (items-: align or justify? content-: align or justify?)

It’s Difficult to Read

Really hard to do code reviews

You Can’t Chain Selectors

Tailwind Locks You Into the Utility CSS Paradigm

Tailwind Is an Unnecessary Abstraction

Tailwind, Dev Tools, and Developer Experience (Imposible create variants)

Slide 42

Slide 42

It’s Inconsistent items-: align or justify? content-: align or justify? justify-: content or items? align-: content or items?

Slide 43

Slide 43

It’s hard to read

<div class=”w-16 h-16 md:w-32 md:h-32 lg:w-48 lg:h-48”></div> <div class=”w-16 h-16 rounded text-white bg-black py-1 px-2 m-1 text-sm md:w-32 md:h-32 md:rounded-md md:text-base lg:w-48 lg:h-48 lg:rounded-lg lg:text-lg” > Yikes. </div>

Slide 44

Slide 44

It’s hard to read

Slide 45

Slide 45

More pleasure for eyes

Slide 46

Slide 46

You Can’t Chain Selectors

Slide 47

Slide 47

It’s Harder to Tweak CSS in Dev Tools ➔ It is hard to simulate styling in DevTools ➔ It’s Harder to Find Components in Dev Tools ➔ Recompiling HTML Is Slower Than Recompiling CSS

Slide 48

Slide 48

Tailwind Is Still Missing Some Key Features of CSS What will be in CSS specification soon? What is in working drafts? ➔ Container Queries ➔ :has() selector ➔ @when/@else rules ➔ Cascade Layers ➔ Subgrid ➔ Nesting