CSS helps style web pages to look nice and easy to use. Common UI use cases show how to make buttons, forms, and layouts that people expect.
Common UI use cases in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
CSS
selector {
property: value;
}Use selectors to pick HTML elements to style.
Properties control colors, sizes, spacing, and layout.
Examples
CSS
button {
background-color: blue;
color: white;
padding: 0.5rem 1rem;
border: none;
border-radius: 0.25rem;
cursor: pointer;
}
button:hover {
background-color: darkblue;
}CSS
nav {
display: flex;
gap: 1rem;
background-color: #eee;
padding: 1rem;
}CSS
input[type='text'] { border: 1px solid #ccc; padding: 0.5rem; border-radius: 0.25rem; width: 100%; max-width: 300px; }
CSS
.card { border: 1px solid #ddd; border-radius: 0.5rem; padding: 1rem; box-shadow: 0 2px 5px rgba(0,0,0,0.1); max-width: 300px; }
Sample Program
This example shows a navigation bar, a styled text input, a button with hover effect, and a card layout. It uses semantic HTML and accessible labels.
CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Common UI Use Cases</title> <style> body { font-family: Arial, sans-serif; margin: 2rem; background-color: #f9f9f9; } nav { display: flex; gap: 1rem; background-color: #eee; padding: 1rem; border-radius: 0.5rem; } nav a { text-decoration: none; color: #333; font-weight: bold; } nav a:hover { color: #0077cc; } button { background-color: #0077cc; color: white; padding: 0.5rem 1rem; border: none; border-radius: 0.25rem; cursor: pointer; font-size: 1rem; margin-top: 1rem; } button:hover { background-color: #005fa3; } input[type='text'] { border: 1px solid #ccc; padding: 0.5rem; border-radius: 0.25rem; width: 100%; max-width: 300px; font-size: 1rem; margin-top: 1rem; display: block; } .card { border: 1px solid #ddd; border-radius: 0.5rem; padding: 1rem; box-shadow: 0 2px 5px rgba(0,0,0,0.1); max-width: 300px; background-color: white; margin-top: 2rem; } </style> </head> <body> <nav aria-label="Main navigation"> <a href="#home">Home</a> <a href="#about">About</a> <a href="#contact">Contact</a> </nav> <input type="text" aria-label="Search input" placeholder="Search..." /> <button type="button">Click me</button> <section class="card" aria-label="Example card"> <h2>Card Title</h2> <p>This is a simple card to show content clearly with padding and shadow.</p> </section> </body> </html>
Important Notes
Use semantic HTML elements like <nav>, <section>, and <button> for better accessibility.
Use relative units like rem for padding and font sizes to help with responsiveness.
Always add aria-label or other accessibility attributes when needed for screen readers.
Summary
CSS styles common UI parts like buttons, navigation, inputs, and cards.
Use hover effects and spacing to improve user experience.
Semantic HTML and accessibility attributes make your UI friendly for everyone.
Practice
1. Which CSS property is commonly used to add space inside a button to make it easier to click?
easy
Solution
Step 1: Understand padding's role
Padding adds space inside an element, between its content and border, making buttons larger and easier to click.Step 2: Differentiate from margin
Margin adds space outside the element, not inside. Border and font-size do not add internal space.Final Answer:
padding -> Option CQuick Check:
Internal space in buttons = padding [OK]
Hint: Padding adds inside space; margin adds outside space [OK]
Common Mistakes:
- Confusing margin with padding
- Using border to add space
- Changing font-size to add space
2. Which CSS selector correctly targets all buttons with the class
primary?easy
Solution
Step 1: Understand class selector syntax
To select elements with a class, use a dot (.) before the class name. Combining with element name is element.class.Step 2: Analyze each option
button.primary selects allbuttonelements with classprimary. .button primary is invalid syntax. #primary button selects buttons inside an element with idprimary. button#primary selects button with idprimary.Final Answer:
button.primary -> Option AQuick Check:
Class selector with element = element.class [OK]
Hint: Use dot before class name, no spaces for element.class [OK]
Common Mistakes:
- Using space instead of dot
- Confusing id (#) with class (.)
- Wrong order of selectors
3. What will be the background color of the button when hovered, given this CSS?
button {
background-color: blue;
color: white;
}
button:hover {
background-color: green;
}medium
Solution
Step 1: Understand the hover pseudo-class
The:hoverselector changes styles when the mouse is over the element.Step 2: Check the hover background-color
On hover, the background-color changes from blue to green as defined.Final Answer:
Green -> Option BQuick Check:
Hover changes background to green [OK]
Hint: Hover styles override normal styles on mouse over [OK]
Common Mistakes:
- Ignoring :hover effect
- Confusing color with background-color
- Assuming no style change on hover
4. This CSS code is intended to center a card horizontally, but it doesn't work:
.card {
width: 300px;
margin: 0 auto 0 auto;
display: inline-block;
}
What is the main reason it fails?medium
Solution
Step 1: Understand margin auto centering
Margin auto centers block elements with a fixed width horizontally.Step 2: Check display property effect
Settingdisplay: inline-blockmakes the element inline-level, somargin: 0 autodoes not center it.Final Answer:
'display: inline-block' prevents 'margin: 0 auto' from centering block elements -> Option AQuick Check:
Inline-block disables margin auto centering [OK]
Hint: Use block display for margin auto centering [OK]
Common Mistakes:
- Using inline-block instead of block
- Wrong margin syntax
- Forgetting width units
5. You want to create a responsive navigation bar with evenly spaced links that wrap on small screens. Which CSS layout method is best suited for this?
hard
Solution
Step 1: Identify layout needs
We want links spaced evenly and wrapping on small screens, so flexible layout and wrapping are needed.Step 2: Evaluate layout methods
Flexbox supports flexible spacing and wrapping withflex-wrap: wrapandjustify-content: space-between. Grid with fixed columns won't wrap well. Floats and inline-block are outdated and less flexible.Final Answer:
Flexbox with flex-wrap: wrap and justify-content: space-between -> Option DQuick Check:
Responsive spacing and wrapping = Flexbox wrap + space-between [OK]
Hint: Use flex-wrap and justify-content for responsive nav bars [OK]
Common Mistakes:
- Using fixed widths preventing wrap
- Relying on floats for layout
- Ignoring flex-wrap property
