Basic Selectors
Basic Selectors
Universal Selector
Selects all elements:
* {
margin: 0;
padding: 0;
}
Type Selector
Selects elements by tag name:
h1 {
color: navy;
}
p {
line-height: 1.6;
}
Class Selector
Selects elements with a specific class attribute:
.button {
background: blue;
color: white;
}
.button.primary {
background: darkblue;
}
ID Selector
Selects a single element with a specific ID:
#header {
background: #333;
}
Attribute Selectors
Select elements by their attributes:
/* Elements with href attribute */
a[href] {
color: blue;
}
/* Exact attribute value */
input[type="text"] {
border: 1px solid gray;
}
/* Attribute value contains word */
[class~="highlight"] {
background: yellow;
}
/* Attribute value starts with */
[href^="https"] {
color: green;
}
/* Attribute value ends with */
[href$=".pdf"] {
color: red;
}
/* Attribute value contains */
[href*="example"] {
text-decoration: underline;
}
Grouping Selectors
Apply the same styles to multiple selectors:
h1, h2, h3 {
font-family: Arial;
}
Combinators
Combinators
Combinators describe the relationship between selectors.
Descendant Selector (space)
Selects all elements inside another element:
/* All <p> inside <div> */
div p {
color: blue;
}
Child Selector (>)
Selects direct children only:
/* Only <p> that are direct children of <div> */
div > p {
color: red;
}
Adjacent Sibling (+)
Selects the first sibling immediately after:
/* First <p> after any <h1> */
h1 + p {
font-weight: bold;
}
General Sibling (~)
Selects all siblings after:
/* All <p> siblings after <h1> */
h1 ~ p {
color: gray;
}
Nesting Selector (&)
Modern CSS nesting:
.card {
background: white;
& .title {
font-size: 1.5rem;
}
&:hover {
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
}
Pseudo-classes and Pseudo-elements
Pseudo-classes and Pseudo-elements
Pseudo-classes
Select elements in a specific state:
/* Link states */
a:link { color: blue; }
a:visited { color: purple; }
a:hover { color: red; }
a:active { color: orange; }
/* Form states */
input:focus {
border-color: blue;
outline: none;
}
input:disabled {
background: gray;
}
input:checked {
accent-color: blue;
}
/* Structural */
li:first-child { font-weight: bold; }
li:last-child { border-bottom: none; }
li:nth-child(2n) { background: #f0f0f0; }
li:nth-child(3n+1) { color: red; }
/* Other useful ones */
p:not(.special) { color: gray; }
div:has(> img) { border: 1px solid; }
Pseudo-elements
Select and style specific parts of an element:
/* First line of text */
p::first-line {
font-weight: bold;
}
/* First letter */
p::first-letter {
font-size: 2em;
}
/* Before/After content */
.quote::before {
content: '\"';
}
.quote::after {
content: '\"';
}
/* Custom marker */
li::marker {
color: red;
}
/* Placeholder text */
input::placeholder {
color: gray;
}
/* Tooltip example */
.tooltip:hover::after {
content: attr(data-tooltip);
position: absolute;
background: black;
color: white;
padding: 4px 8px;
}
Practice Problems
Create a reusable React component implementing CSS Selectors. Include proper state management and accessibility.
Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where neededWrite unit and integration tests for CSS Selectors using React Testing Library.
Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility testsOptimize CSS Selectors for performance. Consider memoization, code splitting, and bundle size.
Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysisQuiz
1. What does the `>` combinator select?
2. Which pseudo-class targets an element when a user hovers over it?
3. What does `li:nth-child(2n)` select?
4. Which selector has the highest specificity?
5. What does `[href^="https"]` select?
Flashcards
Question
What is the difference between `>` and space (descendant) combinators?
Click to reveal answer
Answer
`>` selects only direct children. Space selects all descendants at any depth.
Question
What is a pseudo-class?
Click to reveal answer
Answer
A pseudo-class selects elements based on their state or position, like :hover, :focus, :first-child, :nth-child().
Question
What is a pseudo-element?
Click to reveal answer
Answer
A pseudo-element selects specific parts of an element, like ::before, ::after, ::first-line, ::first-letter.
Question
What does `[type="text"]` select?
Click to reveal answer
Answer
Elements with a type attribute equal to 'text'. This is an attribute selector.
Question
What is the `:not()` pseudo-class used for?
Click to reveal answer
Answer
To select all elements that do NOT match the given selector. Example: p:not(.special) selects all p elements without the 'special' class.
Revision Notes
Key Takeaways
- 1.Class selectors are more reusable than ID selectors
- 2.Combinators let you target elements based on relationships
- 3.Pseudo-classes target elements in specific states
- 4.Pseudo-elements target specific parts of an element
- 5.Attribute selectors are powerful for targeting based on element attributes
Interview Tips
- •Know the difference between child (>) and descendant (space) selectors
- •Be able to explain how :nth-child() works with formulas
- •Understand the difference between pseudo-classes (single colon) and pseudo-elements (double colon)
- •Be familiar with attribute selectors and their pattern matching operators
Cheat Sheet
CSS Selectors Cheat Sheet
Basic Selectors
*- Universal (all elements)h1- Type selector.class- Class selector#id- ID selector[attr]- Attribute selector
Combinators
A B- Descendant (all B inside A)A > B- Child (direct children)A + B- Adjacent siblingA ~ B- General sibling
Pseudo-classes
:hover,:active,:focus- User interaction:first-child,:last-child,:nth-child(n)- Structural:not(selector)- Negation:has(selector)- Container
Pseudo-elements
::before,::after- Generated content::first-line,::first-letter- Text parts::placeholder- Input placeholder::marker- List marker
Attribute Selectors
[attr]- Has attribute[attr="val"]- Exact match[attr^="val"]- Starts with[attr$="val"]- Ends with[attr*="val"]- Contains[attr~="val"]- Contains word