logo

CSS 定位:通知徽章

✏️ Editable
Loading...
Live Preview
+Enter Run+R Reset

About This Exercise

在这个实验中,你将学习 CSS 定位,这是创建复杂布局的关键技能。

CSS position 属性可以让你精确控制元素在页面上的位置,脱离正常的文档流。

你将学到:

  • 理解 position: relativeabsolute 的配合使用
  • 使用 top, right, bottom, left 精确定位元素
  • 理解 z-index 控制堆叠顺序
  • 创建常见的徽章和弹出层效果
Intermediate⏱ 15-20 min

Learning Objectives

  • 理解四种定位方式的区别
  • 掌握 relative + absolute 的父子定位技巧
  • 学会使用 transform 进行精确居中
  • 理解 z-index 堆叠上下文

Scenario

你正在为一个应用创建导航栏图标。 设计要求: - 图标右上角显示红色通知徽章 - 徽章显示未读消息数量 - 徽章需要固定在图标的右上角 - 当数字超过 99 时显示 99+

Why Do This

  • CSS is fundamentally about constraining layout and visual hierarchy, not memorizing properties.
  • Clarify the box model and layout rules first, then add visual polish — less rework that way.
  • Writing styles as reusable patterns is more valuable than tweaking a single page.

Hands-on Practice

  • Write the layout skeleton first (display/gap/align), then add colors and shadows.
  • Extract repeated styles into reusable classes.
  • Use validation rules to reverse-engineer the design intent of your current styles.

Common Mistakes

  • Adding margin after margin to fix positioning while ignoring layout containers.
  • Selectors nested too deep, causing style coupling.
  • Only caring about how it looks without considering maintainability.

Deliverable

A reusable component style template (layout rules + visual rules + naming conventions).

My Progress

Completed Steps0 / 4
Total Attempts0
Best Score0%

Completion Criteria (Checkable)

Confirm before completing this Lab

Reflection Questions (Recommended)

What was your main layout decision? Why did you choose it?

What change did you make specifically for maintainability?

What would you prioritize improving next time?

Tags

CSSPositionLayoutBadge