|

ITCSS: Comprehensive Guide & Starter Framework

Let’s dive into ITCSS in this comprehensive guide. Kickstart your projects with a handy starter framework.

TL;DR;

In a nutshell, ITCSS is a CSS architecture that helps you organize and structure your stylesheets in a logical and efficient way. Here’s a rundown of the different segments of the inverted triangle and what they’re all about:

  1. Settings: Define variables, contains no CSS output.
  2. Tools: Contains mixins & functions, should also contain no CSS output.
  3. Generic: Contains resets & normalizing styles for HTML elements.
  4. Elements: Provides default styling for basic HTML elements.
  5. Objects: Contains styles for layout/structuring page content.
  6. Components: Styles for standalone UI components.
  7. Utilities: Helper classes to make tweaks to Objects or Components.

In this guide, we’re diving headfirst into the world of ITCSS – the Inverted Triangle CSS architecture. This method is all about maintaining large-scale CSS and making it scalable, manageable, and efficient. I’ll be laying out everything from the basics of ITCSS, its clever layered pyramid, right through to the nitty-gritty of implementing it in your projects. Plus, I’m giving you a ready-to-use ITCSS starter framework for your new projects. So, if you’re ready to revolutionize your CSS game, stick around. It’s time to master ITCSS!


Setting the Stage for ITCSS

Hey there, frontender! I’m thrilled you’re here to venture into the realm of ITCSS with me. If you’ve ever found yourself wrestling with CSS – maybe your stylesheets are growing faster than bamboo in the rain, or your CSS architecture feels like it’s held together with duct tape – then ITCSS is just the lifebuoy you need.

ITCSS, short for Inverted Triangle CSS, is a smart, scalable, and maintainable architecture that’s been making big waves in the web development ocean for several years now. It’s not just a passing trend or a cool acronym (though it is a pretty cool acronym if you ask me). No, ITCSS is a real game-changer. It’s a methodology that can take your CSS from a tangled mess to an organized, efficient, well-oiled machine.

In this comprehensive guide, we’ll break down the ITCSS pyramid layer by layer, delve into its rich history, and uncover the benefits it brings to your CSS projects. I’ll be your tour guide, providing step-by-step instructions, real-world examples, common pitfalls to avoid, and even a handy ITCSS project architecture starter kit. Whether you’re a seasoned developer or new to the world of CSS, there’s something here for you.

So, pull up a chair, grab a cup of your favorite brew, and let’s embark on this ITCSS adventure together!


The ITCSS Origin Story

Before we delve into the core of ITCSS, let’s do a bit of time travel. Back in 2016, Harry Roberts introduced us to a new CSS methodology that was destined to leave a mark on the web development community: ITCSS. At the time, there were scarce resources on ITCSS, making it a unique and intriguing approach to tackling CSS architecture​.

As the methodology grew, so did the interest. An early article about ITCSS, sharing first-hand experience with its implementation, attracted significant attention, landing at the top spot in Google search results, and has remained there ever since. Over the years, this article accumulated 290k views, shedding light on the increasing interest in ITCSS​.

However, like all things in tech, interest has fluctuated. As reported in the State of CSS 2020 findings, interest in ITCSS dipped slightly, from 40% to 37% within a year. Despite this, it’s worth noting that ITCSS landed in the ‘low usage / high satisfaction‘ quadrant in the Satisfaction vs. Usage diagram from the same report, suggesting that those who use it, love it​.

ITCSS Interest
Source: State of CSS 2020

ITCSS, being partially proprietary, does not have open-source documentation available. This has somewhat stymied its widespread adoption. Yet, respect for Harry Robert’s intellectual property remains high, and the best way to learn ITCSS remains through his Skillshare class.

This trip down memory lane offers a glimpse into the initial splash ITCSS made in the web development world and its journey so far. The evolution of ITCSS is a testament to the dynamic nature of web development and the ever-evolving quest for more efficient, scalable, and maintainable CSS solutions.


Diving Into the ITCSS Triangle

Alright, now that we’ve set the stage, let’s roll up our sleeves and get into the crux of the matter: What exactly is ITCSS?

ITCSS, or Inverted Triangle CSS, is like a secret weapon for your CSS projects. It’s not just a fancy acronym, but a systematic approach to managing your CSS files, tackling issues like global namespace, cascade, and selectors specificity head-on.

The ITCSS pyramid, as its name suggests, is an inverted triangle, a visual representation of how your CSS should be structured. It starts broad and generic at the base and gradually becomes more specific and explicit as we ascend, mirroring the natural cascade of CSS. Each layer of the pyramid serves a unique purpose, contributing to the overall maintainability and scalability of your stylesheets.

A Roadmap to CSS Success

Think of the pyramid as a roadmap. It guides you from the base, where you define global settings and tools, to the summit, where utility classes hold the power to override anything that comes before. With each layer, you’re building upon the previous one, adding another piece to the puzzle that is your project’s CSS.

One of the reasons ITCSS is such a darling in the web development community is its compatibility. You can use it alongside preprocessors, and it plays nicely with other CSS methodologies like BEM, SMACSS, or OOCSS. It’s not a disruptive force but a harmonious companion to your existing CSS methods.

ITCSS Segments Broken Down

ITCSS is like a well-organized party, where each guest (or layer, in this case) has a specific role to play:

1. Settings Segment

Okay, let’s kick things off with the first layer of our ITCSS pyramid: Settings. This is where you lay the groundwork for your entire CSS project. Think of it as the blueprint for your CSS architecture.

In the Settings layer, you define variable configurations for various elements like colors, fonts, sizes, and so forth. It’s all about getting those foundational aspects right before moving forward. Consider it like selecting the paint, bricks, and mortar before you start building a house.

Here’s a simple example of what your settings file could look like when using a preprocessor like Sass:

// _settings.scss

$font-primary: 'Open Sans', sans-serif;
$font-secondary: 'Roboto', sans-serif;

$color-primary: #3498db;
$color-secondary: #2ecc71;
$color-tertiary: #e74c3c;

$spacing-small: 10px;
$spacing-medium: 20px;
$spacing-large: 30px;

In this example, we’ve defined variables for our primary and secondary fonts, as well as our primary, secondary, and tertiary colors. We’ve also set up some standard spacing sizes that we can use throughout our CSS. These variables act as a reference point, ensuring consistency and ease of use across your CSS project.

Remember, the Settings layer doesn’t output any CSS. It’s all about setting up those handy variables that you’ll use in the layers to come​. It’s a vital step in establishing a maintainable and scalable CSS architecture, and it’s what makes ITCSS such a powerful tool.

2. Tools Segment

Next up, we have the Tools layer. This is where we store globally used mixins and functions. These are essential pieces of code that you’ll reuse throughout your CSS, kind of like the go-to tools in your developer toolkit.

This layer is specific to pre/post-processing languages like Sass or PostCSS, as mixins and functions aren’t supported in native CSS. In essence, it’s your very own collection of handy CSS helpers that you can call upon when needed.

Here’s an example of what this might look like in a Sass project:

// _tools.scss

@mixin transition($properties) {
    transition: $properties;
}

@mixin box-shadow($x, $y, $blur, $color) {
    box-shadow: $x $y $blur $color;
}

@mixin transform($transform) {
    transform: $transform;
}

In this Tools layer, we’ve defined a few mixins that we can use across our CSS. These include a mixin for applying CSS transitions, one for applying a box shadow, and one for applying CSS transforms.

Like the Settings layer, the Tools layer does not output any CSS by itself. Rather, it provides the tools (mixins and functions) that you’ll use in the subsequent layers of your ITCSS architecture.

It’s all about setting you up for success and making your CSS development process as smooth as possible. So, with the right settings in place and your tools at the ready, you’re well-prepared to dive deeper into the ITCSS framework.

3. Generic Segment

The third layer in the ITCSS architecture is the Generic layer. Picture this layer as the foundation of your CSS house – it’s where you lay down the groundwork for everything that follows.

This layer includes CSS resets and normalizing rules. Resets are used to strip away the default browser styles, giving you a blank slate to work with. Normalizing rules, on the other hand, make sure that styles render consistently across different browsers.

Here’s an example of what this might look like:

/* Generic Layer */
/* ==================== */

/* Reset Styles */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Normalize Styles */
html {
    font-size: 16px; /* Base font size for rem units */
}

body {
    line-height: 1.5; /* Improved readability */
    color: #333; /* Default text color */
}

In this example, we’ve first included a reset rule that sets the margin and padding of all elements to 0, and applies box-sizing: border-box to everything. This ensures that the dimensions of elements include padding and border sizes, making layout calculations more straightforward.

Next, we’ve included some normalizing rules. We set a base font size on the html element, which is used for calculating rem unit values. On the body, we’ve set a default line-height and color to improve the readability of text.

With this Generic layer in place, we’ve effectively created a solid, consistent foundation for the rest of our styles. This is just the beginning of our journey through the ITCSS architecture, so let’s keep moving forward​.

4. Elements Segment

Next up on our ITCSS adventure is the Elements layer, a key player in the process. Think of this layer as the interior decorator of our CSS house. It’s where we add a touch of personality to the bare HTML elements, like h1, button, and so on.

In this layer, we provide default styling for these basic HTML elements. These styles are not tied to any specific classes or ids, they are universally applied to these elements across your entire project.

Here’s an example of what the Elements layer could look like:

/* Elements Layer */
/* ==================== */

/* Headings */
h1, h2, h3, h4, h5, h6 {
    font-weight: bold;
}

/* Links */
a {
    color: blue;
    text-decoration: none;
}

/* Buttons */
button {
    background-color: green;
    color: white;
    border: none;
    padding: 10px;
    cursor: pointer;
}

In this example, we’ve styled headings to be bold, links to be blue and without an underline, and buttons to have a green background with white text, no border, a bit of padding, and a cursor change on hover.

Remember, the styles you provide here should be general enough to be a good default but specific enough to give your site a cohesive look and feel. With the Elements layer complete, we are ready to move on to more complex components in our ITCSS architecture​.

5. Objects Segment

The fifth layer in our ITCSS structure is the Objects layer. This is where our website starts to take shape. This layer houses the style rules for elements that are responsible for the layout or structuring of our page.

In the Objects layer, we create style rules for class-based selectors that define undecorated design patterns. These aren’t tied to any specific component but serve as the backbone of many components.

Think of these as the building blocks that we will use to construct our website. They are like the Lego pieces that we can use and reuse to build various sections of our site.

Here’s an example of what you might find in the Objects layer:

/* Objects Layer */
/* ==================== */

/* Container */
.container {
    width: 80%;
    margin: 0 auto;
}

/* Flex container */
.flex-container {
    display: flex;
    flex-wrap: wrap;
}

/* Grid container */
.grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}

In this example, we have a .container class for centered content, a .flex-container class for flexbox layouts, and a .grid-container for grid layouts. These classes can be applied to any HTML element to quickly and consistently apply these styles.

But remember, the key here is to keep these styles undecorated and general enough to be reusable across different components. We’re creating a toolbox of classes that can be used to build out more complex components in the following layers of our ITCSS architecture​.

6. Components Segment

The sixth layer, where a significant portion of our work takes place, is the Components layer. This is where we start to see the website come alive as we begin to define style rules for specific UI components.

In the Components layer, we focus on writing style rules for distinct sections of our UI. This can include components such as navigation bars, footers, cards, modals, and many others. We often compose these UI components using the building blocks defined in our Objects layer.

The Components layer is where we see the beauty of ITCSS come to life. By this stage, we’ve built up a solid foundation of reusable classes and design patterns that we can now use to craft our unique components.

Here’s an example of what you might find in the Components layer:

/* Components Layer */
/* ==================== */

/* Navigation bar */
.navbar {
    background-color: var(--primary-color);
    padding: 1rem;
    display: flex;
    justify-content: space-between;
}

/* Footer */
.footer {
    background-color: var(--secondary-color);
    padding: 2rem;
    text-align: center;
}

/* Card */
.card {
    border: 1px solid var(--border-color);
    padding: 1rem;
    border-radius: 4px;
}

In this example, we have a .navbar class for our navigation bar, a .footer class for our footer, and a .card class for card components. These classes can be applied to any HTML element to quickly and consistently apply these styles.

At this stage, we’re starting to see the benefits of our organized and scalable CSS architecture. By building up from general to specific, we’ve made it easier to manage our CSS and have set ourselves up for success as our project grows​.

7. Utilities Segment

Last but definitely not least, we have the Utilities layer, also known as the Trumps layer. This is our final layer and it serves a very specific and crucial purpose in our ITCSS architecture.

The Utilities layer is all about helper or utility rules that are designed to make small tweaks to our Objects and Components layers. These rules can adjust and override existing styles when necessary. Think of it as our secret weapon for dealing with those pesky edge cases and one-off style needs.

In the Utilities layer, we might define helper classes such as .hide to hide an element, .text-center to center text, or .mt-1 to add a specific amount of top margin. These utilities can then be applied directly to HTML elements as needed, providing us with a powerful tool for quick and easy adjustments.

Here’s an example of what you might find in the Utilities layer:

/* Utilities Layer */
/* ==================== */

/* Hide element */
.hide {
    display: none;
}

/* Center text */
.text-center {
    text-align: center;
}

/* Margin top 1rem */
.mt-1 {
    margin-top: 1rem;
}

In this example, we’ve defined a .hide class to hide an element, a .text-center class to center text, and a .mt-1 class to add a top margin of 1rem. We can use these utility classes as needed throughout our HTML to make precise adjustments.

With the Utilities layer, we’ve reached the end of our ITCSS journey. We’ve built a solid, scalable, and maintainable CSS architecture that can grow with our project. Now it’s time to dive in and start coding!


ITCSS with BEM

Now, before I get ahead of myself, let’s take a step back. I know, I know, you’re itching to dive right into the world of ITCSS and BEM. But for the uninitiated, BEM is one of the most popular CSS naming schemes out there. It stands for Block-Element-Modifier, and it’s got a pretty unique syntax.

In the BEM world, blocks are classes for individual elements that can stand alone and be replicated. Elements, on the other hand, are always part of a block, while modifiers… well, they modify a block or an element to tweak its appearance. Think on/off, active/inactive, fixed, static, highlight/neutral, you get the idea.

So, BEM gives us a solid foundation. But what happens when we inject some ITCSS into the mix? We get BEMIT, a naming convention that integrates the best of BEM with the scalability of ITCSS.

In BEMIT, blocks have names with either no separation, or names separated by one dash or one underscore. Elements use two underscores, representing internal elements consistent with their block. Modifiers use two dashes for identification. Here’s a taste of what BEM syntax looks like:

.form { }
.form--theme-xmas { }
.form--simple { }
.form__input { }
.form__submit { }
.form__submit--disabled { }

Harry Roberts took BEM and extended it to become BEMIT. He didn’t add new types of classes – we still have Blocks, Elements, and Modifiers – but he did add usage and state information.

In BEMIT, namespaces are defined for objects and appear as prefixes for each major class name. They break down as o- for objects, c- for components, and u- for utilities (like clearfix or text centering). Let me give you an example of what typical BEMIT naming conventions look like:

<div class="o-media  c-user  c-user--premium">
  <img src="" alt="" class="o-media__img  c-user__photo  c-avatar" />
  <p class="o-media__body  c-user__bio">...</p>
</div>

Harry also recommends using the @ suffix for classes based on media styles. So, .o-media might become .o-media@lg for large screens, and .o-media@md for mid-sized screens.

Whether you choose to use these additional suffixes or stick with common media queries and rewrite classes at different breakpoints is up to you. Both methods have their pros and cons, and the best one really depends on your specific project needs.


An ITCSS Starter Framework

Hey there, I’m super stoked to introduce you to the world of inuitcss! This isn’t just another CSS framework. No, it’s an extensible, scalable, Sass-based, OOCSS framework built to stand the test of time on large and long-lasting UI projects​.

It’s time to make your CSS journey all the more exciting. With inuitcss, you’re not just given a UI design on a silver platter. Instead, you’re equipped with a rock-solid architectural foundation to build upon, and make your own masterpiece

Here’s a little taste of what you can expect when you dive into the world of inuitcss:

  • ITCSS Layers Already Laid Out: The very core of inuitcss is based on ITCSS principles. It comes with predefined layers that guide you in structuring your CSS so that it’s both scalable and a breeze to manage.
  • BEM Naming Convention: Inuitcss speaks the language of BEM. It helps you ensure your classes are well-named and your front-end code remains clean, easy to read, and understandable.
  • Works Harmoniously with Preprocessors: Whether you’re using Sass, LESS, or PostCSS, inuitcss is built to blend in smoothly. It leverages the power and flexibility of these preprocessors to give you the best coding experience​.
  • Straightforward Installation: You can install inuitcss using your preferred package manager like npm or yarn. It can also be installed using Bower or even by copy-pasting, though the latter is not recommended as it lacks the ease of managing and updating inuitcss as a dependency.
  • Getting Started is Easy: Once installed, there are a few steps to follow to get started. You’ll be working with example files that give you a clear idea of how to structure your CSS, manage settings, and even create your own components. It’s like a little playground to get comfortable before you start building for real​.
  • Core Functionality: inuitcss requires your base font-size and line-height to get started. These settings become the cornerstone of your UI, providing a default margin and padding value for your components. This approach allows for a free vertical rhythm and reduces magic numbers in your codebase, making everything more logical and rational.
  • Highly Configurable: Want to modify inuitcss? Easy peasy! It’s highly configurable, and you can easily override its default settings with your own values before importing specific files.

So, are you ready to dive into the world of inuitcss? I promise it’s going to be a thrilling ride. As you embark on this journey, don’t hesitate to throw any questions my way. I’m here to guide you every step of the way. Let’s make CSS more fun together!


Pitfalls & Misunderstandings

Alright, here we go! This section is all about some of the common issues and misunderstandings folks often face when working with ITCSS (Inverted Triangle CSS). Let’s clear the air!

Not a One-Size-Fits-All Approach

First things first, ITCSS is not a one-size-fits-all approach. It’s super flexible, letting you tailor it to suit your workflow and tooling needs. But this flexibility can sometimes lead to confusion. Many of us worry about the amount of boilerplate involved in setting up ITCSS. But hey, guess what? That’s totally up to you! ITCSS doesn’t force you to have all layers present, only specifying the order they should be in if they are present.

Naming Conventions

Another common misunderstanding is the naming convention. ITCSS recommends using BEMIT (Block-Element-Modifier Inverted Triangle), which is an extension of BEM (Block-Element-Modifier). This naming convention might look a bit complex at first, but it’s there to help you focus on solving front-end challenges rather than sweating over class names and their locations. Trust me, once you get the hang of it, it’ll be a lifesaver!​

Organizing ITCSS Layers

Then there’s the issue of organizing ITCSS layers. It’s best to organize ITCSS layers into subfolders, especially if you’re using a preprocessor like Sass. However, remember to store each component in its own file. Mixing styles from different components in one file is a big no-no! I’ve seen it lead to a lot of headaches down the line.

Nesting in ITCSS

Now, this is a big one: Nesting. Limit your nesting to 2 levels. I know, I know, it’s tempting to go for deep nesting, especially when you’re using Sass and that handy parent selector (&) is just a keystroke away. But trust me, overqualified selectors resulting from deep nesting are against the spirit of ITCSS. And in most cases, a flat structure with full selectors expanded is just as good, if not better.

There’s No Rule Book

Lastly, ITCSS doesn’t come with a rule book. It’s mostly proprietary, so no detailed rules exist about its usage, just a set of specific principles. And this is where the real confusion often lies. But hey, that’s also the beauty of it! You can mix and match concepts from other methodologies to create your own hybrid workflow. Plus, there’s no ITCSS compliance checker to breathe down your neck. You have the freedom to slightly alter the model to fit your needs.

To wrap it up, while ITCSS might seem a bit complex at first, once you get the hang of it, it’s a highly effective tool for organizing your CSS in a scalable and maintainable way. Just remember to be flexible, keep your classes organized, limit your nesting, and make the methodology your own!


Wrapping Up

Well, there you have it, my friend! Our journey through the exciting world of ITCSS has come to an end. From understanding the ins and outs of this magnificent CSS architecture, to delving deep into the layers of the ITCSS pyramid, we’ve covered it all.

Remember, ITCSS is more than just another tech acronym to impress your developer buddies. It’s a game-changer that can radically transform the way you handle CSS in your projects. It’s about creating a scalable and maintainable CSS architecture that won’t drive you mad as your project grows.

Keep exploring, keep learning. ITCSS is a powerful tool, but like any tool, it becomes even more potent when wielded with skill and understanding. So go ahead, dive in, mix it up with BEM, and see how this inverted triangle can turn your CSS world right side up.

I hope you’ve found this guide helpful. I can’t wait to hear how you’re using ITCSS in your projects. Feel free to share your experiences, challenges, triumphs, and yes, even your epic fails. Remember, every stumble is just another step forward on the path to becoming a CSS master.

So, ready to conquer the CSS world with ITCSS? Let’s do this!

Share Your Thoughts

Your email address will not be published. Required fields are marked *

Latest Articles