Home Web Front-end CSS Tutorial Why add Tailwind CSS Atomic Classes to project ❓

Why add Tailwind CSS Atomic Classes to project ❓

Dec 12, 2024 am 11:40 AM

Problem

  • When projects with many UI developers, start to code components in their own way, each declaring their own custom css classes, as per need.

Traditional way

When we consider a simple well know issue to center a "div" in a page, this is how we typically create a class, with all the necessary styling.

<template>
    <div>



<p>output :- </p>

<p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173397481225444.jpg" class="lazy" alt="Why add Tailwind CSS Atomic Classes to project ❓"></p>

<p>Pros :- </p>

Copy after login
  • Developers can add any styling for the classes as they please.

Cons :-

  • As project grows, there won't be any uniformed styling for the project.
  • It becomes tedious to apply same styles for new modules, as develepoers need to apply them themselves.
  • Developer intent is not clear, i.e class name is "center-div" but inner styling can be anything they desire.

Tailwind philosophy

Building complex components from a constrained set of primitive utilities.

  • We need to break a component classes from group up with Atomic classes.
<template>
    <div
       >



<p>Output</p>

<p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173397481369954.jpg" class="lazy" alt="Why add Tailwind CSS Atomic Classes to project ❓"></p>

<p>What Happened, where are the classes ⁉️</p>

Copy after login
  • As you can see from the code above we have used quite a few classes in our "div"

class="box-border absolute flex justify-items-center top-1-2 left-1-2 fill-gray-alpha color-fill max-w-sm"

  • Each class is registered in the global application scope, which goes like this
.box-border {
    box-sizing: border-box;
}

.absolute {
    position: absolute;
}

.flex {
    display: flex;
}

.justify-items-center {
    justify-items: center;
}

.top-1-2 {
    top: 50%
}

.left-40-p {
    left: 40%;
}

.max-w-sm {
    max-width: 24rem; /* 384px */
}
Copy after login
  • Since all these classes are available in the global scope, any developer can use them freely.

Pros

  • Reduces CSS Bundle size significantly.
  • Ensures component styling to be consistent across the team.
  • Developers can rapidly prototype their ideas, with less effort on redoing the same style.

Cons

  • Learning curve, each developer needs to get familiar with classes that already existing.
  • Project needs to keep proper documentation, when adding these Global declared classes for others to consume.

Vue JS Pitfalls

:deep() / ::v-deep

  • the bain ? of Vue JS css targeting.

Traditional Classes

  • Targeting and Applying styling for the classes is very easy
div {
    ::v-deep .center-div {
        background-color: red;
    }
}
Copy after login

Taiwind Classes

  • Developers need to get very creative when targeting "div"
div {
    ::v-deep :first-of-type {
        background-color: red;
    }
}
Copy after login

How to Introduce Tailwind CSS Classes into your Application

Traditional way

  • we can easily install them with

Tailwind Installation

npm install -D tailwindcss
npx tailwindcss init
Copy after login
  • but this will install (i.e register) a plethorah of all the classes, in the global scope.

Unconventional ? way

  • when your Application already has an existing css library, it would be ideal to cherry pick, the classes we need and add them in one css file and register it globally in the App.

  • Say your Application only wants flexibility in flexbox styling
    -- Get a list of the classes you need from flex styles

  • cherry pick the classes you ? assume ? your application needs, and add them as per need.

  • with this way we can keep the CSS bundle significanlty small, but the development team needs to have tight control over css they apply ?.

/* FlexBox */
.flex {
    display: flex;
}

.flex-row {
    flex-direction: row;
}

.flex-row-reverse {
    flex-direction: row-reverse;
}

.flex-col {
    flex-direction: column;
}

.flex-col-reverse   {
    flex-direction: column-reverse;

}

.flex-wrap {
    flex-wrap: wrap;
}

.flex-nowrap {
    flex-wrap: nowrap;
}

.grow {
    flex-grow: 1;
}

.grow-0 {
    flex-grow: 0;
}

.shrink {
    flex-shrink: 1;
}
.shrink-0 {
    flex-shrink: 0;
}
.justify-normal {
    justify-content: normal;
}
.justify-start {
    justify-content: flex-start;
}
.justify-end {
    justify-content: flex-end;
}
.justify-center {
    justify-content: center;
}
.justify-between {
    justify-content: space-between;
}
.justify-around {
    justify-content: space-around;
}
.justify-evenly {
    justify-content: space-evenly;
}
.justify-stretch {
    justify-content: stretch;
}
.justify-items-start {
    justify-items: start;
}
.justify-items-end {
    justify-items: end;
}
.justify-items-center {
    justify-items: center;
}
.justify-items-stretch {
    justify-items: stretch;
}
.justify-self-auto {
    justify-self: auto;
}
.justify-self-start {
    justify-self: start;
}
.justify-self-end {
    justify-self: end;
}
.justify-self-center {
    justify-self: center;
}
.justify-self-stretch {
    justify-self: stretch
}
.content-noraml {
    align-content: normal;
}
.content-center {
    align-content: center;
}
.content-start {
    align-content: start;
}
.content-end {
    align-content: end;
}
.content-between {
    align-content: space-between;
}
.content-around {
    align-content: space-around;
}
.content-evenly {
    align-content: space-evenly;
}
.content-baseline {
    align-content: baseline;
}
.content-stretch {
    align-content: stretch;
}
.items-start {
    align-items: start;
}
.items-end {
    align-items: end;
}
.items-center {
    align-items: center;
}
.items-baseline {
    align-items: baseline;
}
.items-stretch {
    align-items: stretch;
}

// Align Self 
.self-auto {
    align-self: auto;
}

.self-start {
    align-self: flex-start;
}

.self-end {
    align-self: flex-end;
}

.self-center {
    align-self: center;
}

.self-stretch {
    align-self: stretch;
}

.self-baseline {
    align-self: baseline;
}

Copy after login

Conslusion

  • Using Atomic classes with Tailwind as reference can
  • Reduce a Project's CSS footprint.
  • Maintain Styling consistensy across the Application.
  • Increases Developer speed, with rapid prototyping. ?

The above is the detailed content of Why add Tailwind CSS Atomic Classes to project ❓. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

How to Use CSS Grid for Sticky Headers and Footers How to Use CSS Grid for Sticky Headers and Footers Apr 02, 2025 pm 06:29 PM

CSS Grid is a collection of properties designed to make layout easier than it’s ever been. Like anything, there&#039;s a bit of a learning curve, but Grid is

Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

See all articles