Introduction

CSS (Cascading Style Sheets) controls how HTML content looks. It defines colors, spacing, layout, and typography across the web.

In this documentation, you'll learn core CSS concepts with examples you can copy and run.

CSS can be applied inline, embedded in the document, or linked as an external stylesheet.

💡
Tip: External stylesheets are preferred — they keep style separate from structure and are cached by the browser for faster repeat loads.
/* Link a stylesheet */
<link rel="stylesheet" href="styles.css">
Selectors

Selectors target elements to style. They can be simple or complex, matching by tag name, class, ID, attribute, or state.

p { line-height: 1.7; }
.highlight { background: yellow; }
#hero { padding: 2rem; }
a[target="_blank"] { text-decoration: underline; }
a:hover { text-decoration: none; }
📌
Note: Specificity determines which rules win when selectors conflict. IDs beat classes, classes beat element selectors.
Box Model

Every element is a rectangle composed of content, padding, border, and margin. Understanding this model is fundamental to controlling layout precisely.

*, *::before, *::after { box-sizing: border-box; }
.card {
  width: 300px;
  padding: 1rem;
  border: 1px solid #ddd;
  margin: 1rem auto;
}
Flexbox

Flexbox is a one-dimensional layout system designed for aligning items along a single axis — horizontally or vertically with ease.

.toolbar {
  display: flex;
  gap: 1rem;
  justify-content: space-between;
  align-items: center;
}
.item { flex: 1 1 200px; }
💡
Tip: Use gap instead of margins between flex children — it's cleaner and doesn't bleed on outer edges.
Grid

CSS Grid is a two-dimensional layout system — perfect for page-level structure where you need control over both rows and columns simultaneously.

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}
.gallery__item--wide { grid-column: span 2; }
📌
Note: Use Flexbox for component-level layout, Grid for page-level layout — they're designed to complement each other.
Typography

Good typography improves readability, establishes visual hierarchy, and gives your design a distinctive personality. Start with a solid type scale.

html { font-size: 16px; }
h1 { font-size: 2rem; }
p  { font-size: 1rem; line-height: 1.7; }
Color and Backgrounds

Color sets mood, guides attention, and is critical for accessibility. Always verify contrast ratios against WCAG 2.1 guidelines before shipping.

.hero {
  background:
    linear-gradient(to bottom, rgba(0,0,0,.4), rgba(0,0,0,.4)),
    url('hero.jpg') center/cover no-repeat;
  color: white;
  padding: 4rem 2rem;
}
Responsive Design

Responsive design ensures your site works across all screen sizes using fluid layouts, flexible images, and strategic media queries.

.card { padding: 1rem; }
@media (min-width: 768px) {
  .card { padding: 2rem; }
}
💡
Tip: Design mobile-first — start with the smallest layout, then add min-width breakpoints to enhance for larger screens.