Reusable CSS helps you write styles once and use them many times. This saves time and keeps your website consistent.
Writing reusable CSS
.className { property: value; } /* Then use the class in HTML */ <div class="className">Content</div>
class="btn" to any button to get this style..btn { background-color: #007BFF; color: white; padding: 0.5rem 1rem; border-radius: 0.25rem; cursor: pointer; }
class="text-center" on any element to center its text..text-center { text-align: center; }
class="card" to containers you want to look like cards..card { border: 1px solid #ccc; padding: 1rem; border-radius: 0.5rem; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
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.
<!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>
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.
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
Solution
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.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.Final Answer:
You can write styles once and use them many times. -> Option AQuick Check:
Reusable CSS = Write once, use many times [OK]
- Thinking reusable CSS slows down the site
- Confusing reusable classes with inline styles
- Believing reusable CSS requires more code
button?Solution
Step 1: Identify CSS selector for classes
In CSS, classes are selected using a dot (.) before the class name.Step 2: Match syntax to class selector
.button { color: blue; } uses.buttonwhich is correct. #button { color: blue; } uses an ID selector (#), C uses an element selector, and D uses an invalid selector.Final Answer:
.button { color: blue; } -> Option CQuick Check:
Class selector starts with dot (.) [OK]
- Using # instead of . for classes
- Omitting the dot before class name
- Confusing element selectors with class selectors
.red { color: red; } .bold { font-weight: bold; }Hello World
Solution
Step 1: Analyze the applied classes
The paragraph has two classes:redandbold. Theredclass sets text color to red, andboldsets font weight to bold.Step 2: Combine effects of both classes
Both styles apply together, so the text will be red and bold.Final Answer:
Red and bold -> Option DQuick Check:
Multiple classes combine styles [OK]
- Thinking only one class style applies
- Confusing color and font-weight properties
- Ignoring combined class effects
button { background-color: blue; } .button { background-color: red; }Click me
Solution
Step 1: Understand selector types
The selectorbuttontargets all <button> elements, while.buttontargets elements with class "button".Step 2: Identify conflict in styles
The paragraph has class "button" but is not a <button> element, so only.buttonapplies. The similar names can confuse developers.Final Answer:
The element selectorbuttonand class.buttonconflict causing confusion. -> Option BQuick Check:
Element and class selectors with same name cause confusion [OK]
- Using # instead of . for classes
- Assuming class selectors can't be reused
- Ignoring selector specificity and conflicts
Solution
Step 1: Use correct class selector syntax
Classes use a dot (.) before the name, so.cardis 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.Step 2: Verify CSS properties for shadow, padding, and rounded corners
box-shadow,padding, andborder-radiusare correct CSS properties. .card { shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 1rem; rounded: 0.5rem; } uses invalid propertiesshadowandrounded.Final Answer:
.card { box-shadow: 0 4px 6px rgba(0,0,0,0.1); padding: 1rem; border-radius: 0.5rem; } -> Option AQuick Check:
Correct class selector and CSS properties [OK]
- Using # instead of . for classes
- Using invalid CSS properties like shadow or rounded
- Omitting units or using wrong selectors
Ready to prove it?
Pass the test and this pattern moves unknown
