Why Practice CSS with a Live Preview?
CSS is easiest to learn when you can see the effect of each change immediately. With a live preview there is no save, switch and refresh cycle: you change a value and watch the page respond, which makes it quick to build an intuition for spacing, sizing and layout.
This guide goes from your first selector to Flexbox and Grid layouts. Every example is short enough to paste into a browser-based editor and change.
Step 1: Open an Editor with a Live Preview
Open the HTML Runner. It starts with three files in tabs: index.html, styles.css and scripts.js, with a live preview on the right. Write the HTML for an example in index.html and its CSS in styles.css; the runner applies the stylesheet automatically. In a normal project you connect the two yourself with <link rel="stylesheet" href="styles.css"> inside <head>.
Step 2: Understand the CSS Syntax
Every CSS rule follows this pattern:
selector {
property: value;
another-property: value;
}
- Selector โ which HTML element(s) to style
- Property โ what aspect to style (color, size, spacing)
- Value โ what to set the property to
Example: To make all paragraph text red:
p {
color: red;
}
Step 3: Learn the Most Important CSS Selectors
- Element selector โ
p,h1,divโ targets all elements of that type - Class selector โ
.my-classโ targets elements with that class attribute - ID selector โ
#my-idโ targets one specific element - Descendant selector โ
.card pโ targets paragraphs inside .card elements - Pseudo-class โ
a:hoverโ targets elements in a specific state - Pseudo-element โ
p::first-lineโ targets part of an element
Step 4: Master the Box Model
Every HTML element is a rectangular box. Understanding the box model is fundamental to CSS layout:
- Content โ the actual text or image inside the element
- Padding โ space between content and border (
padding: 16px) - Border โ outline around the padding (
border: 1px solid #ccc) - Margin โ space outside the border, between elements (
margin: 24px 0)
Practice: write a div with a background color, then adjust padding and margin to see the box model in action.
๐ก Essential CSS Tip: box-sizing
Always add *, *::before, *::after { box-sizing: border-box; } at the top of your CSS. This makes padding and border part of the element's total width, making layouts much more predictable.
Step 5: Style Text and Typography
Typography is one of the highest-impact CSS skills. Key properties:
font-familyโ typeface (e.g.,'Inter', system-ui, sans-serif)font-sizeโ text size (e.g.,16px,1.125rem)font-weightโ boldness (100โ900, orbold)line-heightโ space between lines (e.g.,1.6)colorโ text color (hex, rgb, hsl, or named colors)text-alignโ alignment (left, center, right, justify)letter-spacingโ space between characters
Step 6: Use Colors and Backgrounds
CSS offers multiple ways to specify color:
- Hex โ
#3b82f6(the most common format) - RGB โ
rgb(59, 130, 246) - HSL โ
hsl(217, 91%, 60%)(most intuitive for adjusting) - Named colors โ
red,blue,coral
Backgrounds can be solid colors, gradients, or images:
background-color: #f0f4ff;background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);background-image: url('image.jpg');
Step 7: Build Layouts with Flexbox
Flexbox is the modern standard for one-dimensional layouts (rows or columns). Enable it with:
.container {
display: flex;
justify-content: space-between; /* horizontal alignment */
align-items: center; /* vertical alignment */
gap: 16px; /* space between items */
}
Key Flexbox properties to practice:
flex-direction: row | columnjustify-content: flex-start | center | space-between | space-aroundalign-items: stretch | center | flex-start | flex-endflex-wrap: nowrap | wrapflex: 1on child elements to make them fill available space equally
Step 8: Master CSS Grid for Complex Layouts
CSS Grid is ideal for two-dimensional layouts. Use it for page structure and card grids:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
gap: 20px;
}
Common Grid patterns:
grid-template-columns: 200px 1frโ sidebar + main contentgrid-template-columns: repeat(auto-fill, minmax(300px, 1fr))โ responsive card gridgrid-column: span 2โ make an element span multiple columns
Step 9: Add CSS Animations and Transitions
CSS animations bring pages to life without JavaScript:
/* Transition: smooth hover effect */
.button {
background: #3b82f6;
transition: background 0.2s ease, transform 0.2s ease;
}
.button:hover {
background: #2563eb;
transform: translateY(-2px);
}
/* Animation: spinning loader */
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.spinner {
animation: spin 1s linear infinite;
}
Step 10: Make It Responsive with Media Queries
Responsive CSS adapts your layout to different screen sizes:
/* Mobile first: base styles for small screens */
.grid {
display: grid;
grid-template-columns: 1fr; /* single column on mobile */
}
/* Tablet and up */
@media (min-width: 768px) {
.grid {
grid-template-columns: repeat(2, 1fr);
}
}
/* Desktop and up */
@media (min-width: 1024px) {
.grid {
grid-template-columns: repeat(3, 1fr);
}
}
CSS Best Practices to Follow
- Use CSS custom properties (variables) for colors and spacing:
--primary: #3b82f6 - Write mobile-first CSS and add larger breakpoints progressively
- Use relative units (
rem,em,%) over fixed pixels for accessibility - Keep specificity low โ prefer classes over IDs for styling
- Group related properties (typography, spacing, colors) for readability
- Comment sections of your CSS for team readability
Related Articles
Conclusion
A live preview gives you immediate feedback on every CSS change, which makes it a good way to learn and to try out a layout idea. Start with the basics (selectors, box model, typography), build toward layouts (Flexbox, Grid), and add polish with animations and responsive design. Everything you need is already in your browser.
Practice CSS with Live Preview Now
Paste any example from this guide into the HTML Runner and change the values to see what each property does.
Open the HTML Runner โ