JOSE JESUSNavigate back to the homepage

Functional CSS

Jose Jesus Ochoa Torres
January 29th, 2021 · 4 min read

Functional CSS / Atomic CSS

A while ago, I published an article explaining how grid systems work and how you can create one from scratch. I’ll like to complement that article by talking about functional CSS (Atomic CSS); you can find this approach with both names, but be careful “Atomic CSS” is not the same as Atomic design.


What the functional CSS is?

This approach consists of creating reusable and small CSS classes with only one purpose. We can create a class to change the background color, text color, paddings, etc. These are just examples; you can use your imagination to create the CSS classes appropriate for your project.

Let me show you more examples to try to reinforce my definition above.

Updating text color:

1.text-color-red {
2 color: red;
3}
4
5.text-color-yellow {
6 color: yellow;
7}
8
9...

Updating padding:

1.padding-1 {
2 padding: 1rem;
3}
4
5.padding-top-1 {
6 padding-top: 1rem;
7}
8
9.padding-bottom-1 {
10 padding-bottom: 1rem;
11}
12
13...

Updating padding and margin

1.no-spacing {
2 padding: 0;
3 margin: 0;
4}

As you can see, it’s a regular pattern to use, one class by one CSS property, but this is not a rule. Pay attention to my last example where I’m removing the padding, and margin in the same class, so we can say that we have a one purpose CSS class.


When you can use this approach?

To try to answer this, let’s talk about my first IBM role. The official name of that role was “Web Builder” or the equivalent outside of IBM, Web Designer.

The Web builder’s goal is to create IBM web pages following standards, good practices, accessibility, security, improving performance, responsive design, and content available all time.

What do I mean by the “content available all time”?

Well, it’s Important to show content appropriately without breaking the layout in different breakpoints or resolutions.

If you look at this from the business side, you want to build web pages quickly, saving time (money money money) while following high standards, and also making sure your content is available at all times, so it makes sense to create a CSS framework limiting the development options and minimizing the error range.

As web builders, we used this CSS framework without coding a single CSS line. Maybe at this point, you are thinking then how do you add styles to the IBM pages? Easy, this CSS framework uses a lot of functional CSS combined with prefabricated elements; these elements are styled following the BEM methodology, let me use more examples to clarify this approach.

If you inspect an IBM web page with your dev tools you will find something like this:

1<div class="ibm-card ibm-card--noborder">
2 <div class="ibm-card__heading ibm-nospacing">
3 <p class="ibm-padding-bottom-1">...</p>
4 </div>
5
6 <div class="ibm-card__image ibm-padding-bottom-1">
7 <img class="ibm-resize ibm-flex" src="...">
8 </div>
9
10 <div class="ibm-card__content ibm-nospacing">
11 <p>...</p>
12 </div>
13
14 <div class="ibm-card__bottom ibm-nospacing">
15 <p class="ibm-padding-bottom-0">
16 <span class="ibm-textcolor-systems-blue-5">...</span>
17 </p>
18 </div>
19</div>

Do you recognize it? In that example, IBM uses the BEM methodology to create the element’s structure with a couple of base styles and overwriting or complementing them with Functional CSS.

  • ibm-nospacing
  • ibm-padding-bottom-0
  • ibm-padding-bottom-1
  • ibm-textcolor-systems-blue-5

The functional CSS is not used only by IBM in their internal framework, you can find more examples in popular CSS frameworks:

Bootstrap

1<span class="d-block p-2 bg-primary text-white">d-block</span>
2<span class="d-block p-2 bg-dark text-white">d-block</span>
3
4<p class="text-start">Start aligned text on all viewport sizes.</p>
5<p class="text-center">Center aligned text on all viewport sizes.</p>
6<p class="text-end">End aligned text on all viewport sizes.</p>

Bulma

1<div class="is-flex">...</div>
2<div class="is-block">...</div>
3<div class="is-inline">...</div>
4<div class="inline-block">...</div>
5
6<p class="mb-4">Margin bottom</p>
7<p class="px-1">Horizontal padding</p>
8
9<p class="mr-0 pt-3">
10 Removes the margin on the right and adds 0.75rem padding at the top
11</p>

Tailwind

1<div class="border-4 border-light-blue-500 border-opacity-100"></div>
2<div class="border-4 border-light-blue-500 border-opacity-75"></div>
3<div class="border-4 border-light-blue-500 border-opacity-50"></div>
4<div class="border-4 border-light-blue-500 border-opacity-25"></div>
5<div class="border-4 border-light-blue-500 border-opacity-0"></div>

After this short introduction, you can understand why I wanted to complement my first article (in Spanish) with this one. If you read these articles, you will have a piece of basic knowledge to understand how the most popular CSS frameworks work.


How do I implement my Functional CSS classes?

If you are only using CSS, you don’t have another option, write your classes manually, but if you are using SASS or another CSS preprocessor, this will be more fun.

Using SASS, we can use mixings to auto-generate all classes taking the information from a map, where the stored keys and values are, to be used in the future building of CSS classes.

Variables and map

Imagine we have the next SASS variables:

1/**
2* BLACK AND WHITE
3*/
4$color-black: #000000;
5$color-white: #FFFFFF;
6
7
8/**
9* BLACK AND WHITE ALPHA COLORS
10*/
11$color-black-a80: rgba(0, 0, 0, .8);
12$color-white-a50: rgba(157, 157, 157, .5);
13
14
15/**
16* GRAY COLORS
17*/
18$color-gray-1: #F5F5F5;
19$color-gray-2: #EFEFEF;
20$color-gray-3: #D3D3D3;

Let’s create a map with these variables:

1$colors: (
2 black: $color-black,
3 white: $color-white,
4 black-a80: $color-black-a80,
5 white-a50: $color-white-a50,
6 gray-1: $color-gray-1,
7 gray-2: $color-gray-2,
8 gray-3: $color-gray-3,
9);

I’m going to use the “key” as my class name and the “value” in the map like the real value of the property I select in my SASS mixin.

Classes generator

1@mixin generator($map, $property, $prefix: '-', $sufix: '') {
2 @each $key, $value in $map {
3 &#{$prefix}#{$key} {
4 #{$property}:#{$value}#{$sufix};
5 }
6 }
7}

Well, let me explain:

$map: This parameter is the data collection with the class name and their property value.

$property: This parameter could be explained by itself. I can use X params like color, background-color, font-size, display, etc.

$prefix: The parameter used to concatenate the map’s key and the class where the mixin is called.

$sufix: This is an optional parameter that I can use to set the measure type, px, rem, em, vh, etc. Or you can store the type inside the map and use this parameter to concatenate something else (!important could be a good example in some specific cases).

As you know now, I’m iterating over each map element, using the key to generate the class name and the value like the property value, and of course the property as my rule property.

Let’s see how to call the mixing:

1.text-color {
2 @include generator($colors, 'color', $prefix: '-');
3}

Pay attention I’m calling our mixing instance inside of a class, so I’m going to concatenate key values with this class, for example:

1.text-color-black { color: #000000; }
2.text-color-white {...}
3.text-color-black-a80 {...}
4.text-color-white-a50 {...}
5.text-color-gray-1 {...}
6.text-color-gray-2 {...}
7...

Another interesting thing to keep in mind is, that I’m using SASS variables in this example, and these are pre-compiled, so that means we can’t update them after being compiled; if you are working with a color theme (dark and light theme) I recommend using native CSS variables.


What classes do I usually reuse?

  • Text alignment:
1.text {
2 &-center {
3 text-align: center;
4 }
5
6 &-right {
7 text-align: right;
8 }
9
10 &-left {
11 text-align: left;
12 }
13}

NOTE: In this case, I’m not using either map or the mixing, if you want you can add these rules inside a map and iterate them.

Example of generated classes:

1.text-center { text-align: center; }
2...
  • Content alignment and distribution
1.flex {
2 display: flex;
3}

Nested map:

1$contentAlignments: (
2 justify-content: (
3 left: flex-start,
4 center: center,
5 right: flex-end
6 ),
7 align-items: (
8 top: flex-start,
9 middle: center,
10 bottom: flex-end
11 ),
12);
13
14$contentDistribution: (
15 justify-content: (
16 around: space-around,
17 between: space-between,
18 evenly: space-evenly
19 )
20);

Mixing variation:

1@mixin nestedGenerator($breakpoint, $content) {
2 @each $property, $values in $content {
3 @each $key, $value in $values {
4 .#{$breakpoint}-#{$key} {
5 #{$property}: #{$value};
6 }
7 }
8 }
9}

Calling mixing

1@include nestedGenerator($breakpoint: 'xs', $content: $contentAlignments);
2@include nestedGenerator($breakpoint: 'xs', $content: $contentDistribution);
3
4 @include sm {
5 @include nestedGenerator($breakpoint: 'sm', $content: $contentAlignments);
6 @include nestedGenerator($breakpoint: 'sm', $content: $contentDistribution);
7 }
8
9 @include md {
10 @include nestedGenerator($breakpoint: 'md', $content: $contentAlignments);
11 ...
12 }
13
14 @include lg {
15 @include nestedGenerator($breakpoint: 'lg', $content: $contentAlignments);
16 ...
17 }

NOTE: I’m using another breakpoint mixing to apply the same classes in different breakpoints. If you are not interested in working with different resolutions you can remove the $breakpoint parameter.

Example of generated classes:

1.sm-left { justify-content: flex-start; }
2.md-center { justify-content: center; }
3.lg-right { justify-content: flex-end; }
4...
  • Colors:
1$colors: (
2 black: $black,
3 white: $white,
4 gray-light-1: $gray-light-1,
5 gray-light-2: $gray-light-2,
6 gray-light-3: $gray-light-3,
7 gray-1: $gray-1,
8 gray-2: $gray-2,
9 gray-3: $gray-3,
10 gray-dark-1: $gray-dark-1,
11 gray-dark-2: $gray-dark-2,
12 gray-dark-3: $gray-dark-3,
13);
1.text-color {
2 @include generator($colors, 'color', $prefix: '__');
3}
4
5.background-color {
6 @include generator($colors, 'background-color', $prefix: '__');
7}
8
9.border-color {
10 @include generator($colors, 'border-color', $prefix: '__');
11}
1.text-color__black { color: #000000; }
2.background-color__black { background-color: #000000; }
3.border-color__black { border-color: #000000; }

NOTE: I have updated the prefix value just to help you understand what their purpose is.

  • Text:
1.text-bold {
2 font-weight: bold;
3}
4
5.text-regular {
6 font-weight: normal;
7}
8
9.text-light {
10 font-weight: lighter;
11}
12
13.text-uppercase {
14 text-transform: uppercase;
15}
16
17.h1 {
18 font-size: $text-xl;
19}
20
21.h2 {
22 font-size: $text-lg;
23}
24
25.h3 {
26 font-size: $text-md;
27}
28
29.h4 {
30 font-size: $text-sm;
31}
  • Spacing padding/margin
1$spacings: (
2 0: 0,
3 1: 0.625,
4 2: 1.25,
5 3: 1.875,
6 4: 2.5,
7 5: 3.125
8);
9
10$responsiveSpacings: (
11 r1: 2,
12 r2: 5,
13 r3: 10
14);
15
16$spacingSides: top, right, bottom, left;
17$properties: padding, margin;
1@each $property in $properties {
2 .#{$property} {
3 @include generator($spacings, #{$property}, $sufix: 'rem');
4 @include generator($responsiveSpacings, #{$property}, $sufix: 'vw');
5
6 @each $side in $spacingSides {
7 &-#{$side} {
8 @include generator($spacings, #{$property}-#{$side}, $sufix: 'rem');
9 @include generator($responsiveSpacings, #{$property}-#{$side}, $sufix: 'vw');
10 }
11 }
12 }
13}

NOTE: Take a look at how I’m not setting a measure type in my $spacings and $responsiveSpacings map. I’m sending the measure type as a parameter in the mixing (“rem” and “vh”).

Example of generated classes:

1.padding-top-1 { padding-top: 0.625rem; }
2.margin-right-1 { margin-right: 0.625rem; }
3.padding-bottom-r1 { padding-bottom: 2vh; }
4.margin-1 { margin: 0.625rem; }

Conclusion

The frontend development is changing quickly, and If you are using web components, then you might be encapsulating the CSS styles in them.

My suggestion is the next:

  • You can create a reusable web component encapsulating the basic CSS styles inside it and have small variations applying Functional CSS classes.

  • If you are not working with a grid system, it could be useful to generate a couple of Functional Classes like alignment, distribution, and padding/margin, to help you with your web component’s position in particular places.

  • You can easily create a layout if you have a suite of Functional CSS classes. It could help you deliver an MVP faster.

  • You can combine other CSS methodologies with Functional CSS.


Extra

If you want to see these code snippets, you can set up a SASS project locally or use this option online: Sassmeister

Join our email list and get notified about new content

Be the first to receive our latest content with the ability to opt-out at anytime. We promise to not spam your inbox or share your email with any third parties.

More articles from Jose Jesus

Crea tu propio CSS Grid system

¿Qué es un grid system y cómo se construyen?

October 18th, 2020 · 12 min read

CODERHOUSE (Talk): Grid systems and more

Platica en la cual se explica el origen, la evolución y estado actual de los grid systems

December 14th, 2021 · 1 min read
© 2020–2021 Jose Jesus
Link to $https://www.linkedin.com/in/jjot93/Link to $https://github.com/JoseJesusOchoaTorres/Link to $https://twitter.com/jjot93Link to $https://www.instagram.com/jose.jesus.ochoa.torres/Link to $https://www.facebook.com/jose.jesus.ochoa.torresLink to $https://medium.com/@jjotLink to $https://dev.to/josejesusochoatorres