CSS Tutorial

Style your project with selectors, the box model and responsive layout.

本课程正文目前为英文。

Before you begin

Complete HTML first. Save styles.css beside index.html and add <link rel="stylesheet" href="styles.css"> inside head. Examples target modern browsers.

Follow the four chapters in order, then complete the practice task. These are introductory lessons, not an exhaustive language reference.

CHAPTER 1 / 4

Connect styles and select elements

A CSS rule combines a selector and declarations. A class can be reused on several elements. Add class="card" to a section in your HTML, then put this example in styles.css. In the same origin and cascade layer, specificity helps resolve competing normal declarations; source order resolves equal specificity.

css
body { font-family: sans-serif; color: #1B1814; background: #F3EFE4; }
.card { background: white; color: #0B5853; }
.card p { line-height: 1.6; }

Chapter documentation →

Expected result

The card has a white background and green inherited text. Paragraphs inside it have increased line spacing.

Common mistake

A missing stylesheet or mismatched class name looks like a selector problem. Check the browser Network panel first. Specificity alone does not describe the entire cascade.

Try it yourself

Make only the card heading green while keeping paragraph text dark.

Check your answer

Set .card { color: #1B1814; } and .card h2 { color: #0B5853; }.

CHAPTER 2 / 4

Understand the box model

Content, padding, border and margin are different parts of a box. With content-box, width describes content. With border-box, width includes padding and border, but never margin. This makes component dimensions easier to predict.

Margin
Border
Padding
Content
css
.card {
  box-sizing: border-box;
  width: 280px;
  max-width: 100%;
  padding: 20px;
  border: 2px solid #0B5853;
  margin: 16px 0;
}

Chapter documentation →

Expected result

At a sufficient container width, the border box is 280px and content is 236px: 280 − 40 − 4.

Common mistake

Vertical margins can collapse in normal block layout. Margin is not part of the declared border-box width.

Try it yourself

Switch to content-box and calculate the outer border-box width.

Check your answer

It becomes 324px: 280 + 40 padding + 4 border, before margins.

CHAPTER 3 / 4

Arrange cards with Grid and Flexbox

Use Grid for a row-and-column card layout and Flexbox for a line of controls. Place several card sections inside an element with class="cards". Grid gap separates tracks without adding margins to individual cards.

css
.cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.card { width: auto; min-width: 0; overflow-wrap: anywhere; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

Chapter documentation →

Expected result

Cards form two equal columns. An actions container wraps its children when space runs out.

Common mistake

Flexbox is one-dimensional; wrapped lines do not automatically share the same column widths. Long unbroken content can overflow unless it can wrap.

Try it yourself

Create three cards and predict the last row.

Check your answer

The third card starts the next row in the first column; the second column stays empty.

CHAPTER 4 / 4

Practice: a responsive project grid

Build two project cards using the HTML course structure. Start with one column and introduce two only when the viewport has enough room. Use the browser responsive viewport to test both sides of the breakpoint.

css
.cards { display: grid; grid-template-columns: 1fr; gap: 1rem; }
@media (min-width: 48rem) {
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
img { max-width: 100%; height: auto; }

Chapter documentation →

Expected result

Narrow viewports show one column; wider viewports show two. Images shrink within their container.

Common mistake

Do not hide overflow on the entire page to conceal broken layout. A breakpoint should follow content needs rather than a particular phone model.

Try it yourself

Test at 375px, 768px and 1280px and with browser zoom increased.

Check your answer

Text should stay readable, controls reachable, and the page should not scroll sideways. Inspect a long URL inside a card too.

Continue learning

Review the examples against the documentation, then change one input or layout constraint and explain the result.

CSS documentation →

Choose another course →