CSS Selectors and Box Model
CSS Selectors
| Selector | Example | Description |
|---|---|---|
| Element | p |
All paragraph elements |
| Class | .product-name |
Elements with class="product-name" |
| ID | #header |
Element with id="header" |
| Descendant | .product p |
All p inside .product |
| Child | .product > p |
Direct children only |
| Attribute | [type="email"] |
Elements with type="email" |
| Pseudo-class | :hover, :focus, :nth-child(2n) |
State-based |
| Pseudo-element | ::before, ::after |
Content generation |
/* Specificity (low to high): */
/* element < .class < #id < inline < !important */
p { color: blue; } /* specificity: 0,0,1 */
.product-name { color: green; } /* specificity: 0,1,0 */
#header .nav a { color: red; } /* specificity: 0,1,2 */
/* Attribute selectors */
input[type="text"] { border: 1px solid #ccc; }
a[href^="https"] { color: green; } /* starts with */
a[href$=".pdf"] { color: red; } /* ends with */
/* Pseudo-classes */
a:hover { color: blue; } /* mouse over */
input:focus { border-color: #0066cc; } /* focused */
li:nth-child(even) { background: #f5f5f5; } /* every other */
/* Pseudo-elements */
.required::after {
content: " *";
color: red;
}
blockquote::before {
content: "\201C"; /* left double quote */
font-size: 2em;
}
The Box Model
Every HTML element is a box with four areas:
+-------------------------------------------+
| MARGIN |
| +--------------------------------------+ |
| | BORDER | |
| | +--------------------------------+ | |
| | | PADDING | | |
| | | +--------------------------+ | | |
| | | | CONTENT | | | |
| | | +--------------------------+ | | |
| | +--------------------------------+ | |
| +--------------------------------------+ |
+-------------------------------------------+
/* Standard box model */
.product-card {
width: 300px; /* content width */
padding: 20px; /* space inside border */
border: 1px solid #ddd;
margin: 15px; /* space outside border */
/* Total width = 300 + 20*2 + 1*2 + 15*2 = 372px */
}
/* Border-box (recommended) */
.product-card {
box-sizing: border-box;
width: 300px; /* total width including padding and border */
padding: 20px;
border: 1px solid #ddd;
margin: 15px;
/* Total width = 300px + margin only */
}
/* Global border-box */
*, *::before, *::after {
box-sizing: border-box;
}
Common CSS Patterns
/* Centering */
.container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Text truncation */
.product-name {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 200px;
}
/* Sticky footer */
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex: 1;
}
Magento LESS Variables
Magento uses LESS instead of plain CSS:
// Magento LESS variables (lib/web/css/source/lib/variables.less)
@primary__color: #1979c2;
@secondary__color: #f26322;
@page__background-color: #ffffff;
@page__width: 1280px;
@indent__base: 20px;
// Using variables in custom CSS
.product-card {
background: @page__background-color;
border: 1px solid darken(@page__background-color, 10%);
max-width: @page__width;
margin: 0 auto;
padding: @indent__base;
}
Flexbox Layout
Flexbox Fundamentals
Flexbox provides efficient alignment and distribution of space among items in a container.
Flex Container Properties
/* Enable flexbox */
.product-grid {
display: flex;
flex-wrap: wrap; /* Allow items to wrap */
gap: 20px; /* Space between items */
justify-content: center; /* Horizontal alignment */
align-items: stretch; /* Vertical alignment */
}
/* justify-content options */
justify-content: flex-start; /* Items at start */
justify-content: flex-end; /* Items at end */
justify-content: center; /* Items centered */
justify-content: space-between; /* Equal space between */
justify-content: space-around; /* Equal space around */
justify-content: space-evenly; /* Equal space everywhere */
/* align-items options */
align-items: stretch; /* Fill container height */
align-items: flex-start; /* Align to top */
align-items: flex-end; /* Align to bottom */
align-items: center; /* Center vertically */
align-items: baseline; /* Align text baselines */
Flex Item Properties
.product-item {
flex: 1; /* grow, shrink, basis */
/* flex: 1 1 0 = grow equally, shrink equally, start from 0 width */
}
.product-item.featured {
flex: 2; /* Takes twice the space */
}
.product-item.sidebar {
flex: 0 0 300px; /* Don't grow, don't shrink, fixed 300px */
}
/* Individual alignment */
.product-item.promo {
align-self: flex-end; /* Override container's align-items */
}
Practical Flexbox Examples
/* Product card layout */
.product-card {
display: flex;
flex-direction: column; /* Stack vertically */
border: 1px solid #ddd;
border-radius: 8px;
overflow: hidden;
}
.product-card .image {
height: 200px;
object-fit: cover;
}
.product-card .content {
flex: 1; /* Take remaining space */
padding: 16px;
display: flex;
flex-direction: column;
}
.product-card .price {
margin-top: auto; /* Push to bottom */
font-weight: bold;
}
/* Navigation bar */
.nav-bar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 24px;
height: 64px;
}
.nav-links {
display: flex;
gap: 24px;
list-style: none;
}
/* Centered content */
.hero {
display: flex;
justify-content: center;
align-items: center;
min-height: 400px;
}
Flexbox vs Display Properties
| Property | Use Case |
|---|---|
display: block |
Full-width elements (div, p, h1) |
display: inline |
Flow within text (span, a) |
display: inline-block |
Flow within text with block properties |
display: flex |
1D layout (row or column) |
display: grid |
2D layout (rows and columns) |
CSS Grid and Responsive Design
CSS Grid
Grid provides two-dimensional layout capabilities (rows AND columns).
/* Product grid */
.product-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 24px;
padding: 24px;
}
/* Fixed columns */
.dashboard {
display: grid;
grid-template-columns: 250px 1fr 300px; /* sidebar, main, aside */
gap: 20px;
min-height: 100vh;
}
/* Named areas */
.page-layout {
display: grid;
grid-template-areas:
"header header header"
"nav main sidebar"
"footer footer footer";
grid-template-columns: 200px 1fr 250px;
grid-template-rows: auto 1fr auto;
}
.header { grid-area: header; }
.nav { grid-area: nav; }
.main { grid-area: main; }
.sidebar { grid-area: sidebar; }
.footer { grid-area: footer; }
Responsive Design
Media Queries
/* Mobile-first approach (recommended) */
/* Base styles for mobile */
.product-grid {
display: flex;
flex-direction: column;
gap: 16px;
}
/* Tablet (768px and up) */
@media (min-width: 768px) {
.product-grid {
flex-direction: row;
flex-wrap: wrap;
}
.product-item {
flex: 0 0 calc(50% - 8px);
}
}
/* Desktop (1024px and up) */
@media (min-width: 1024px) {
.product-item {
flex: 0 0 calc(25% - 12px);
}
}
/* Large desktop (1440px and up) */
@media (min-width: 1440px) {
.page-wrapper {
max-width: 1440px;
margin: 0 auto;
}
}
Responsive Units
/* Different units */
.container {
width: 100%; /* Percentage of parent */
max-width: 1200px; /* Maximum width */
padding: 2rem; /* Relative to font size */
font-size: 1rem; /* 16px default */
margin: 0 auto;
}
/* Viewport units */
.hero {
min-height: 100vh; /* Full viewport height */
width: 100vw; /* Full viewport width */
font-size: 5vw; /* 5% of viewport width */
}
/* Clamp for fluid typography */
h1 {
font-size: clamp(1.5rem, 4vw, 3rem);
/* Minimum 24px, preferred 4vw, maximum 48px */
}
Magento Theme Customization
// Custom LESS for Magento theme
// web/css/source/_extend.less
// Override Magento variables
@primary__color: #e74c3c;
@page__width: 1400px;
// Custom product grid
.product-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(@product-card-width, 1fr));
gap: @indent__base;
}
.product-card {
border: 1px solid @color-gray-light2;
border-radius: @border-radius-base;
transition: box-shadow 0.3s;
&:hover {
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
}
// Responsive breakpoints
@media (max-width: @screen__m) {
.product-grid {
grid-template-columns: repeat(2, 1fr);
}
}
@media (max-width: @screen__s) {
.product-grid {
grid-template-columns: 1fr;
}
}
Key Takeaway
Use Flexbox for 1D layouts (rows or columns) and CSS Grid for 2D layouts. Always use mobile-first responsive design with min-width media queries. Magento uses LESS for CSS preprocessing with predefined variables.
Quiz
1. What is the difference between Flexbox and CSS Grid?
2. What does box-sizing: border-box do?
3. What is the recommended approach for responsive design?
4. What does flex: 1 mean in a flex item?
5. What is the CSS specificity order (low to high)?
Flashcards
Question
What is the CSS box model?
Click to reveal answer
Answer
Every element is a box with four layers: content (inner content), padding (space inside border), border (edge of element), and margin (space outside border).
Question
What does box-sizing: border-box do?
Click to reveal answer
Answer
Includes padding and border in the element's width/height calculation. Without it, adding padding increases the element's total size.
Question
When to use Flexbox vs Grid?
Click to reveal answer
Answer
Flexbox: 1D layouts (row OR column), like navigation bars, card rows. Grid: 2D layouts (rows AND columns), like page layouts, product grids.
Question
What is mobile-first responsive design?
Click to reveal answer
Answer
Start with styles for small screens, then use @media (min-width: 768px) to enhance for larger screens. Better for performance and SEO.
Question
What does justify-content do in Flexbox?
Click to reveal answer
Answer
Controls horizontal alignment of flex items along the main axis. Options: flex-start, flex-end, center, space-between, space-around, space-evenly.
Question
What is CSS specificity?
Click to reveal answer
Answer
The priority system that determines which CSS rule wins when multiple rules target the same element. Order: element < class < ID < inline < !important.
Question
How does Magento use LESS?
Click to reveal answer
Answer
Magento compiles LESS files to CSS. Variables (@primary__color) are defined in lib/web/css/source/lib/variables.less. Custom styles go in _extend.less.
Question
What does gap do in Flexbox/Grid?
Click to reveal answer
Answer
Sets the spacing between flex/grid items without using margins. Replaces margin hacks. gap: 20px adds 20px between all items.
Revision Notes
Key Takeaways
- 1. CSS selectors target elements by tag, class, ID, attribute, or pseudo-class
- 2. The box model: content + padding + border + margin; use border-box
- 3. Flexbox for 1D layouts, CSS Grid for 2D layouts
- 4. Mobile-first responsive design uses min-width media queries
- 5. Specificity: element < class < ID < inline < !important
- 6. Magento uses LESS for CSS preprocessing with predefined variables
- 7. Flexbox gap property is the modern way to add spacing between items
Interview Tips
- • Explain the difference between Flexbox and CSS Grid
- • Describe the box model and why border-box is recommended
- • Know the CSS specificity hierarchy
- • Explain mobile-first vs desktop-first responsive design
- • Understand how Magento's LESS compilation works
Cheat Sheet
CSS Cheat Sheet
Box Model:
*, *::before, *::after { box-sizing: border-box; }
Flexbox:
.container { display: flex; gap: 20px; }
.item { flex: 1; /* grow equally */ }
Grid:
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 24px;
}
Media Queries:
@media (min-width: 768px) { /* tablet+ */ }
@media (min-width: 1024px) { /* desktop+ */ }
Specificity:
element (0,0,1) < .class (0,1,0) < #id (1,0,0) < inline (1,0,0,0) < !important
Magento LESS Variables:
@primary__color, @page__width, @indent__base, @screen__m, @screen__s