CSS to SASS Guide (refactor for OCS)
How to properly convert CSS files to SASS in this website
What is SASS and Why Use It? (refactor for opencoding)
SASS (Syntactically Awesome Style Sheets) is a CSS preprocessor that makes your styles:
- Easier to maintain - Use variables for colors, sizes, fonts
- More organized - Nest selectors logically
- Reusable - Create mixins and functions
- Consistent - One place to change site-wide colors
CSS vs SASS - Quick Comparison
| Feature | CSS | SASS |
|---|---|---|
| Variables | Limited (--var) |
Full support ($var) |
| Nesting | No | Yes |
| Math | Limited | Full calculations |
| Imports | Multiple HTTP requests | Compiled into one file |
| Mixins | No | Reusable code blocks |
How SASS Works in This Website
The Import Chain (How Files Connect)
_sass/minima/custom-styles.scss β ENTRY POINT (Jekyll starts here)
β
imports opencs-color-map.scss β Maps variables to CSS custom properties
β
imports root-color-map.scss β ALL COLOR VARIABLES DEFINED HERE
β
imports open-coding/_main.scss β MAIN HUB (imports all components)
β
imports all component .scss files β Your styles end up here
What This Means For You
When you create a new .scss file, you need to:
- Put it in the right folder (
_sass/open-coding/) - Import it in
_main.scssso Jekyll knows about it - Jekyll will automatically compile everything into one CSS file
Complete Step-by-Step Tutorial
Letβs convert a real CSS file to SASS. Weβll use leaderboard.css as our example.
Step 1: Find Your CSS File
First, locate the CSS file you want to convert.
Common locations for CSS files:
assets/css/your-file.css- Inline
<style>tags in HTML/Markdown files
For this example: assets/css/leaderboard.css
Step 2: Create the New SCSS File
Create a new file in the SASS folder:
File Location: _sass/open-coding/leaderboard.scss
Start with this template:
// ============================================
// LEADERBOARD STYLES
// Converted from assets/css/leaderboard.css
// ============================================
// Import to get access to color variables
@import "materials/main.scss";
// Your styles go below...
Why
@import "materials/main.scss"?
This gives you access to all the color variables like$accent,$text,$bg-1, etc.
Step 3: Copy Your CSS Into the SCSS File
Take your original CSS and paste it below the import:
@import "materials/main.scss";
// Paste your CSS here - it will work as-is!
.leaderboard-widget {
position: fixed;
bottom: 20px;
right: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 16px;
}
Good news: Regular CSS is valid SCSS! Your file will work immediately.
Step 4: Register Your File in _main.scss
Open _sass/open-coding/_main.scss and add your import:
@import 'root-color-map.scss';
@import "bathroom";
@import "blogs";
@import "calendar";
// ... other imports ...
@import "leaderboard"; // β ADD THIS LINE (no .scss extension needed!)
Important: The order of imports can matter! Add new imports at the end to avoid conflicts.
Step 5: Delete the Old CSS File
Now that SASS handles your styles, delete the original:
rm assets/css/leaderboard.css
Step 6: Remove Old CSS References
Search your project for any <link> tags loading the old CSS:
<!-- DELETE THIS LINE if you find it -->
<link rel="stylesheet" href="/assets/css/leaderboard.css">
SASS automatically includes your styles - no manual linking needed!
Step 7: Build and Test
Run the build to compile your SASS:
make
# or
bundle exec jekyll serve
Check for errors in the terminal. If successful, your styles are now SASS!
Improving Your SASS (Optional But Recommended)
Now that it works, letβs make it better using SASS features.
Improvement 1: Replace Hardcoded Colors
Find hex codes and replace with variables:
Before:
.button {
background: #4CAFEF;
color: #F0F0F0;
border: 1px solid #E53E3E;
}
After:
.button {
background: $accent; // #4CAFEF
color: $text; // #F0F0F0
border: 1px solid $red; // #E53E3E
}
Improvement 2: Use Nesting
Group related selectors together:
Before (CSS):
.card { background: #1F1F1F; }
.card .header { font-size: 20px; }
.card .header h3 { margin: 0; }
.card .header h3:hover { color: blue; }
.card .body { padding: 16px; }
After (SASS):
.card {
background: $bg-2;
.header {
font-size: 20px;
h3 {
margin: 0;
&:hover {
color: $blue;
}
}
}
.body {
padding: 16px;
}
}
The
&symbol refers to the parent selector.&:hoverbecomes.card .header h3:hover
Improvement 3: Create Local Variables
For values used multiple times in your file:
Before:
.box1 { border-radius: 16px; padding: 20px; }
.box2 { border-radius: 16px; padding: 20px; }
.box3 { border-radius: 16px; padding: 20px; }
After:
// Local variables (at top of file)
$card-radius: 16px;
$card-padding: 20px;
.box1 { border-radius: $card-radius; padding: $card-padding; }
.box2 { border-radius: $card-radius; padding: $card-padding; }
.box3 { border-radius: $card-radius; padding: $card-padding; }
Improvement 4: Use SASS Math
SASS can do calculations:
$base-spacing: 8px;
.small { padding: $base-spacing; } // 8px
.medium { padding: $base-spacing * 2; } // 16px
.large { padding: $base-spacing * 3; } // 24px
Available Color Variables Reference
These are defined in _sass/root-color-map.scss - use them instead of hex codes!
Background Colors
| Variable | Value | Use For |
|βββ-|ββ-|βββ|
| $bg-0 | #000 | Darkest background |
| $bg-1 | #1F2020 | Dark panels |
| $bg-2 | #1F1F1F | Cards |
| $bg-3 | #2A2D2D | Lighter panels |
| $panel | #2B2B2B | Panel backgrounds |
| $background | varies | Page background |
Text Colors
| Variable | Value | Use For |
|βββ-|ββ-|βββ|
| $text | #F0F0F0 | Main text |
| $text-muted | #aaa | Secondary/dimmed text |
Accent Colors
| Variable | Value | Use For |
|βββ-|ββ-|βββ|
| $accent | #4CAFEF | Primary accent/links |
| $blue | #3182CE | Info elements |
| $green | #4ADE80 | Success states |
| $red | #E53E3E | Error states |
| $orange | #ED8936 | Warning states |
File Structure Overview
_sass/
βββ root-color-map.scss β All color variables defined here
βββ opencs-color-map.scss β Maps SASS vars β CSS custom properties
βββ user-colors.scss β User-editable palette
β
βββ minima/
β βββ custom-styles.scss β ENTRY POINT (Jekyll starts here)
β
βββ open-coding/
βββ _main.scss β MAIN HUB (imports all components)
βββ bathroom.scss
βββ blogs.scss
βββ calendar.scss
βββ your-new-file.scss β YOUR NEW SCSS FILES GO HERE
β
βββ materials/ β Shared utilities & mixins
β βββ _main.scss
β βββ colors.scss
β βββ ...
β
βββ elements/ β UI element styles
βββ buttons/
βββ forms/
βββ ...
Quick Reference Checklist
Use this checklist every time you convert a file:
Before Starting
- Identify the CSS file to convert
- Note where itβs currently loaded (which pages use it)
Creating the SCSS File
- Create new file:
_sass/open-coding/your-file.scss - Add
@import "materials/main.scss";at the top - Copy CSS content into the file
Registering the File
- Open
_sass/open-coding/_main.scss - Add
@import "your-file";(no .scss extension)
Cleanup
- Delete old CSS file from
assets/css/ - Remove any
<link>tags loading the old CSS - Search for inline styles that could be moved
Testing
- Run
makeorbundle exec jekyll serve - Check terminal for SASS errors
- View the page in browser to verify styles work
Optional Improvements
- Replace hex colors with
$variables - Add nesting for cleaner code
- Create local variables for repeated values
Common Errors and Fixes
Error: βFile to import not foundβ
Error: File to import not found or unreadable: materials/main.scss
Fix: Check your import path. From _sass/open-coding/, use:
@import "materials/main.scss"; // Correct
@import "../materials/main.scss"; // Wrong
Error: βUndefined variableβ
Error: Undefined variable: "$accent"
Fix: Make sure you have the import at the top of your file:
@import "materials/main.scss"; // This gives you access to variables
Error: βInvalid CSSβ
Error: Invalid CSS after "..."
Fix: SASS is stricter than browsers. Check for:
- Missing semicolons
- Unclosed brackets
- Invalid property values
Styles Not Showing Up
Possible causes:
- Forgot to add import in
_main.scss - Old CSS file still being loaded (check for
<link>tags) - Browser cache - try hard refresh (
Cmd+Shift+R)
Full Example: Complete Conversion
Letβs see a complete before/after conversion:
Original CSS File
assets/css/leaderboard.css
.leaderboard-widget {
position: fixed;
bottom: 20px;
right: 20px;
width: 400px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 16px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
.leaderboard-header {
display: flex;
justify-content: space-between;
padding: 16px 20px;
background: rgba(255, 255, 255, 0.1);
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}
.leaderboard-header h3 {
margin: 0;
color: white;
font-size: 20px;
}
.leaderboard-content {
padding: 16px;
}
.leaderboard-item {
display: flex;
align-items: center;
padding: 12px;
border-radius: 8px;
}
.leaderboard-item:hover {
background: rgba(255, 255, 255, 0.1);
}
Converted SASS File
_sass/open-coding/leaderboard.scss
// ============================================
// LEADERBOARD WIDGET STYLES
// Converted from assets/css/leaderboard.css
// ============================================
@import "materials/main.scss";
// Local variables
$leaderboard-width: 400px;
$leaderboard-radius: 16px;
$leaderboard-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
$item-radius: 8px;
$base-padding: 16px;
.leaderboard-widget {
position: fixed;
bottom: 20px;
right: 20px;
width: $leaderboard-width;
background: $leaderboard-gradient;
border-radius: $leaderboard-radius;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
.leaderboard-header {
display: flex;
justify-content: space-between;
padding: $base-padding 20px;
background: rgba(255, 255, 255, 0.1);
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
h3 {
margin: 0;
color: $text;
font-size: 20px;
}
}
.leaderboard-content {
padding: $base-padding;
}
.leaderboard-item {
display: flex;
align-items: center;
padding: 12px;
border-radius: $item-radius;
&:hover {
background: rgba(255, 255, 255, 0.1);
}
}
Donβt Forget to Register It!
_sass/open-coding/_main.scss
// ... existing imports ...
@import "leaderboard"; // Add this line!
Summary
Converting CSS to SASS is a 7-step process:
- Find your CSS file
- Create new
.scssfile in_sass/open-coding/ - Add the import for variables at the top
- Copy your CSS (it works as-is!)
- Register in
_main.scss - Delete old CSS file and references
- Build and test
Then optionally improve with:
- Variable substitution
- Nesting
- Local variables
- SASS math