Skip to content

CSS Organization Structure: Building Scalable Style Architecture โ€‹

In small projects, putting all CSS in one file seems fine. But as the project grows, this file becomes larger and larger, making it as difficult as finding a needle in a haystack to locate specific style rules. New team members don't know where to add new styles, and when modifying existing styles, they worry about breaking layouts elsewhere.

This is like an unorganized closetโ€”still manageable at first, but as clothes accumulate, finding a specific piece becomes extremely difficult. We need a systematic organization methodโ€”categorizing clothes by type, season, and usage frequency, which not only helps find items quickly but also keeps things neat and orderly.

CSS organization is similar. Good organizational structure makes code easier to understand, maintain, and extend.

Why is CSS Organization Important? โ€‹

The Problem with Single Files โ€‹

Let's look at typical problems encountered in single-file CSS projects:

css
/* styles.css - a huge file with thousands of lines */

/* Reset styles */
* {
  margin: 0;
  padding: 0;
}
html {
  font-size: 16px;
}

/* Navigation bar */
.nav {
  background-color: white;
}
.nav ul {
  list-style: none;
}
.nav li {
  display: inline-block;
}

/* Homepage carousel */
.carousel {
  position: relative;
}
.carousel-item {
  display: none;
}

/* ... hundreds of lines later ... */

/* Footer */
.footer {
  background-color: #333;
}

/* ... more styles ... */

/* Responsive styles */
@media (max-width: 768px) {
  .nav {
    /* ... */
  }
  /* Responsive styles scattered throughout the file */
}

/* Wait, where are the navigation styles? */
/* To modify button styles, need to search the entire file... */

This organizational approach has obvious problems:

  1. Hard to locate: Need to search through thousands of lines to find specific styles
  2. Easy to create conflicts: Accidentally redefine the same selector
  3. Difficult to reuse: Similar styles appear repeatedly in different places
  4. Collaboration difficulties: Multiple people editing the same file can cause conflicts
  5. Loading performance: Even when only part of the styles are needed, the entire file must be loaded

Benefits of Good Organization โ€‹

In contrast, well-organized CSS projects provide:

  • Quick location: Know where each style is located
  • Modularity: Independent components don't affect each other
  • Easy maintenance: Modifying one module won't break other parts
  • Team collaboration: Different team members can work on different files simultaneously
  • Performance optimization: Can load needed styles on demand
  • Code reuse: Shared styles can be easily reused

Basic File Structure โ€‹

Simple Project Structure โ€‹

For small to medium projects, you can use this structure:

styles/
โ”œโ”€โ”€ base/
โ”‚   โ”œโ”€โ”€ reset.css        # Reset browser default styles
โ”‚   โ”œโ”€โ”€ typography.css   # Font typography
โ”‚   โ””โ”€โ”€ variables.css    # CSS variable definitions
โ”œโ”€โ”€ components/
โ”‚   โ”œโ”€โ”€ button.css       # Button component
โ”‚   โ”œโ”€โ”€ card.css         # Card component
โ”‚   โ”œโ”€โ”€ form.css         # Form component
โ”‚   โ””โ”€โ”€ navigation.css   # Navigation component
โ”œโ”€โ”€ layout/
โ”‚   โ”œโ”€โ”€ header.css       # Page header layout
โ”‚   โ”œโ”€โ”€ footer.css       # Page footer layout
โ”‚   โ”œโ”€โ”€ grid.css         # Grid system
โ”‚   โ””โ”€โ”€ container.css    # Container styles
โ”œโ”€โ”€ pages/
โ”‚   โ”œโ”€โ”€ home.css         # Homepage specific styles
โ”‚   โ”œโ”€โ”€ about.css        # About page styles
โ”‚   โ””โ”€โ”€ contact.css      # Contact page styles
โ”œโ”€โ”€ utilities/
โ”‚   โ”œโ”€โ”€ helpers.css      # Helper utility classes
โ”‚   โ””โ”€โ”€ animations.css   # Animation effects
โ””โ”€โ”€ main.css             # Main file, imports all styles

Let's look at the role of each folder:

1. Base Layer โ€‹

The base layer defines the basic styles of the entire website, including resets, variables, and typography:

css
/* base/reset.css */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  line-height: 1.6;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: #333;
  background-color: #fff;
}

img {
  max-width: 100%;
  height: auto;
  display: block;
}

a {
  color: inherit;
  text-decoration: none;
}
css
/* base/variables.css */
:root {
  /* Color system */
  --color-primary: #3498db;
  --color-secondary: #2ecc71;
  --color-danger: #e74c3c;
  --color-warning: #f39c12;
  --color-text: #333;
  --color-text-light: #666;
  --color-bg: #fff;
  --color-bg-light: #f8f9fa;

  /* Spacing system */
  --spacing-xs: 4px;
  --spacing-sm: 8px;
  --spacing-md: 16px;
  --spacing-lg: 24px;
  --spacing-xl: 32px;

  /* Font system */
  --font-size-sm: 14px;
  --font-size-base: 16px;
  --font-size-lg: 18px;
  --font-size-xl: 24px;
  --font-size-2xl: 32px;

  /* Shadow system */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1);

  /* Border radius system */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 16px;
  --radius-full: 9999px;
}
css
/* base/typography.css */
h1,
h2,
h3,
h4,
h5,
h6 {
  margin-top: 0;
  margin-bottom: var(--spacing-md);
  line-height: 1.2;
  font-weight: 700;
  color: var(--color-text);
}

h1 {
  font-size: var(--font-size-2xl);
}
h2 {
  font-size: var(--font-size-xl);
}
h3 {
  font-size: var(--font-size-lg);
}
h4 {
  font-size: var(--font-size-base);
}

p {
  margin-top: 0;
  margin-bottom: var(--spacing-md);
  line-height: 1.6;
}

strong {
  font-weight: 700;
}
em {
  font-style: italic;
}

code {
  padding: 2px 4px;
  font-family: "Courier New", monospace;
  background-color: var(--color-bg-light);
  border-radius: var(--radius-sm);
  font-size: 0.9em;
}

2. Components Layer โ€‹

The components layer contains reusable UI components:

css
/* components/button.css */
.button {
  display: inline-block;
  padding: var(--spacing-sm) var(--spacing-lg);
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  font-size: var(--font-size-base);
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
  background-color: var(--color-bg-light);
  color: var(--color-text);
}

.button:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.button--primary {
  background-color: var(--color-primary);
  color: white;
}

.button--secondary {
  background-color: var(--color-secondary);
  color: white;
}

.button--large {
  padding: var(--spacing-md) var(--spacing-xl);
  font-size: var(--font-size-lg);
}

.button--small {
  padding: var(--spacing-xs) var(--spacing-md);
  font-size: var(--font-size-sm);
}

.button--block {
  display: block;
  width: 100%;
}
css
/* components/card.css */
.card {
  background-color: white;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  transition: box-shadow 0.3s;
}

.card:hover {
  box-shadow: var(--shadow-lg);
}

.card__header {
  padding: var(--spacing-lg);
  background-color: var(--color-bg-light);
  border-bottom: 1px solid #e0e0e0;
}

.card__title {
  margin: 0;
  font-size: var(--font-size-lg);
  color: var(--color-text);
}

.card__body {
  padding: var(--spacing-lg);
}

.card__footer {
  padding: var(--spacing-lg);
  background-color: var(--color-bg-light);
  border-top: 1px solid #e0e0e0;
}

3. Layout Layer โ€‹

The layout layer defines the main structure of the page:

css
/* layout/container.css */
.container {
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
  padding-left: var(--spacing-lg);
  padding-right: var(--spacing-lg);
}

.container--narrow {
  max-width: 800px;
}

.container--wide {
  max-width: 1400px;
}

.container--fluid {
  max-width: 100%;
}
css
/* layout/grid.css */
.grid {
  display: grid;
  gap: var(--spacing-lg);
}

.grid--2-cols {
  grid-template-columns: repeat(2, 1fr);
}

.grid--3-cols {
  grid-template-columns: repeat(3, 1fr);
}

.grid--4-cols {
  grid-template-columns: repeat(4, 1fr);
}

@media (max-width: 768px) {
  .grid--2-cols,
  .grid--3-cols,
  .grid--4-cols {
    grid-template-columns: 1fr;
  }
}
css
/* layout/header.css */
.header {
  background-color: white;
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header__container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--spacing-md) 0;
}

.header__logo {
  font-size: var(--font-size-xl);
  font-weight: 700;
  color: var(--color-primary);
}

.header__nav {
  display: flex;
  gap: var(--spacing-lg);
}

4. Pages Layer โ€‹

The pages layer contains specific page styles:

css
/* pages/home.css */
.hero {
  padding: var(--spacing-xl) 0;
  text-align: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.hero__title {
  font-size: var(--font-size-2xl);
  margin-bottom: var(--spacing-md);
}

.hero__subtitle {
  font-size: var(--font-size-lg);
  margin-bottom: var(--spacing-xl);
  opacity: 0.9;
}

.features {
  padding: var(--spacing-xl) 0;
}

.features__grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--spacing-xl);
}

.feature {
  text-align: center;
}

.feature__icon {
  font-size: 48px;
  margin-bottom: var(--spacing-md);
  color: var(--color-primary);
}

5. Utilities Layer โ€‹

The utilities layer provides common helper classes:

css
/* utilities/helpers.css */

/* Display control */
.hide {
  display: none !important;
}
.show {
  display: block !important;
}
.invisible {
  visibility: hidden;
}

/* Text alignment */
.text-left {
  text-align: left;
}
.text-center {
  text-align: center;
}
.text-right {
  text-align: right;
}

/* Spacing utilities */
.mt-1 {
  margin-top: var(--spacing-sm);
}
.mt-2 {
  margin-top: var(--spacing-md);
}
.mt-3 {
  margin-top: var(--spacing-lg);
}
.mb-1 {
  margin-bottom: var(--spacing-sm);
}
.mb-2 {
  margin-bottom: var(--spacing-md);
}
.mb-3 {
  margin-bottom: var(--spacing-lg);
}

/* Color utilities */
.text-primary {
  color: var(--color-primary);
}
.text-secondary {
  color: var(--color-secondary);
}
.text-muted {
  color: var(--color-text-light);
}

.bg-primary {
  background-color: var(--color-primary);
}
.bg-light {
  background-color: var(--color-bg-light);
}

Main File Assembly โ€‹

css
/* main.css - import all styles */

/* 1. Base layer - loaded first */
@import "base/reset.css";
@import "base/variables.css";
@import "base/typography.css";

/* 2. Layout layer */
@import "layout/container.css";
@import "layout/grid.css";
@import "layout/header.css";
@import "layout/footer.css";

/* 3. Components layer */
@import "components/button.css";
@import "components/card.css";
@import "components/form.css";
@import "components/navigation.css";

/* 4. Pages layer */
@import "pages/home.css";
@import "pages/about.css";

/* 5. Utilities layer - loaded last, ensuring highest priority */
@import "utilities/helpers.css";
@import "utilities/animations.css";

Large Project Structure โ€‹

For more complex projects, we need more detailed organization:

styles/
โ”œโ”€โ”€ 01-settings/
โ”‚   โ”œโ”€โ”€ _colors.scss
โ”‚   โ”œโ”€โ”€ _typography.scss
โ”‚   โ””โ”€โ”€ _breakpoints.scss
โ”œโ”€โ”€ 02-tools/
โ”‚   โ”œโ”€โ”€ _mixins.scss
โ”‚   โ””โ”€โ”€ _functions.scss
โ”œโ”€โ”€ 03-generic/
โ”‚   โ”œโ”€โ”€ _normalize.scss
โ”‚   โ””โ”€โ”€ _reset.scss
โ”œโ”€โ”€ 04-elements/
โ”‚   โ”œโ”€โ”€ _headings.scss
โ”‚   โ”œโ”€โ”€ _links.scss
โ”‚   โ””โ”€โ”€ _forms.scss
โ”œโ”€โ”€ 05-objects/
โ”‚   โ”œโ”€โ”€ _container.scss
โ”‚   โ”œโ”€โ”€ _grid.scss
โ”‚   โ””โ”€โ”€ _media.scss
โ”œโ”€โ”€ 06-components/
โ”‚   โ”œโ”€โ”€ _button.scss
โ”‚   โ”œโ”€โ”€ _card.scss
โ”‚   โ”œโ”€โ”€ _navigation.scss
โ”‚   โ””โ”€โ”€ _modal.scss
โ”œโ”€โ”€ 07-utilities/
โ”‚   โ”œโ”€โ”€ _spacing.scss
โ”‚   โ”œโ”€โ”€ _text.scss
โ”‚   โ””โ”€โ”€ _display.scss
โ””โ”€โ”€ main.scss

This structure follows ITCSS (Inverted Triangle CSS) philosophy, organizing styles from lowest to highest specificity.

ITCSS Layer Explanation โ€‹

1. Settings Layer โ€‹

Global configuration and variables:

scss
/* 01-settings/_colors.scss */
$color-primary: #3498db;
$color-secondary: #2ecc71;
$color-danger: #e74c3c;

$colors: (
  "primary": $color-primary,
  "secondary": $color-secondary,
  "danger": $color-danger,
);
scss
/* 01-settings/_breakpoints.scss */
$breakpoints: (
  "sm": 576px,
  "md": 768px,
  "lg": 992px,
  "xl": 1200px,
);

2. Tools Layer โ€‹

Mixins and Functions:

scss
/* 02-tools/_mixins.scss */
@mixin respond-to($breakpoint) {
  @media (min-width: map-get($breakpoints, $breakpoint)) {
    @content;
  }
}

@mixin flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

@mixin truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
scss
/* 02-tools/_functions.scss */
@function spacing($multiplier) {
  @return $multiplier * 8px;
}

@function color($name) {
  @return map-get($colors, $name);
}

3. Generic Layer โ€‹

Reset and normalization styles:

scss
/* 03-generic/_reset.scss */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-size: 16px;
}

body {
  margin: 0;
  font-family: system-ui, sans-serif;
}

4. Elements Layer โ€‹

Bare element styles:

scss
/* 04-elements/_headings.scss */
h1,
h2,
h3,
h4,
h5,
h6 {
  margin-top: 0;
  margin-bottom: spacing(2);
  line-height: 1.2;
  font-weight: 700;
}

h1 {
  font-size: 2rem;
}
h2 {
  font-size: 1.5rem;
}
h3 {
  font-size: 1.25rem;
}

5. Objects Layer โ€‹

Design patterns and layout objects:

scss
/* 05-objects/_container.scss */
.o-container {
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
  padding-left: spacing(2);
  padding-right: spacing(2);
}
scss
/* 05-objects/_grid.scss */
.o-grid {
  display: grid;
  gap: spacing(2);
}

.o-grid--2-cols {
  grid-template-columns: repeat(2, 1fr);
}

6. Components Layer โ€‹

Specific UI components:

scss
/* 06-components/_button.scss */
.c-button {
  display: inline-block;
  padding: spacing(1) spacing(2);
  border: none;
  border-radius: 4px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;

  &--primary {
    background-color: color("primary");
    color: white;
  }

  &--large {
    padding: spacing(2) spacing(3);
    font-size: 1.125rem;
  }
}

7. Utilities Layer โ€‹

Single-purpose utility classes:

scss
/* 07-utilities/_spacing.scss */
@each $size in (1, 2, 3, 4) {
  .u-mt-#{$size} {
    margin-top: spacing($size) !important;
  }
  .u-mb-#{$size} {
    margin-bottom: spacing($size) !important;
  }
  .u-pt-#{$size} {
    padding-top: spacing($size) !important;
  }
  .u-pb-#{$size} {
    padding-bottom: spacing($size) !important;
  }
}

Modularization Strategies โ€‹

Component Independence โ€‹

Each component should be self-contained and not depend on external styles:

css
/* โŒ Poor practice: components depend on external styles */
.button {
  /* Depends on global .container */
}

.container .button {
  padding: 10px;
}

/* โœ… Good practice: components are self-contained */
.button {
  padding: 10px 20px;
  /* All necessary styles are here */
}

File Naming Conventions โ€‹

Maintain consistent file naming:

โœ… Good naming:
- button.css
- navigation.css
- user-profile.css

โŒ Avoid:
- btn.css (too abbreviated)
- Navigation.css (capitalized)
- user_profile.css (underscores, inconsistent)

Importance of Import Order โ€‹

The order of style imports affects priority:

css
/* Correct order */
@import "base/reset.css"; /* 1. Reset */
@import "base/variables.css"; /* 2. Variables */
@import "layout/grid.css"; /* 3. Layout */
@import "components/button.css"; /* 4. Components */
@import "utilities/helpers.css"; /* 5. Utility classes (last, highest priority) */

/* โŒ Wrong order */
@import "utilities/helpers.css"; /* Utility classes are overridden by later styles */
@import "components/button.css";

Namespace Management โ€‹

Use prefixes to distinguish different types of styles:

css
/* Layout */
.l-container {
}
.l-header {
}
.l-sidebar {
}

/* Components */
.c-button {
}
.c-card {
}
.c-modal {
}

/* Utility classes */
.u-hide {
}
.u-text-center {
}
.u-mt-2 {
}

/* States */
.is-active {
}
.is-hidden {
}
.is-loading {
}

/* JavaScript hooks (no styles applied) */
.js-toggle {
}
.js-submit {
}

The benefits of this prefix system:

  1. Clear intent: Can see the purpose at a glance
  2. Avoid conflicts: Classes with different prefixes won't conflict
  3. Easy search: Easy to find specific types of classes
  4. Team collaboration: New team members quickly understand code structure

CSS Preprocessor Organization โ€‹

Sass/SCSS Project Structure โ€‹

styles/
โ”œโ”€โ”€ abstracts/
โ”‚   โ”œโ”€โ”€ _variables.scss
โ”‚   โ”œโ”€โ”€ _mixins.scss
โ”‚   โ”œโ”€โ”€ _functions.scss
โ”‚   โ””โ”€โ”€ _placeholders.scss
โ”œโ”€โ”€ base/
โ”‚   โ”œโ”€โ”€ _reset.scss
โ”‚   โ”œโ”€โ”€ _typography.scss
โ”‚   โ””โ”€โ”€ _utilities.scss
โ”œโ”€โ”€ layout/
โ”‚   โ”œโ”€โ”€ _header.scss
โ”‚   โ”œโ”€โ”€ _footer.scss
โ”‚   โ”œโ”€โ”€ _navigation.scss
โ”‚   โ””โ”€โ”€ _grid.scss
โ”œโ”€โ”€ components/
โ”‚   โ”œโ”€โ”€ _buttons.scss
โ”‚   โ”œโ”€โ”€ _cards.scss
โ”‚   โ”œโ”€โ”€ _forms.scss
โ”‚   โ””โ”€โ”€ _modals.scss
โ”œโ”€โ”€ pages/
โ”‚   โ”œโ”€โ”€ _home.scss
โ”‚   โ”œโ”€โ”€ _about.scss
โ”‚   โ””โ”€โ”€ _contact.scss
โ”œโ”€โ”€ themes/
โ”‚   โ”œโ”€โ”€ _default.scss
โ”‚   โ””โ”€โ”€ _dark.scss
โ””โ”€โ”€ main.scss

Using Partial Files โ€‹

scss
/* _variables.scss - files starting with underscore are partials */
$color-primary: #3498db;
$spacing-unit: 8px;

/* main.scss - main file imports all partial files */
@import "abstracts/variables";
@import "abstracts/mixins";
@import "base/reset";
@import "components/button";
/* Note: omit underscore and extension when importing */

Modular Mixins โ€‹

scss
/* abstracts/_mixins.scss */
@mixin button-base {
  display: inline-block;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

@mixin button-variant($bg-color, $text-color) {
  @include button-base;
  background-color: $bg-color;
  color: $text-color;

  &:hover {
    background-color: darken($bg-color, 10%);
  }
}

/* components/_buttons.scss */
.button--primary {
  @include button-variant($color-primary, white);
}

.button--secondary {
  @include button-variant($color-secondary, white);
}

Best Practices for Organization โ€‹

1. Single Responsibility Principle โ€‹

Each file should be responsible for one thing:

css
/* โœ… Good: button.css only contains button styles */
.button {
  /* ... */
}
.button--primary {
  /* ... */
}
.button--large {
  /* ... */
}

/* โŒ Poor: components.css contains all components */
.button {
  /* ... */
}
.card {
  /* ... */
}
.modal {
  /* ... */
}
/* This file is too big! */

2. Organize by Function, Not Page โ€‹

โœ… Good organization:
components/
โ”œโ”€โ”€ button.css
โ”œโ”€โ”€ card.css
โ””โ”€โ”€ navigation.css

โŒ Avoid:
pages/
โ”œโ”€โ”€ home-buttons.css
โ”œโ”€โ”€ home-cards.css
โ”œโ”€โ”€ about-buttons.css
โ””โ”€โ”€ about-cards.css

3. Keep File Sizes Reasonable โ€‹

โœ… Ideal file sizes:
- 50-200 lines per file
- Consider splitting files over 300 lines

โŒ Avoid:
- Single files over 500 lines
- Files that are too small (under 20 lines) are also not good

4. Use Comments to Separate โ€‹

css
/* ==========================================================================
   Button Component
   ========================================================================== */

/**
 * Base button styles
 * Used for all button variants
 */
.button {
  /* ... */
}

/* Button Variants
   ========================================================================== */

.button--primary {
  /* ... */
}

/* Button Sizes
   ========================================================================== */

.button--large {
  /* ... */
}

5. Create Index Files โ€‹

css
/* components/_index.css */
/**
 * Components Index
 *
 * This file imports all components
 * Arranged alphabetically for easy lookup
 */

@import "button";
@import "card";
@import "form";
@import "modal";
@import "navigation";
@import "table";

Real Project Examples โ€‹

Let's look at a complete CSS organization for an e-commerce website:

ecommerce-website/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ styles/
โ”‚       โ”œโ”€โ”€ settings/
โ”‚       โ”‚   โ”œโ”€โ”€ _colors.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _typography.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _spacing.scss
โ”‚       โ”‚   โ””โ”€โ”€ _breakpoints.scss
โ”‚       โ”œโ”€โ”€ tools/
โ”‚       โ”‚   โ”œโ”€โ”€ _mixins.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _functions.scss
โ”‚       โ”‚   โ””โ”€โ”€ _placeholders.scss
โ”‚       โ”œโ”€โ”€ generic/
โ”‚       โ”‚   โ”œโ”€โ”€ _normalize.scss
โ”‚       โ”‚   โ””โ”€โ”€ _reset.scss
โ”‚       โ”œโ”€โ”€ elements/
โ”‚       โ”‚   โ”œโ”€โ”€ _page.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _headings.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _links.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _lists.scss
โ”‚       โ”‚   โ””โ”€โ”€ _images.scss
โ”‚       โ”œโ”€โ”€ objects/
โ”‚       โ”‚   โ”œโ”€โ”€ _container.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _grid.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _media.scss
โ”‚       โ”‚   โ””โ”€โ”€ _stack.scss
โ”‚       โ”œโ”€โ”€ components/
โ”‚       โ”‚   โ”œโ”€โ”€ _header.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _footer.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _navigation.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _button.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _card.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _product-card.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _cart-item.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _checkout-form.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _search-bar.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _filter.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _breadcrumb.scss
โ”‚       โ”‚   โ””โ”€โ”€ _pagination.scss
โ”‚       โ”œโ”€โ”€ pages/
โ”‚       โ”‚   โ”œโ”€โ”€ _home.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _product-list.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _product-detail.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _cart.scss
โ”‚       โ”‚   โ””โ”€โ”€ _checkout.scss
โ”‚       โ”œโ”€โ”€ utilities/
โ”‚       โ”‚   โ”œโ”€โ”€ _spacing.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _text.scss
โ”‚       โ”‚   โ”œโ”€โ”€ _display.scss
โ”‚       โ”‚   โ””โ”€โ”€ _colors.scss
โ”‚       โ””โ”€โ”€ main.scss
โ””โ”€โ”€ package.json

Common Problems and Solutions โ€‹

Problem 1: Too Many Files, Don't Know Where Styles Are โ€‹

Solution: Establish naming conventions and documentation

project-root/
โ”œโ”€โ”€ STYLE_GUIDE.md    # Style guide documentation
โ”œโ”€โ”€ styles/
โ”‚   โ””โ”€โ”€ README.md     # Style structure explanation

In README, explain:

markdown
# Styles Structure

## Directory Explanations

- `settings/` - Global variables and configuration
- `components/` - UI component styles
- `layout/` - Page layout styles

## Adding New Styles

1. Component styles go in the `components/` directory
2. File names use lowercase and hyphens
3. Use BEM naming conventions

Problem 2: @import Causes Multiple HTTP Requests โ€‹

Solution: Use build tools

javascript
// webpack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: ["style-loader", "css-loader", "sass-loader"],
      },
    ],
  },
};

Build tools merge all @imports into a single file.

Problem 3: Style Override Issues โ€‹

Solution: Strictly control import order

scss
/* main.scss - strictly follow specificity from low to high */

// 1. Settings - lowest specificity
@import "settings/variables";

// 2. Generic
@import "generic/reset";

// 3. Elements
@import "elements/headings";

// 4. Objects
@import "objects/container";

// 5. Components
@import "components/button";

// 6. Utilities - highest specificity, should override other styles
@import "utilities/spacing";

Problem 4: Team Members Don't Follow Organization Rules โ€‹

Solution: Use Linters and automation tools

json
// .stylelintrc.json
{
  "rules": {
    "selector-class-pattern": "^[a-z]([a-z0-9-]+)?(__([a-z0-9]+-?)+)?(--([a-z0-9]+-?)+){0,2}$",
    "max-nesting-depth": 3,
    "selector-max-compound-selectors": 3
  }
}

Combine with Git Hooks to automatically check before commits.

Summary โ€‹

Good CSS organization structure is the foundation of maintainable projects.

Core principles:

  • Modularity: Each file is responsible for a clear function
  • Clear layering: Base โ†’ Layout โ†’ Components โ†’ Utilities
  • Naming conventions: Unified naming approach
  • Complete documentation: Help team members get started quickly

Organization methods:

  • Small projects: Simple folder structure
  • Large projects: ITCSS or similar layered architecture
  • Use preprocessors: Take full advantage of partials and imports

Best practices:

  • Single responsibility principle
  • Reasonable file sizes
  • Clear comments
  • Strict import order
  • Use build tools

Good organizational structure isn't staticโ€”it should be adjusted based on the project's actual needs. The most important thing is for the team to reach consensus and consistently execute it.