Skip to content
Subjects · ⁨科目⁩
  • 1 HTML basics · ⁨HTML 基础⁩
    1.1

    你的第一个网页

    English

    HTML 超文本标记语言 describes a web page with tags 标签. A tag is a word in angle brackets, like <p>. Most come in pairs — an opening tag <p> and a closing tag </p> wrap some content. A tag plus its content is an element 元素.

    • The browser draws whatever is between the tags.
    • A tag you forget to close, or spell wrong, may not show correctly.
    中文

    HTML(超文本标记语言)用标签(tag)来描述一个网页。标签是尖括号里的一个词,比如 <p>。大多数标签成对出现——一个开始标签 <p> 和一个结束标签 </p> 把内容包起来。一个标签加上它的内容就是一个元素(element)。

    <p>Hello, world!</p>
    <p>This is my first web page.</p>
    
    • 浏览器会画出标签之间的内容。
    • 忘记闭合或拼错的标签,可能显示不正确。
    One element = opening tag + content + closing tag
    One element = opening tag + content + closing tag
    1.2

    标题与段落

    English

    Headings 标题 run from <h1> (biggest) down to <h6> (smallest). A paragraph 段落 is <p>. Use exactly one <h1> for the page's main title.

    中文

    标题(heading)从 <h1>(最大)到 <h6>(最小)。段落(paragraph)是 <p>。整页只用一个 <h1> 作为主标题。

    <h1>My Blog</h1>
    <h2>About me</h2>
    <p>I am learning to build web pages.</p>
    
    1.3

    文本格式

    English

    Make text bold 加粗 with <strong> and italic 斜体 with <em>. A line break 换行 is <br> — it has no closing tag.

    Common mistakes

    • Most tags come in pairs — <p>...</p>. Forgetting the closing tag breaks the layout below it.
    • The tag goes in angle brackets; the text you see goes between the opening and closing tags.
    • Headings run <h1> (biggest) to <h6> — do not pick a heading just for its size.
    中文

    用 <strong> 让文字加粗(bold),用 <em> 让文字变斜体(italic)。换行(line break)是 <br>——它没有结束标签。

    <p><strong>Warning:</strong> read this carefully.</p>
    <p>First line<br>second line</p>
    <p>You can <em>emphasise</em> any word.</p>
    

    常见错误

    • 大多数标签成对出现——<p>...</p>。忘了闭合标签会破坏它下面的布局。
    • 标签写在尖括号里;你看到的文字写在开始和结束标签之间。
    • 标题从 <h1>(最大)到 <h6>——不要只为了字号去选标题级别。
    Vocabulary · ⁨词汇⁩
    English中文Pinyin · ⁨拼音⁩
    HTML/ˌeɪtʃ tiː em ˈel/超文本标记语言chāo wén běn biāo jì yǔ yán
    tag/tæɡ/标签biāo qiān
    element/ˈelɪmənt/元素yuán sù
    heading/ˈhedɪŋ/标题biāo tí
    paragraph/ˈpærəɡræf/段落duàn luò
    bold/bəʊld/加粗jiā cū
    italic/ɪˈtælɪk/斜体xié tǐ
    line break/laɪn breɪk/换行huàn háng
  • 2 Lists, links & images · ⁨列表、链接与图片⁩
    2.1

    列表

    English

    An unordered list 无序列表 <ul> shows bullets; an ordered list 有序列表 <ol> shows numbers. Each list item 列表项 is an <li>.

    中文

    无序列表(unordered list)<ul> 显示圆点;有序列表(ordered list)<ol> 显示数字。每个列表项(list item)是一个 <li>。

    <ul>
      <li>Apples</li>
      <li>Oranges</li>
    </ul>
    <ol>
      <li>Wake up</li>
      <li>Go to school</li>
    </ol>
    
    ul 用于无序列表,ol 用于有序列表;每个项目项为 li
    ul 用于无序列表,ol 用于有序列表;每个项目项为 li
    2.2

    链接

    English

    A link 链接 is an anchor <a>. Its href attribute 属性 holds the address it points to.

    • Add target="_blank" to open the link in a new tab.
    中文

    链接(link)是一个锚 <a>。它的 href 属性(attribute)保存它指向的地址。

    <a href="https://example.com">Visit Example</a>
    <br>
    <a href="page2.html">Go to the next page</a>
    
    • 加上 target="_blank" 可以在新标签页打开链接。
    2.3

    图片

    English

    An image 图片 is <img> — it has no closing tag. src is the picture's address; alt is text shown if the image cannot load (and read aloud to blind users).

    Common mistakes

    • An image needs src (the file) and alt (a description); a link needs href.
    • <img> has no separate closing tag.
    • A broken src path shows nothing — check the file name and folder.
    中文

    图片(image)是 <img>——它没有结束标签。src 是图片的地址;alt 是当图片无法加载时显示的文本(也会读给视障用户听)。

    <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Bitmap_VS_SVG.svg/120px-Bitmap_VS_SVG.svg.png"
         alt="bitmap versus vector" width="120">
    <p>If the picture is missing, the alt text shows instead.</p>
    

    常见错误

    • 图片需要 src(文件)和 alt(描述);链接需要 href。
    • <img> 没有单独的闭合标签。
    • src 路径错了就什么都不显示——检查文件名和文件夹。
    Vocabulary · ⁨词汇⁩
    English中文Pinyin · ⁨拼音⁩
    unordered list/ʌˈnɔːdəd lɪst/无序列表wú xù liè biǎo
    ordered list/ˈɔːdəd lɪst/有序列表yǒu xù liè biǎo
    list item/lɪst ˈaɪtəm/列表项liè biǎo xiàng
    link/lɪŋk/链接liàn jiē
    attribute/ˈætrɪbjuːt/属性shǔ xìng
    image/ˈɪmɪdʒ/图片tú piàn
  • 3 Page structure · ⁨页面结构⁩
    3.1

    一个完整的页面

    English

    A real page has a fixed skeleton 骨架. <!DOCTYPE html> says it is HTML5. Everything sits inside <html>. The <head> holds information about the page; the <body> holds what you see.

    • The <title> shows in the browser tab.
    • <meta charset="utf-8"> lets the page show any language.
    中文

    真正的页面有固定的骨架(skeleton)。<!DOCTYPE html> 表示它是 HTML5。所有内容都放在 <html> 里面。<head> 放关于页面的信息;<body> 放你看得见的内容。

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>My Page</title>
      </head>
      <body>
        <h1>Welcome</h1>
        <p>This is a complete page.</p>
      </body>
    </html>
    
    页面是一棵树:head 描述页面,body 放你看到的内容
    页面是一棵树:head 描述页面,body 放你看到的内容
    • <title> 显示在浏览器标签页上。
    • <meta charset="utf-8"> 让页面能显示任何语言。
    3.2

    注释与嵌套

    English

    A comment 注释 <!-- ... --> is a note the browser ignores. Tags nest 嵌套 inside one another; indent 缩进 each level so the structure stays clear. Close tags in the reverse order you opened them.

    Common mistakes

    • Tags must nest, not overlap: <b><i>text</i></b>, never <b><i>text</b></i>.
    • A page needs <html>, a <head> (for the title and links) and a <body> (for what you see).
    • A comment is <!-- ... -->; the browser ignores it.
    中文

    注释(comment)<!-- ... --> 是浏览器会忽略的备注。标签彼此嵌套(nest);每一层都缩进(indent),让结构清楚。按打开的相反顺序关闭标签。

    <!-- a simple navigation bar -->
    <nav>
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
      </ul>
    </nav>
    

    常见错误

    • 标签要嵌套,不能交叉:<b><i>text</i></b>,绝不能 <b><i>text</b></i>。
    • 一个页面需要 <html>、一个 <head>(放标题和链接)和一个 <body>(放看得见的内容)。
    • 注释是 <!-- ... -->;浏览器会忽略它。
    Vocabulary · ⁨词汇⁩
    English中文Pinyin · ⁨拼音⁩
    skeleton/ˈskelɪtn/骨架gǔ jià
    comment/ˈkɒment/注释zhù shì
    nest/nest/嵌套qiàn tào
    indent/ˌɪnˈdent/缩进suō jìn
  • 4 Tables & forms · ⁨表格与表单⁩
    4.1

    表格

    English

    A table 表格 <table> is built from rows <tr>. A header cell 单元格 is <th> (bold, centred); a normal data cell is <td>.

    中文

    表格(table)<table> 由行 <tr> 构成。表头单元格(cell)是 <th>(加粗、居中);普通数据单元格是 <td>。

    <table border="1" cellpadding="6">
      <tr><th>Name</th><th>Score</th></tr>
      <tr><td>Mei</td><td>88</td></tr>
      <tr><td>Sam</td><td>71</td></tr>
    </table>
    
    table → tr rows → th/td cells
    table → tr rows → th/td cells
    4.2

    表单

    English

    A form 表单 <form> collects input from the user. An <input> is a box; a <label> names it; a <button> submits 提交 it.

    • Common <input> type values: text, number, email, password, checkbox.

    Common mistakes

    • Table cells go inside rows: a <tr> holds the <td> cells.
    • Every form input should have a matching <label> so it is clear and accessible.
    • A button inside a form submits it by default — set type="button" if you do not want that.
    中文

    表单(form)<form> 收集用户的输入。<input> 是一个输入框;<label> 给它一个名字;<button> 用来提交(submit)。

    <form>
      <label>Name: <input type="text"></label>
      <br><br>
      <label>Age: <input type="number"></label>
      <br><br>
      <button type="submit">Send</button>
    </form>
    
    • 常见的 <input> type 值:text、number、email、password、checkbox。

    常见错误

    • 表格单元格放在行里面:一个 <tr> 装若干 <td> 单元格。
    • 每个表单输入都应有对应的 <label>,这样更清楚也更易用。
    • 表单里的按钮默认会提交表单——不想这样就设 type="button"。
    Vocabulary · ⁨词汇⁩
    English中文Pinyin · ⁨拼音⁩
    table/ˈteɪbl/表格biǎo gé
    cell/sel/单元格dān yuán gé
    form/fɔːm/表单biǎo dān
    submit/səbˈmɪt/提交tí jiāo
  • 5 Boxes & sections · ⁨盒子与分区⁩
    5.1

    div、span 与语义分区

    English

    A <div> is a generic block 块级 box that groups content on its own lines. A <span> is a generic inline 行内 box inside a line of text. You use them to group things so you can style or position them.

    Semantic 语义 tags describe their role, which helps screen readers and search engines. Prefer them over plain <div> where they fit: <header>, <nav>, <main>, <section>, <article>, <footer>.

    Common mistakes

    • <div> is a block (starts a new line); <span> is inline (stays in the text).
    • Use semantic tags like <header>, <nav> and <footer> where they fit, not <div> for everything.
    • Do not use a tag just for its default look — style it with CSS instead.
    中文

    <div> 是一个通用的块级(block)盒子,把内容成块地组合起来。<span> 是一行文字里通用的行内(inline)盒子。用它们把内容分组,方便设置样式或定位。

    <div>
      <span>Price:</span> <span>$9.99</span>
    </div>
    <div>
      <span>Stock:</span> <span>in stock</span>
    </div>
    

    语义(semantic)标签描述自己的角色,有助于屏幕阅读器和搜索引擎。在合适的地方优先用它们,而不是普通的 <div>:<header>、<nav>、<main>、<section>、<article>、<footer>。

    <header><h1>My Site</h1></header>
    <main>
      <section>
        <h2>News</h2>
        <p>Today we launched the site.</p>
      </section>
    </main>
    <footer>© 2026 My Site</footer>
    

    常见错误

    • <div> 是块级(另起一行);<span> 是行内(留在文字中)。
    • 在合适处用 <header>、<nav>、<footer> 等语义标签,别什么都用 <div>。
    • 不要只为了标签的默认外观去用它——外观交给 CSS。
    div 是块级元素;span 包裹行内文本
    div 是块级元素;span 包裹行内文本
    Vocabulary · ⁨词汇⁩
    English中文Pinyin · ⁨拼音⁩
    block/blɒk/块级kuài jí
    inline/ˈɪnlaɪn/行内xíng nèi
    semantic/səˈmæntɪk/语义yǔ yì
  • 6 CSS basics · ⁨CSS 基础⁩
    6.1

    添加 CSS

    English

    CSS 层叠样式表 styles HTML. The simplest way is a <style> block in the <head> (a bigger site uses a separate .css file with <link>). A CSS rule 规则 has a selector 选择器, then { property: value; } pairs called declarations 声明.

    中文

    CSS(层叠样式表)给 HTML 设置样式。最简单的方式是在 <head> 里放一个 <style> 块(更大的站点会用单独的 .css 文件配合 <link>)。一条 CSS 规则(rule)有一个选择器(selector),后面是 { property: value; } 这样的对,叫作声明(declaration)。

    <style>
      p  { color: blue; font-size: 18px; }
      h1 { color: darkred; }
    </style>
    <h1>Styled heading</h1>
    <p>This paragraph is blue.</p>
    
    Link a stylesheet so CSS rules style HTML elements
    Link a stylesheet so CSS rules style HTML elements
    6.2

    选择器:class 与 id

    English

    A class 类 styles many elements: add class="..." in HTML and write .name in CSS. An id 标识符 styles ONE element: id="..." and #name.

    The selectors you will meet most, in one place:

    Selector Chooses
    p every <p> element
    .note everything with class="note"
    #title the one element with id="title"
    h1, h2 several selectors at once
    nav a every <a> inside a <nav>
    .btn:hover one state of an element (see topic 10)

    Common mistakes

    • A class selector starts with . and an id selector with #.
    • Link the stylesheet in the <head>, or put rules inside a <style> block.
    • A class can be reused on many elements; an id should appear once per page.
    中文

    类(class)给许多元素设置样式:在 HTML 里加 class="...",在 CSS 里写 .name。id(标识符)只给一个元素设置样式:id="..." 和 #name。

    <style>
      .note  { color: green; }
      #title { font-size: 24px; }
    </style>
    <h1 id="title">Welcome</h1>
    <p class="note">First note.</p>
    <p class="note">Second note.</p>
    

    最常见的选择器,集中在一张表里:

    选择器 选中
    p 每个 <p> 元素
    .note 所有 class="note" 的元素
    #title 那个 id="title" 的元素
    h1, h2 同时选中多个选择器
    nav a <nav> 里的每个 <a>
    .btn:hover 元素的一种状态(见主题 10)

    常见错误

    • 类选择器以 . 开头,id 选择器以 # 开头。
    • 在 <head> 里链接样式表,或把规则放进 <style> 块。
    • 一个类可以用在许多元素上;一个 id 每页只应出现一次。
    Vocabulary · ⁨词汇⁩
    English中文Pinyin · ⁨拼音⁩
    CSS/ˌsiː es ˈes/层叠样式表céng dié yàng shì biǎo
    rule/ruːl/规则guī zé
    selector/sɪˈlektə/选择器xuǎn zé qì
    declaration/ˌdekləˈreɪʃn/声明shēng míng
    class/klæs/类lèi
    id/aɪˈdiː/标识符biāo shí fú
  • 7 Colours & text · ⁨颜色与文字⁩
    7.1

    颜色与背景

    English

    Set the text colour with color and the background 背景 with background-color. A colour can be a name (red), a hex 十六进制 code (#ff0000), or rgb(255, 0, 0).

    中文

    用 color 设置文字颜色,用 background-color 设置背景(background)。颜色可以是名字(red)、十六进制(hex)代码(#ff0000),或 rgb(255, 0, 0)。

    <style>
      body { background-color: #f0f4ff; }
      .card { background-color: white; color: #1e3a8a; padding: 12px; }
    </style>
    <div class="card">A card with its own colours.</div>
    
    CSS colours: named, hex, and rgb()
    CSS colours: named, hex, and rgb()
    7.2

    文字样式

    English

    Common text properties: font-size, font-weight (e.g. bold), font-family 字体, text-align 对齐 (left / center / right), and line-height for spacing.

    Common mistakes

    • color sets the text colour; background-color sets the background.
    • A colour can be a name, a #rrggbb hex code, or rgb(...).
    • End each declaration inside the braces with a semicolon ; — it separates the property: value pairs (the one after the last declaration is optional). The rule itself ends with the closing brace }.
    中文

    常见的文字属性:font-size、font-weight(例如 bold)、font-family 字体(font)、text-align 对齐(align)(left / center / right),以及控制行距的 line-height。

    <style>
      h1 { font-family: Arial, sans-serif; text-align: center; }
      p  { font-size: 18px; line-height: 1.6; }
    </style>
    <h1>Centered title</h1>
    <p>Readable paragraph text with comfortable line spacing.</p>
    

    常见错误

    • color 设置文字颜色;background-color 设置背景色。
    • 颜色可以是名字、#rrggbb 十六进制码,或 rgb(...)。
    • 大括号里每条声明(declaration)都以分号 ; 结尾——分号用来分隔 property: value 对(最后一条声明后的分号可选)。规则本身以右大括号 } 结尾。
    Vocabulary · ⁨词汇⁩
    English中文Pinyin · ⁨拼音⁩
    background/ˈbækɡraʊnd/背景bèi jǐng
    hex/heks/十六进制shí liù jìn zhì
    font/fɒnt/字体zì tǐ
    align/əˈlaɪn/对齐duì qí
  • 8 The box model · ⁨盒模型⁩
    8.1

    盒模型

    English

    Every element is a box with four layers, from inside out: the content 内容, the padding 内边距 (space inside the border), the border 边框, and the margin 外边距 (space outside the border).

    中文

    每个元素都是一个盒子,从里到外有四层:内容(content)、内边距(padding)(边框内侧的空间)、边框(border),以及外边距(margin)(边框外侧的空间)。

    <style>
      .box { background: #dbeafe; border: 3px solid #2563eb;
             padding: 16px; margin: 20px; }
    </style>
    <div class="box">content + padding + border + margin</div>
    
    盒模型:内容,然后内边距,然后边框,然后外边距
    盒模型:内容,然后内边距,然后边框,然后外边距
    8.2

    外边距与内边距

    English

    padding adds space inside the border, pushing the content inwards. margin adds space outside, pushing other boxes away. Give one value for all sides, or four (top right bottom left).

    中文

    padding 在边框内侧加空间,把内容往里推。margin 在外侧加空间,把其它盒子推开。可以给一个值(四边相同),或四个值(上 右 下 左)。

    <style>
      .a { background: #fde68a; padding: 4px; }
      .b { background: #bbf7d0; padding: 20px; margin-top: 16px; }
    </style>
    <div class="a">tight padding</div>
    <div class="b">roomy padding</div>
    
    8.3

    display 显示

    English

    The display property sets how a box flows. block 块级 takes a whole line; inline 行内 sits within a line of text; none removes the box completely; flex lays the children in a row (next topic).

    Common mistakes

    • Padding is inside the border; margin is the space outside it.
    • box-sizing: border-box makes the set width include padding and border — often what you want.
    • display: none hides an element completely; visibility: hidden leaves its space.
    中文

    display 属性决定盒子如何排布。block(块级)占一整行;inline(行内)处在一行文字之中;none 把盒子完全移除;flex 把子元素排成一行(下一主题)。

    <style>
      .hidden { display: none; }
      .chip { display: inline; background: #e0e7ff; padding: 2px 8px; border-radius: 6px; }
    </style>
    <p>Visible text. <span class="hidden">(secret)</span> <span class="chip">a chip</span></p>
    

    常见错误

    • 内边距(padding)在边框内侧;外边距(margin)是边框外侧的空间。
    • box-sizing: border-box 让设定的宽度包含内边距和边框——通常正合你意。
    • display: none 会彻底隐藏元素;visibility: hidden 会保留它的位置。
    Vocabulary · ⁨词汇⁩
    English中文Pinyin · ⁨拼音⁩
    content/ˈkɒntent/内容nèi róng
    padding/ˈpædɪŋ/内边距nèi biān jù
    border/ˈbɔːdə/边框biān kuāng
    margin/ˈmɑːdʒɪn/外边距wài biān jù
    block/blɒk/块级kuài jí
    inline/ˈɪnlaɪn/行内xíng nèi
  • 9 Layout · ⁨布局⁩
    9.1

    宽度与居中

    English

    Set a box's width. To centre 居中 a block, give it a width and margin: 0 auto — auto shares the leftover space equally on the left and right.

    中文

    设置盒子的 width。要让一个块居中(centre),给它一个宽度,再加 margin: 0 auto——auto 会把剩余空间在左右两边平分。

    <style>
      .page { width: 300px; margin: 0 auto; background: #e0e7ff;
              padding: 16px; text-align: center; }
    </style>
    <div class="page">A centered, fixed-width page.</div>
    
    9.2

    Flexbox

    English

    Flexbox 弹性布局 lays children out in a row (or a column). Set display: flex on the parent — the container 容器. gap adds space between children; flex: 1 makes them share the width.

    中文

    Flexbox(弹性布局)把子元素排成一行(或一列)。在父元素——容器(container)——上设置 display: flex。gap 在子元素之间加空隙;flex: 1 让它们平分宽度。

    <style>
      .row { display: flex; gap: 10px; }
      .row div { background: #93c5fd; padding: 12px; flex: 1; text-align: center; }
    </style>
    <div class="row">
      <div>One</div>
      <div>Two</div>
      <div>Three</div>
    </div>
    
    弹性容器沿主轴摆放子项;gap 在它们之间加间距
    弹性容器沿主轴摆放子项;gap 在它们之间加间距
    9.3

    小项目:个人名片

    English

    Combine boxes, colours, text and spacing into one small component 组件.

    Common mistakes

    • Centre a block with a set width using margin: 0 auto.
    • Flexbox needs display: flex on the CONTAINER, not on the items.
    • A width in % is relative to the parent, so check the parent's width too.
    中文

    把盒子、颜色、文字和间距组合成一个小组件(component)。

    <style>
      .card { width: 240px; margin: 0 auto; border: 1px solid #cbd5e1;
              border-radius: 12px; padding: 16px; font-family: sans-serif; }
      .card h2 { margin: 0; color: #1e3a8a; }
      .card .role { color: #64748b; margin: 4px 0 8px; }
    </style>
    <div class="card">
      <h2>Mei Chen</h2>
      <p class="role">Student · Grade 11</p>
      <p>Loves maths and building web pages.</p>
    </div>
    

    常见错误

    • 给设定了宽度的块用 margin: 0 auto 居中。
    • Flexbox 要在容器上写 display: flex,不是在子项上。
    • 以 % 为单位的宽度是相对父元素的,所以也要看父元素的宽度。
    Vocabulary · ⁨词汇⁩
    English中文Pinyin · ⁨拼音⁩
    centre/ˈsentə/居中jū zhōng
    Flexbox/ˈfleksbɒks/弹性布局tán xìng bù jú
    container/kənˈteɪnə/容器róng qì
    component/kəmˈpəʊnənt/组件zǔ jiàn
  • 10 Going further · ⁨更进一步⁩
    10.1

    :hover 与伪类

    English

    A pseudo-class 伪类 styles an element in one particular state. :hover applies while the mouse is over the element. Add transition 过渡 so the change happens smoothly instead of jumping.

    • Other useful pseudo-classes: :focus (an input the user clicked into) and :first-child.
    中文

    伪类(pseudo-class)给处于某种状态的元素设置样式。:hover 在鼠标悬停在元素上时生效。加上 transition(过渡),变化就会平滑发生,而不是突然跳变。

    <style>
      .btn { background: #2563eb; color: white; padding: 10px 18px;
             border: none; border-radius: 8px; font-size: 16px;
             transition: background 0.3s; }
      .btn:hover { background: #16a34a; }
    </style>
    <button class="btn">Hover me</button>
    
    • 其他常用伪类::focus(用户点进去的输入框)和 :first-child。
    :hover 在指针悬停时样式化元素
    :hover 在指针悬停时样式化元素
    10.2

    position 定位

    English

    Positioning 定位 moves a box away from its normal place. relative nudges the box (and makes it the anchor 锚点 for its children); absolute places a box exactly, measured from the nearest positioned parent; fixed pins it to the screen even when the page scrolls.

    • top, right, bottom, left say how far to move — they only work on positioned boxes.
    中文

    定位(positioning)让盒子离开原来的位置。relative 轻微移动盒子(并让它成为子元素的锚点(anchor));absolute 从最近的已定位父元素量起,把盒子精确摆放;fixed 把盒子固定在屏幕上,页面滚动也不动。

    <style>
      .card { position: relative; width: 260px; height: 90px;
              background: #dbeafe; border-radius: 10px; padding: 12px; }
      .badge { position: absolute; top: -10px; right: -10px;
               background: #dc2626; color: white; padding: 4px 10px;
               border-radius: 999px; font-size: 13px; }
    </style>
    <div class="card">
      A product card
      <span class="badge">NEW</span>
    </div>
    
    • top、right、bottom、left 指定移动多远——它们只对已定位的盒子生效。
    10.3

    媒体查询与响应式

    English

    A responsive 响应式 page adapts to the screen size. A media query 媒体查询 applies its rules only while a condition about the screen holds, such as max-width.

    • Design mobile-first 移动优先: write the phone styles first, then add @media (min-width: ...) rules for bigger screens.
    • Percent widths and max-width let boxes shrink with the screen.

    Common mistakes

    • :hover sticks to its selector with no space: .btn:hover, not .btn :hover.
    • position: absolute is measured from the nearest positioned parent — give that parent position: relative.
    • A media query wraps whole rules: @media (max-width: 600px) { .a { ... } }.
    中文

    响应式(responsive)页面会适应屏幕大小。媒体查询(media query)只在屏幕满足某个条件时才应用它的规则,例如 max-width。

    <style>
      .banner { background: #2563eb; color: white; padding: 16px; }
      @media (max-width: 600px) {
        .banner { background: #16a34a; }   /* narrow screens turn green */
      }
    </style>
    <div class="banner">Resize the window: blue when wide, green when narrow.</div>
    
    • 移动优先(mobile-first)的做法:先写手机样式,再用 @media (min-width: ...) 补充大屏样式。
    • 百分比宽度和 max-width 让盒子能跟着屏幕缩小。

    常见错误

    • :hover 紧贴选择器,中间没有空格:是 .btn:hover,不是 .btn :hover。
    • position: absolute 从最近的已定位父元素量起——给那个父元素加 position: relative。
    • 媒体查询要包住完整的规则:@media (max-width: 600px) { .a { ... } }。
    Vocabulary · ⁨词汇⁩
    English中文Pinyin · ⁨拼音⁩
    pseudo-class/ˈsuːdəʊ klæs/伪类wěi lèi
    transition/trænˈsɪʃn/过渡guò dù
    positioning/pəˈzɪʃənɪŋ/定位dìng wèi
    anchor/ˈæŋkə/锚点máo diǎn
    responsive/rɪˈspɒnsɪv/响应式xiǎng yìng shì
    media query/ˈmiːdiːə ˈkwɪərɪ/媒体查询méi tǐ chá xún
    mobile-first/ˈməʊbaɪl fɜːst/移动优先yí dòng yōu xiān

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩