Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
CSS Layouts: Flexbox, Grid & Responsive
  1. 1.Introduction to CSS & Selectors
  2. 2.The CSS Box Model & Basic Styling
  3. 3.Display Property & Positioning Fundamentals
  4. 4.Units, Values & Responsive Basics
  5. 5.Flexbox Fundamentals
  6. 6.Advanced Flexbox & Real-world Patterns
  7. 7.CSS Grid Fundamentals
  8. 8.Advanced CSS Grid & Area Layouts
  9. 9.Responsive Design Strategies & Mobile First
  10. 10.CSS Transitions, Transforms & Animations
  11. 11.Advanced CSS Concepts & Performance
  12. 12.Layout Techniques for Complex UIs & Interview Prep
CSS Layouts: Flexbox, Grid & Responsive
01

CSS · Lesson 1 of 12 · 3 min

Introduction to CSS & Selectors

This is about answering the fundamental "class vs. ID" question and showing you can write clean, targeted styles.

After this, you will be able to explain the core purpose of CSS and justify your choice of selectors in a code review or an interview. This is table-stakes knowledge. Get it right.

Why CSS Exists: Separating Concerns

CSS separates presentation (how things look) from structure (what things are). Your HTML defines the content: a heading, a list, a paragraph. Your CSS defines the style: the font, the color, the layout.

Keeping them separate makes your code maintainable. You can change the entire look of a site by editing CSS files without touching the HTML. This is the principle of "separation of concerns." If an interviewer asks "What is CSS for?", this is the answer.

The Core Interview Question: Class vs. ID

This is the most common question you'll get about selectors. The answer demonstrates if you think about maintainability.

Use a .class to apply a style to one or more elements. Classes are for reusable styles, like .btn-primary, .card, or .error-message. An element can have multiple classes. This is your default choice for most styling.

Use an #id to target one, single, unique element on a page. An ID must be unique. It's for things that appear only once, like #main-navigation or #site-footer.

The interview answer: "I use classes for any style I might need to reuse. They are the standard for component-based styling. I reserve IDs for targeting unique, page-level landmarks, often as hooks for JavaScript or for fragment navigation."

The trap: Overusing IDs. An ID has high specificity (which we'll cover next), making its styles hard to override. This can lead to a fight with the cascade and the dreaded !important rule. Codebases heavy with IDs are brittle and difficult to refactor. When in doubt, use a class.

How to Combine Selectors

Simple selectors are useful, but their power comes from combining them to create precise rules.

The descendant selector (a space) targets an element nested inside another. nav a selects any <a> element that is a descendant of a <nav> element, no matter how deeply nested. It provides context to your styles.

The grouping selector (a comma) applies the same rule block to multiple selectors. This keeps your code DRY (Don't Repeat Yourself).

Here is a practical example styling a common UI component.

/* Grouping selector for consistent typography */
.card h2, .card p {
  color: #333;
  margin-bottom: 0.5rem;
}

/* Class selector for the main container */
.card {
  padding: 1rem;
  border: 1px solid #eee;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

/* Descendant selector for a specific link style */
.card .card-footer a {
  color: #007bff;
  text-decoration: none;
}

/* Attribute selector for a specific type of button */
.card button[data-action="delete"] {
  background-color: #dc3545;
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  cursor: pointer;
}

Other Essential Selectors

You need to know these, but they are more specialized.

Type (or Element) Selector: Targets all elements of a given type, like h1, p, or ul. Use these for establishing global, foundational styles—for example, setting a default line-height on all paragraphs.

Attribute Selector: Targets elements based on an attribute or its value. input[type="text"] targets only text inputs. This is extremely useful for styling form elements or targeting elements with data-* attributes for JavaScript hooks.

Universal Selector (*): Targets every single element. Its power is also its danger. Never use it for general styling.

The only common, acceptable use for * is in a reset, most famously to set box-sizing.

* {
  box-sizing: border-box;
}

This rule makes layout math more intuitive. You should know what it does and be able to explain it. We will cover box-sizing in the next lesson.

Try it

Create a single HTML file. Add a <div> with the class profile-card. Inside it, add an <h2> for a name, a <p> for a short bio, and an <a> link with the class social-link. Write embedded CSS in a <style> tag to give the card a border, padding, and a subtle box shadow. Use a descendant selector to style only the link inside the card. This should take you 15 minutes.

Next: We'll look at the rules that decide which style wins when multiple selectors target the same element in "The CSS Box Model & Basic Styling".

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Next

The CSS Box Model & Basic Styling