Bird
Raised Fist0
CSSmarkup~5 mins

Writing reusable CSS

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

Reusable CSS helps you write styles once and use them many times. This saves time and keeps your website consistent.

You want to style buttons the same way across many pages.
You need a common layout style for multiple sections.
You want to apply the same font and colors to headings everywhere.
You want to avoid repeating the same CSS code again and again.
You want to make future changes easier by updating one place.
Syntax
CSS
.className {
  property: value;
}

/* Then use the class in HTML */
<div class="className">Content</div>
Use classes to create reusable styles that can be applied to many elements.
Avoid using IDs for reusable styles because IDs are unique and meant for single elements.
Examples
This class styles buttons with a blue background and white text. You can add class="btn" to any button to get this style.
CSS
.btn {
  background-color: #007BFF;
  color: white;
  padding: 0.5rem 1rem;
  border-radius: 0.25rem;
  cursor: pointer;
}
This class centers text horizontally. Use class="text-center" on any element to center its text.
CSS
.text-center {
  text-align: center;
}
This class creates a simple card style with border and shadow. Apply class="card" to containers you want to look like cards.
CSS
.card {
  border: 1px solid #ccc;
  padding: 1rem;
  border-radius: 0.5rem;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
Sample Program

This example shows three reusable CSS classes: .btn for buttons, .text-center for centering text, and .card for a styled container. You can mix and match these classes on HTML elements to reuse styles easily.

CSS
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Reusable CSS Example</title>
  <style>
    .btn {
      background-color: #28a745;
      color: white;
      padding: 0.75rem 1.5rem;
      border: none;
      border-radius: 0.3rem;
      font-size: 1rem;
      cursor: pointer;
      transition: background-color 0.3s ease;
    }
    .btn:hover {
      background-color: #218838;
    }
    .text-center {
      text-align: center;
    }
    .card {
      border: 1px solid #ddd;
      padding: 1rem;
      border-radius: 0.5rem;
      box-shadow: 0 2px 8px rgba(0,0,0,0.1);
      max-width: 300px;
      margin: 1rem auto;
    }
  </style>
</head>
<body>
  <div class="card text-center">
    <h2>Welcome!</h2>
    <p>This is a card with centered text.</p>
    <button class="btn">Click Me</button>
  </div>
  <div class="card">
    <h3>Another Card</h3>
    <p>This card uses the same style but text is left aligned.</p>
    <button class="btn">Submit</button>
  </div>
</body>
</html>
OutputSuccess
Important Notes

Use meaningful class names that describe what the style does, like btn for buttons or text-center for centered text.

Combine multiple classes on one element to reuse different styles together.

Keep your reusable CSS small and focused on one purpose for easier maintenance.

Summary

Reusable CSS saves time and keeps your website consistent.

Use classes to write styles once and apply them many times.

Combine classes to create flexible and maintainable designs.

Practice

(1/5)
1. What is the main benefit of using reusable CSS classes in web development?
easy
A. You can write styles once and use them many times.
B. It makes the website load slower.
C. It requires writing more code for each element.
D. It only works with inline styles.

Solution

  1. Step 1: Understand the purpose of reusable CSS classes

    Reusable CSS classes allow you to write a style rule once and apply it to multiple elements, saving time and effort.
  2. Step 2: Compare options to the main benefit

    Options B, C, and D describe drawbacks or incorrect uses, while You can write styles once and use them many times. correctly states the main benefit.
  3. Final Answer:

    You can write styles once and use them many times. -> Option A
  4. Quick Check:

    Reusable CSS = Write once, use many times [OK]
Hint: Reusable classes save time by reusing styles [OK]
Common Mistakes:
  • Thinking reusable CSS slows down the site
  • Confusing reusable classes with inline styles
  • Believing reusable CSS requires more code
2. Which of the following is the correct syntax to define a reusable CSS class named button?
easy
A. button { color: blue; }
B. #button { color: blue; }
C. .button { color: blue; }
D. *button { color: blue; }

Solution

  1. Step 1: Identify CSS selector for classes

    In CSS, classes are selected using a dot (.) before the class name.
  2. Step 2: Match syntax to class selector

    .button { color: blue; } uses .button which is correct. #button { color: blue; } uses an ID selector (#), C uses an element selector, and D uses an invalid selector.
  3. Final Answer:

    .button { color: blue; } -> Option C
  4. Quick Check:

    Class selector starts with dot (.) [OK]
Hint: Class selectors start with a dot (.) in CSS [OK]
Common Mistakes:
  • Using # instead of . for classes
  • Omitting the dot before class name
  • Confusing element selectors with class selectors
3. Given the CSS below, what color will the text inside the <p> tag have?
.red { color: red; } .bold { font-weight: bold; }

Hello World

medium
A. Default browser color
B. Bold only
C. Red only
D. Red and bold

Solution

  1. Step 1: Analyze the applied classes

    The paragraph has two classes: red and bold. The red class sets text color to red, and bold sets font weight to bold.
  2. Step 2: Combine effects of both classes

    Both styles apply together, so the text will be red and bold.
  3. Final Answer:

    Red and bold -> Option D
  4. Quick Check:

    Multiple classes combine styles [OK]
Hint: Multiple classes combine their styles on one element [OK]
Common Mistakes:
  • Thinking only one class style applies
  • Confusing color and font-weight properties
  • Ignoring combined class effects
4. What is wrong with this CSS if the goal is to reuse the style for multiple buttons?
button { background-color: blue; } .button { background-color: red; }

Click me

medium
A. The class name should start with a #.
B. The element selector button and class .button conflict causing confusion.
C. Class selectors cannot be reused.
D. The CSS syntax is invalid.

Solution

  1. Step 1: Understand selector types

    The selector button targets all <button> elements, while .button targets elements with class "button".
  2. Step 2: Identify conflict in styles

    The paragraph has class "button" but is not a <button> element, so only .button applies. The similar names can confuse developers.
  3. Final Answer:

    The element selector button and class .button conflict causing confusion. -> Option B
  4. Quick Check:

    Element and class selectors with same name cause confusion [OK]
Hint: Avoid naming classes same as HTML elements [OK]
Common Mistakes:
  • Using # instead of . for classes
  • Assuming class selectors can't be reused
  • Ignoring selector specificity and conflicts
5. You want to create a reusable CSS class for a card component that has a shadow, padding, and rounded corners. Which CSS snippet correctly combines these styles for reuse?
hard
A. .card { box-shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 1rem; border-radius: 0.5rem; }
B. #card { box-shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 1rem; border-radius: 0.5rem; }
C. card { box-shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 1rem; border-radius: 0.5rem; }
D. .card { shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 1rem; rounded: 0.5rem; }

Solution

  1. Step 1: Use correct class selector syntax

    Classes use a dot (.) before the name, so .card is correct. #card { box-shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 1rem; border-radius: 0.5rem; } uses an ID selector (#), C uses an invalid element selector, and D uses incorrect CSS properties.
  2. Step 2: Verify CSS properties for shadow, padding, and rounded corners

    box-shadow, padding, and border-radius are correct CSS properties. .card { shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 1rem; rounded: 0.5rem; } uses invalid properties shadow and rounded.
  3. Final Answer:

    .card { box-shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 1rem; border-radius: 0.5rem; } -> Option A
  4. Quick Check:

    Correct class selector and CSS properties [OK]
Hint: Use dot for class and correct CSS property names [OK]
Common Mistakes:
  • Using # instead of . for classes
  • Using invalid CSS properties like shadow or rounded
  • Omitting units or using wrong selectors