SASS Architecture: The 7-1 Pattern
SASS Architecture: The 7-1 Pattern
As projects grow in complexity, organizing your SASS files becomes critical for maintainability and scalability. The 7-1 pattern is one of the most popular architectural approaches for structuring large-scale SASS projects. In this comprehensive lesson, we'll explore how to implement this powerful pattern effectively.
Why Architecture Matters in Large Projects
Without a clear architectural strategy, SASS projects quickly become unmanageable:
- File Chaos: Hundreds of styles mixed together in a few giant files
- Naming Conflicts: Duplicate class names causing unexpected style overrides
- Debugging Nightmares: Unable to find where specific styles are defined
- Team Confusion: Developers unsure where to add new styles
- Performance Issues: Loading unnecessary CSS for every page
- Scalability Problems: Adding new features breaks existing styles
Benefits of the 7-1 Pattern
- Clear Organization: Every file has a specific purpose and location
- Easy Navigation: Find any style rule quickly
- Team Friendly: Multiple developers can work simultaneously without conflicts
- Maintainable: Changes are isolated and predictable
- Scalable: Easy to add new features without refactoring
- Best Practices: Industry-standard approach used by major projects
The 7-1 Pattern Structure
The name "7-1" comes from having 7 folders for partial files and 1 main file that imports everything:
Complete 7-1 Folder Structure
scss/
├── abstracts/
│ ├── _variables.scss
│ ├── _functions.scss
│ ├── _mixins.scss
│ └── _placeholders.scss
├── base/
│ ├── _reset.scss
│ ├── _typography.scss
│ ├── _animations.scss
│ └── _utilities.scss
├── components/
│ ├── _buttons.scss
│ ├── _cards.scss
│ ├── _forms.scss
│ ├── _modals.scss
│ ├── _navigation.scss
│ ├── _dropdown.scss
│ ├── _tabs.scss
│ └── _carousel.scss
├── layout/
│ ├── _header.scss
│ ├── _footer.scss
│ ├── _sidebar.scss
│ ├── _grid.scss
│ └── _navigation.scss
├── pages/
│ ├── _home.scss
│ ├── _about.scss
│ ├── _contact.scss
│ └── _product.scss
├── themes/
│ ├── _light.scss
│ ├── _dark.scss
│ └── _admin.scss
├── vendors/
│ ├── _bootstrap.scss
│ ├── _normalize.scss
│ └── _jquery-ui.scss
└── main.scss ← The single main file
main.scss start with an underscore (_). This tells SASS these are "partials" that should not be compiled into separate CSS files.
Folder 1: abstracts/
The abstracts/ folder (sometimes called helpers/ or utilities/) contains SASS tools and helpers that don't output any CSS by themselves.
_variables.scss
Store all project variables in one place:
abstracts/_variables.scss
// Color Palette
$color-primary: #3498db;
$color-secondary: #2ecc71;
$color-accent: #e74c3c;
$color-text: #333;
$color-background: #fff;
// Typography
$font-family-base: "Helvetica Neue", Arial, sans-serif;
$font-family-heading: "Georgia", serif;
$font-size-base: 16px;
$line-height-base: 1.6;
// Spacing
$spacing-unit: 8px;
$spacing-xs: $spacing-unit;
$spacing-sm: $spacing-unit * 2;
$spacing-md: $spacing-unit * 3;
$spacing-lg: $spacing-unit * 4;
$spacing-xl: $spacing-unit * 6;
// Breakpoints
$breakpoint-mobile: 480px;
$breakpoint-tablet: 768px;
$breakpoint-desktop: 1024px;
$breakpoint-wide: 1280px;
// Z-index Scale
$z-index-dropdown: 100;
$z-index-modal: 200;
$z-index-popover: 300;
$z-index-tooltip: 400;
// Transitions
$transition-speed: 0.3s;
$transition-easing: ease-in-out;
_mixins.scss
Reusable chunks of styles that can accept parameters:
abstracts/_mixins.scss
// Responsive breakpoint mixin
@mixin respond-to($breakpoint) {
@if $breakpoint == mobile {
@media (max-width: $breakpoint-mobile) { @content; }
}
@else if $breakpoint == tablet {
@media (min-width: $breakpoint-tablet) { @content; }
}
@else if $breakpoint == desktop {
@media (min-width: $breakpoint-desktop) { @content; }
}
}
// Flexbox center mixin
@mixin flex-center {
display: flex;
justify-content: center;
align-items: center;
}
// Button variant mixin
@mixin button-variant($bg-color, $text-color) {
background-color: $bg-color;
color: $text-color;
border: 1px solid darken($bg-color, 10%);
&:hover {
background-color: darken($bg-color, 8%);
}
&:active {
background-color: darken($bg-color, 12%);
}
}
// Card shadow mixin
@mixin card-shadow($level: 1) {
@if $level == 1 {
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
} @else if $level == 2 {
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
} @else if $level == 3 {
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
}
// Clearfix mixin
@mixin clearfix {
&::after {
content: "";
display: table;
clear: both;
}
}
_functions.scss
Custom SASS functions for calculations and transformations:
abstracts/_functions.scss
// Convert pixels to rem
@function px-to-rem($px, $base: $font-size-base) {
@return ($px / $base) * 1rem;
}
// Calculate spacing based on unit
@function spacing($multiplier) {
@return $spacing-unit * $multiplier;
}
// Get color from palette with tint/shade
@function color-level($color, $level) {
@if $level > 0 {
@return lighten($color, $level * 10%);
} @else if $level < 0 {
@return darken($color, abs($level) * 10%);
} @else {
@return $color;
}
}
// Calculate optimal text color (light or dark) based on background
@function text-contrast($background-color) {
$lightness: lightness($background-color);
@if $lightness > 50% {
@return #000;
} @else {
@return #fff;
}
}
_placeholders.scss
Reusable style patterns using @extend:
abstracts/_placeholders.scss
// Visually hidden but accessible to screen readers
%visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
}
// Reset list styles
%list-reset {
margin: 0;
padding: 0;
list-style: none;
}
// Common button styles
%button-base {
display: inline-block;
padding: 0.5rem 1rem;
border: none;
border-radius: 4px;
cursor: pointer;
transition: all $transition-speed $transition-easing;
text-decoration: none;
text-align: center;
}
// Card base styles
%card-base {
background: $color-background;
border-radius: 8px;
padding: 1.5rem;
@include card-shadow(1);
}
Folder 2: base/
The base/ folder contains boilerplate and foundational styles that apply throughout the site.
_reset.scss
base/_reset.scss
// Modern CSS reset
*, *::before, *::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-size: 100%;
-webkit-text-size-adjust: 100%;
}
body {
line-height: $line-height-base;
font-family: $font-family-base;
color: $color-text;
background-color: $color-background;
}
img, picture, video, canvas, svg {
display: block;
max-width: 100%;
height: auto;
}
a {
color: inherit;
text-decoration: none;
}
button {
font: inherit;
}
_typography.scss
base/_typography.scss
// Headings
h1, h2, h3, h4, h5, h6 {
font-family: $font-family-heading;
font-weight: 700;
line-height: 1.2;
margin-bottom: spacing(2);
}
h1 { font-size: px-to-rem(40px); }
h2 { font-size: px-to-rem(32px); }
h3 { font-size: px-to-rem(28px); }
h4 { font-size: px-to-rem(24px); }
h5 { font-size: px-to-rem(20px); }
h6 { font-size: px-to-rem(18px); }
// Paragraphs
p {
margin-bottom: spacing(2);
&:last-child {
margin-bottom: 0;
}
}
// Links
a {
color: $color-primary;
transition: color $transition-speed;
&:hover {
color: darken($color-primary, 15%);
}
}
// Lists
ul, ol {
margin-bottom: spacing(2);
padding-left: spacing(3);
}
// Emphasis
strong, b {
font-weight: 700;
}
em, i {
font-style: italic;
}
Folder 3: components/
The components/ folder contains styles for individual, reusable UI components.
components/_buttons.scss
.btn {
@extend %button-base;
&--primary {
@include button-variant($color-primary, #fff);
}
&--secondary {
@include button-variant($color-secondary, #fff);
}
&--outline {
background: transparent;
color: $color-primary;
border: 2px solid $color-primary;
&:hover {
background: $color-primary;
color: #fff;
}
}
&--large {
padding: 0.75rem 1.5rem;
font-size: px-to-rem(18px);
}
&--small {
padding: 0.25rem 0.5rem;
font-size: px-to-rem(14px);
}
}
components/_cards.scss
.card {
@extend %card-base;
&__header {
padding-bottom: spacing(2);
border-bottom: 1px solid #eee;
margin-bottom: spacing(2);
}
&__title {
font-size: px-to-rem(20px);
margin-bottom: spacing(1);
}
&__body {
margin-bottom: spacing(2);
}
&__footer {
padding-top: spacing(2);
border-top: 1px solid #eee;
}
&--featured {
border: 2px solid $color-primary;
@include card-shadow(2);
}
&--interactive {
transition: transform $transition-speed, box-shadow $transition-speed;
&:hover {
transform: translateY(-4px);
@include card-shadow(3);
}
}
}
Folder 4: layout/
The layout/ folder contains styles for major structural components of the page.
layout/_header.scss
.header {
background: $color-primary;
color: #fff;
padding: spacing(2) 0;
position: sticky;
top: 0;
z-index: $z-index-dropdown;
&__container {
max-width: $breakpoint-wide;
margin: 0 auto;
padding: 0 spacing(2);
@include flex-center;
justify-content: space-between;
}
&__logo {
font-size: px-to-rem(24px);
font-weight: bold;
}
&__nav {
@extend %list-reset;
display: flex;
gap: spacing(3);
}
}
layout/_grid.scss
.container {
max-width: $breakpoint-wide;
margin: 0 auto;
padding: 0 spacing(2);
&--fluid {
max-width: none;
}
}
.row {
display: flex;
flex-wrap: wrap;
margin: 0 spacing(-1);
}
.col {
flex: 1;
padding: 0 spacing(1);
@for $i from 1 through 12 {
&--#{$i} {
flex: 0 0 percentage($i / 12);
max-width: percentage($i / 12);
}
}
}
Folder 5: pages/
The pages/ folder contains page-specific styles.
pages/_home.scss
.home {
&__hero {
background: linear-gradient(135deg, $color-primary, $color-secondary);
color: #fff;
padding: spacing(8) 0;
text-align: center;
h1 {
font-size: px-to-rem(48px);
margin-bottom: spacing(3);
}
}
&__features {
padding: spacing(6) 0;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: spacing(4);
}
&__cta {
background: $color-accent;
color: #fff;
padding: spacing(6) 0;
text-align: center;
}
}
Folder 6: themes/
The themes/ folder contains different theme variations.
themes/_dark.scss
.theme-dark {
--color-text: #fff;
--color-background: #1a1a1a;
--color-surface: #2a2a2a;
background: var(--color-background);
color: var(--color-text);
.card {
background: var(--color-surface);
color: var(--color-text);
}
a {
color: lighten($color-primary, 20%);
}
}
Folder 7: vendors/
The vendors/ folder contains third-party CSS and overrides.
vendors/_normalize.scss
// Import normalize.css or other vendor styles
@import "normalize.css/normalize";
// Override vendor styles if needed
.third-party-widget {
// Custom overrides
}
The Main File: main.scss
This is the single file that imports everything in the correct order:
main.scss - The Manifest File
// Configuration and helpers
@use "abstracts/variables" as *;
@use "abstracts/functions" as *;
@use "abstracts/mixins" as *;
@use "abstracts/placeholders" as *;
// Vendors (third-party styles)
@use "vendors/normalize";
// Base styles
@use "base/reset";
@use "base/typography";
@use "base/animations";
// Layout-related sections
@use "layout/header";
@use "layout/footer";
@use "layout/sidebar";
@use "layout/grid";
// Components
@use "components/buttons";
@use "components/cards";
@use "components/forms";
@use "components/modals";
@use "components/navigation";
// Page-specific styles
@use "pages/home";
@use "pages/about";
@use "pages/contact";
// Themes
@use "themes/light";
@use "themes/dark";
When to Use 7-1 vs Simpler Structures
Use 7-1 When:
- Your project has 50+ components
- Multiple developers work on the codebase
- The project will be maintained long-term
- You need multiple themes or page-specific styles
- The application is large and complex
Use Simpler Structure When:
- Small projects (landing pages, simple sites)
- Solo developer projects
- Prototypes or MVPs
- Projects with fewer than 20 components
Simpler Structure for Small Projects
scss/
├── _variables.scss
├── _mixins.scss
├── _base.scss
├── _components.scss
├── _layout.scss
└── main.scss
Exercise 1: Set Up 7-1 Architecture
Create a complete 7-1 folder structure for a fictional e-commerce site:
- Create all 7 folders with appropriate files
- Set up
main.scsswith proper imports - Create variables for colors, spacing, and breakpoints
- Write at least 3 mixins (responsive, flex-center, button-variant)
- Create components for: buttons, product cards, and navigation
- Create page-specific styles for home and product pages
Exercise 2: Refactor Existing CSS to 7-1
Take an existing CSS file and refactor it using 7-1 architecture:
- Identify which styles belong in which folder
- Extract reusable variables from hard-coded values
- Convert repeated patterns into mixins
- Separate base styles, components, and layout
- Create the proper folder structure and move styles
- Test that everything compiles correctly
Exercise 3: Build a Theme System
Implement a multi-theme system using 7-1 architecture:
- Create light and dark theme files in
themes/ - Use CSS custom properties for theme colors
- Create a theme-switching mixin or function
- Ensure all components respect theme variables
- Test switching between themes
Summary
In this lesson, you've mastered the 7-1 pattern for SASS architecture. You now understand:
- Why architecture matters in large projects
- The structure and purpose of each of the 7 folders
- How to organize abstracts (variables, mixins, functions, placeholders)
- The role of base styles and typography
- How to structure components and layout files
- When to use page-specific styles
- How to implement themes and manage vendors
- The importance of the main.scss manifest file
- When to use 7-1 vs simpler structures
The 7-1 pattern provides a robust, scalable foundation for large SASS projects. It brings order to complexity and makes your codebase maintainable for years to come.