Color Mode

Refactor CSS Using Modern Selectors

In this assignment, you will refactor existing CSS to use modern selectors and nesting. You will replace repetitive selector patterns with :is() and :where(), implement parent-based logic with :has(), use :not() for exclusions, and convert flat CSS to nested CSS. This refactoring will reduce code repetition, improve readability, and demonstrate better specificity management.

Assignment Overview

You are provided with a working HTML page and CSS file that uses older patterns: repetitive selectors, flat (non-nested) structure, and unnecessarily high specificity. Your task is to refactor the CSS using modern techniques while maintaining the exact same visual appearance. The goal is to write cleaner, more maintainable CSS that is easier to understand and modify.

Learning Objectives

Setup Instructions

1. Download Starter Files

Download the starter files from refactoring.zip. Extract the files and you will find:

2. Add to Your Portfolio

Create a new directory in your portfolio for this assignment:


        portfolio/
        └── unit-1/
            └── css-refactoring/
                ├── index.html
                ├── styles-old.css
                └── styles-new.css
    

3. Verify Starting Point

Open index.html in your browser. Notice that styles-old.css is commented out and styles-new.css is linked. The page will appear unstyled initially. Your goal is to recreate the exact same appearance using modern CSS techniques in styles-new.css.

Reference the old styles: You can temporarily uncomment styles-old.css to see what the page should look like when properly styled. Make sure to comment it back out when working on your refactored version.

Requirements

Your refactored CSS must meet all of the following requirements. Each requirement demonstrates your understanding of modern selectors and CSS organization.

1. Use CSS Nesting

2. Replace Repetitive Selectors with :is()

3. Manage Specificity with :where()

4. Implement :has() for Conditional Styling

5. Use :not() for Exclusions

6. Maintain Visual Parity

Refactoring Strategy

1. Analyze the Old CSS

Before writing code, review styles-old.css and identify patterns. Look for:

2. Plan Your Structure

Decide how to organize your nested CSS. Group related rules together and plan your nesting hierarchy. Consider which selectors should be at the root level and which should be nested.

3. Refactor Incrementally

Refactor one section at a time. Work on the navigation, then the hero section, then the cards, and so on. Test each section as you complete it to ensure visual parity.

4. Compare and Document

After completing your refactoring, compare your new CSS with the old CSS. Note the improvements in line count, readability, and specificity management.

Example Refactoring Patterns

Here are some patterns you will encounter in the starter CSS and how to approach them:

Pattern 1: Repetitive Selectors


        /* Old: repetitive */
        .header h1 { }
        .header h2 { }
        .header h3 { }
        .footer h1 { }
        .footer h2 { }
        .footer h3 { }
        
        /* New: using :is() and nesting */
        :is(.header, .footer) {
            :is(h1, h2, h3) {
                /* styles */
            }
        }
    

Pattern 2: High Specificity


        /* Old: high specificity from IDs */
        #navigation ul li a { }
        
        /* New: using :where() for zero specificity */
        :where(#navigation) {
            ul {
                li {
                    a { }
                }
            }
        }
    

Pattern 3: Conditional Classes


        /* Old: requires class added by JavaScript */
        .card.has-image { }
        
        /* New: pure CSS with :has() */
        .card:has(img) { }
    

Testing Checklist

Before submitting, verify that your refactored CSS meets these criteria:

Submission

Once you have completed your refactoring and tested thoroughly, document your work and submit it for evaluation. Complete the form below with your refactoring decisions and analysis, then download the PDF and submit it to Canvas.

CSS Refactoring Documentation
Implementation Details
Provide the link to your refactored implementation in your portfolio.
Portfolio Link
Understanding Trade-offs
Reflect on the decisions you made during refactoring. These questions assess your understanding of when and why to use modern selectors.
What trade-offs did you encounter during refactoring?
Deeper Understanding
How would you explain to another developer when NOT to use :has()?
What are the downsides of using :is() everywhere?
Application to Your Work
Consider how these techniques apply beyond this assignment to your real projects.
Review projects from your previous web development courses. Identify specific sections where modern selectors would improve the code.
Additional Observations
Optional: Add any other insights, patterns you discovered, or questions that arose during your refactoring.

Evaluation Criteria

Your refactored CSS will be evaluated on how effectively it demonstrates modern selector usage and improves upon the original code:

Tips for Success