Convert Sass and Less to Native Nested CSS
Now that you understand the history of CSS preprocessors and the features they introduced, it is time to get hands-on experience with the syntax. In this assignment, you will convert Sass and Less code to modern native CSS, seeing firsthand how CSS has evolved to incorporate features that once required build tools.
This assignment reinforces your understanding of what preprocessors provided and demonstrates that modern CSS can handle most of the same use cases without any build process. You will also encounter one feature that does not have a direct native CSS equivalent, giving you an opportunity to think critically about workarounds and design solutions.
Assignment Overview
You will work with two starter files: one written in Sass (SCSS syntax) and one written in Less. Your task is to convert both files to modern native CSS, preserving the same functionality and structure while using only native CSS features like custom properties, nesting, and modern functions.
After converting the code, you will add a multiline comment to each file reflecting on the conversion process, noting what was straightforward, what required creative solutions, and any limitations you discovered in native CSS.
Starter Files
Download the two starter files below. These contain Sass and Less code that you will convert to modern native CSS.
Review both files carefully to understand what each preprocessor feature does before beginning your conversion. The files contain variables, nesting, color functions, calculations, and a mixin pattern that you will need to convert to native CSS equivalents.
Compile and Compare
Before starting your conversion, compile the Sass and Less files to see what CSS output they generate. This will help you understand what your native CSS should accomplish and give you a reference for comparison.
Compile the Sass File
Use npx to compile the Sass file without installing anything globally:
npx sass styles.scss compiled-sass.css
This command will create a compiled-sass.css file showing how Sass transforms the code. Open this
file to see the expanded CSS output.
Compile the Less File
Similarly, compile the Less file using npx:
npx lessc styles.less compiled-less.css
This creates compiled-less.css with the Less-generated output. Review this file to understand how
Less processes the code.
If you encounter problems installing or running Node.js, npm, or npx, you can use online playgrounds as an alternative to compile the preprocessor code:
- Sass Playground: Copy and paste your Sass code into https://sass-lang.com/playground/ to see the compiled CSS output. This will help you understand how Sass processes your code.
- Less Preview: Copy and paste your Less code into https://lesscss.org/less-preview/ to see the compiled CSS output. This will help you understand how Less processes your code.
While you won't be able to save the compiled files directly, you can copy the compiled CSS from the playgrounds and save it locally for comparison. This approach gives you the same insight into how preprocessors work without requiring any local tooling setup.
Compare Outputs
Open both compiled CSS files and compare them to each other and to the original preprocessor code. Notice how the preprocessors handle variables, expand nesting, apply mixins, and calculate color values. This compiled output is your target: your native CSS should produce similar results using modern CSS features.
The compiled CSS files show you exactly what the preprocessors generate. Pay attention to how mixins get
expanded, how color functions calculate new values, and how nesting creates descendant selectors. Your
native CSS conversion should achieve the same visual results while using modern CSS features like custom
properties and color-mix().
Assignment Instructions
1. Set Up Your Workspace
Create a new directory for this assignment. Copy the Sass starter code into a file named
styles.scss and the Less starter code into a file named styles.less. You will create a
single file named converted-styles.css for both of your converted code sections.
2. Convert the Sass File to Native CSS
Open styles.scss and begin converting all Sass features to their native CSS equivalents. Add your
converted code to converted-styles.css, clearly labeling this section (e.g., with a comment like
/* ===== CONVERSION FROM SASS ===== */). Focus on converting these features:
-
Variables: Convert Sass variables (
$variable-name) to CSS custom properties (--variable-name) defined in:root - Nesting: Keep the nesting structure using native CSS nesting syntax
-
Calculations: Ensure all math operations use
calc()where needed -
Color Functions: Convert
lighten()anddarken()tocolor-mix()with appropriate percentages -
Mixins: Handle the
@mixin button-basethoughtfully (this does not have a direct native CSS equivalent)
Native CSS does not have mixins. You will need to find a practical solution for the
@mixin button-base pattern. Consider how you might restructure the CSS to achieve the same
result without duplicating code excessively. This is an opportunity to think creatively about CSS
architecture.
3. Convert the Less File to Native CSS
Open styles.less and convert all Less features to native CSS. Add your converted code to the same
converted-styles.css file, clearly labeling this section (e.g., with a comment like
/* ===== CONVERSION FROM LESS ===== */). The features are similar to Sass:
-
Variables: Convert Less variables (
@variable-name) to CSS custom properties (--variable-name) - Nesting: Maintain nesting with native CSS syntax
-
Calculations: Verify all math uses
calc() -
Color Functions: Convert Less
lighten()anddarken()to nativecolor-mix() - Mixins: Apply the same solution you developed for the Sass mixin
4. Add Reflection Comments
Add multiline comments to your converted-styles.css file:
- At the top of the file, include an overall reflection on the conversion process
- Within each labeled section (Sass and Less), add multiline comments reflecting on that specific conversion
In your reflections, address these points:
- What was straightforward to convert?
- What required creative problem-solving?
- How did you handle the mixin pattern?
- What are the advantages of native CSS over preprocessors for this code?
- Are there any scenarios where the preprocessor version might still be preferable?
Your reflection should be honest and thoughtful. There is no single correct answer for handling mixins in native CSS, so explain your reasoning and any tradeoffs you considered. Make sure each section is clearly labeled so your instructor can easily identify which conversion comes from which source.
Hints and Considerations
Color Conversion with color-mix()
When converting lighten() and darken(), use color-mix() to blend colors
with white or black:
/* Sass: lighten($color, 10%) */
/* Native CSS: mix color with 10% white */
background-color: color-mix(in srgb, var(--primary-color) 90%, white);
/* Sass: darken($color, 8%) */
/* Native CSS: mix color with 8% black */
background-color: color-mix(in srgb, var(--secondary-color) 92%, black);
Handling Mixins
Consider these approaches for the mixin problem:
-
Shared base class: Create a
.button-baseclass that other buttons extend (though this requires HTML changes) - Duplicate properties: Accept some duplication and define the base styles in each button variant
- CSS custom properties: Use variables to share values while duplicating the property definitions
- Restructure the design: Consider if there is a simpler approach that avoids the need for a mixin pattern
There is no perfect solution. Choose an approach that balances maintainability with practical constraints, and explain your choice in your reflection.
Testing Your Conversion
While you do not need to create an HTML file for this assignment, you should mentally verify (or create a simple test page if you wish) that your native CSS would produce the same visual result as the preprocessor code. The goal is functional equivalence.
Submission
Submit a single CSS file named converted-styles.css to Canvas. This file should contain both
conversions with clear labels indicating which section comes from which source.
Structure your file as follows:
- Start with a multiline comment containing your overall reflection on the conversion process
-
Include a clearly labeled section for the Sass conversion (e.g.,
/* ===== CONVERSION FROM SASS ===== */) -
Include a clearly labeled section for the Less conversion (e.g.,
/* ===== CONVERSION FROM LESS ===== */) - Add multiline comments within each section to reflect on that specific conversion
Ensure your code is properly formatted with consistent indentation and that your reflection comments are thoughtful and complete. Your instructor will evaluate both the technical accuracy of your conversion and the quality of your analysis.
What You Will Learn
This assignment gives you practical experience with preprocessor syntax and demonstrates the capabilities of modern native CSS. By converting real code, you will internalize the differences between preprocessor and native approaches. You will also develop critical thinking skills about CSS architecture and learn to make informed decisions when preprocessors offer features that native CSS does not yet replicate.
Understanding both preprocessors and native CSS makes you a more versatile developer, prepared to work with legacy codebases while embracing modern standards. This hands-on experience complements the historical context you learned and prepares you for the advanced CSS techniques coming in the rest of this unit.