What is CSS? — Cascading Style Sheets Explained
Definition
CSS (Cascading Style Sheets) is the language used to describe the visual presentation of HTML documents. It separates content structure (HTML) from visual design, allowing the same HTML to be styled differently for screens, print, or screen readers. CSS uses selectors to target HTML elements and declarations (property: value pairs) to apply styles. The "cascading" refers to the set of rules that determine which styles apply when multiple rules conflict.
How It Works
CSS works through a cascade of specificity and inheritance. When multiple rules match the same element, the one with highest specificity wins (inline styles > IDs > classes > elements). If specificity is equal, the last rule in source order wins. Many properties like color and font-size are inherited by child elements. CSS layout is primarily handled by the box model (margin, border, padding, content), flexbox (one-dimensional layout), and CSS Grid (two-dimensional layout). Media queries enable responsive design by applying different styles at different viewport sizes.
Common Use Cases
- ▸Styling colors, typography, spacing, and borders on web pages
- ▸Building responsive layouts with flexbox and CSS Grid
- ▸Creating hover effects, transitions, and animations without JavaScript
- ▸Implementing dark mode with CSS custom properties (variables)
- ▸Targeting print, screen, or accessibility features with media queries
Example
/* Flexbox centering */
.container {
display: flex;
align-items: center;
justify-content: center;
gap: 1rem;
}
/* Responsive column */
@media (max-width: 768px) {
.sidebar {
display: none;
}
}Related Tools
Pretty-print CSS with proper indentation and structure.
Remove whitespace and comments from CSS to reduce file size.
Map common CSS properties to approximate Tailwind CSS utility classes.
Interactive CSS flexbox playground with live preview and copyable CSS code.
Interactive CSS grid playground with live preview and copyable CSS code.
FAQ
- What is CSS specificity?
- Specificity is a weight assigned to each selector that determines which rule wins when conflicts occur. Inline styles are most specific (1,0,0,0), then IDs (0,1,0,0), then classes/attributes (0,0,1,0), then elements (0,0,0,1). !important overrides all specificity rules and should be used sparingly.
- What is the difference between flexbox and CSS Grid?
- Flexbox is designed for one-dimensional layouts: a single row or column. CSS Grid is designed for two-dimensional layouts: rows and columns simultaneously. Flexbox is great for component-level alignment; Grid is ideal for page-level layout.
- What are CSS custom properties (variables)?
- CSS custom properties are defined with -- prefix (--primary-color: #007bff) and used with var(). They are scoped to elements (often :root for global variables), can be changed by JavaScript, and inherit to child elements. They are the foundation of CSS-based theming systems.