Coursera 笔记

Module 02 · CSS Overview & HTML5 ElementsCSS 概述与 HTML5 元素

Understanding CSS Selectors - Tags, IDs, and ClassesCSS 选择器:标签、ID 与类

Coursera ↗

Estimated time: 15 minutes
预计时间:15 分钟

About this Reading 关于这篇阅读

In this reading, we will delve into three key types of CSS selectors: Tags, IDs, and Classes. These selectors provide a powerful way to target specific HTML elements and apply styles accordingly.
在本阅读中,我们将深入探讨三种关键的 CSS 选择器类型:标签、ID 和类。这些选择器提供了一种强大的方式来定位特定的 HTML 元素并相应地应用样式。

Cascading Style Sheets (CSS) play a crucial role in styling web pages and understanding the role of CSS selectors for controling how styles are applied.
层叠样式表(CSS)在网页样式化中扮演着至关重要的角色,理解 CSS 选择器的角色对于控制样式应用至关重要。

CSS Selectors Overview CSS 选择器概述

What are CSS Selectors?  CSS 选择器是什么?

  • CSS selectors are patterns used to select and style HTML elements.
    CSS 选择器是用于选择和设置 HTML 元素样式的模式。
  • They define the relationship between HTML elements and the styles applied to them.
    他们定义了 HTML 元素及其应用样式之间的关系。

Why are Selectors Important?
选择器为什么重要?

  • Selectors allow precise targeting of specific elements.
    选择器允许精确地定位特定元素。
  • They enable the creation of consistent and visually appealing designs.
    它们能够实现一致且视觉上吸引人的设计。

Tag Selectors 标签选择器

Tag selectors target HTML elements based on their tag names. They apply styles to all instances of a particular tag.
标签选择器根据 HTML 元素的标签名称来定位元素。它们将样式应用于特定标签的所有实例。

Syntax for Tag Selector:  标签选择器的语法:

tagName {
    /* Styles for the elements with the specified tag name */
}

Replace tagName with the actual HTML tag name you want to style.
将 tagName 替换为您想要样式的实际 HTML 标签名称。

Example: Styling Headings
示例:标题样式

h1 { 
    /* Styles for h1 elements */ 
    color: #3366cc; 
    font-size: 24px; 
}
  • This CSS rule targets all <h1> elements.
    此 CSS 规则针对所有 <h1> 元素。
  • Applies blue color and a font size of 24 pixels to all <h1> headings.
    将蓝色和 24 像素字体大小应用于所有 <h1> 标题。

ID Selectors ID 选择器

ID selectors target a specific HTML element using its unique ID attribute. The ID attribute is assigned to an HTML element to provide a unique identifier for that element within the document. ID selectors are denoted by a hash symbol (#) followed by the ID.
ID 选择器通过 HTML 元素的唯一 ID 属性来定位特定的 HTML 元素。ID 属性被分配给 HTML 元素,以在文档中为该元素提供唯一的标识符。ID 选择器以井号(#)开头,后跟 ID。

Syntax for ID Selector:  ID 选择器语法:

#yourID {
    /* Styles for the element with the specified ID */
}

Replace yourID with the actual ID assigned to the HTML element.
将 yourID 替换为分配给 HTML 元素的实际 ID。

Example: Styling a Unique Element
示例:独特元素的样式

#header {
    /* Styles for the element with id="header" */ 
    background-color: #f0f0f0; 
    padding: 10px; 
}
  • This CSS rule targets an element with the ID header
  • Applies a light gray background and 10 pixels of padding to the element.
    应用浅灰色背景和 10 像素的内边距到该元素。

Class Selectors 类选择器

Class selectors target HTML elements with a specific class attribute. Multiple elements can share the same class. Class selectors are denoted by a period (.) followed by the class name.
类选择器针对具有特定类属性的 HTML 元素。多个元素可以共享同一个类。类选择器由一个点(.)后跟类名表示。

Syntax for Class Selector:
类选择器语法:

.className {
    /* Styles for the elements with the specified class */
}

Replace className with the actual class name you want to style.
将 className 替换为您想要样式的实际类名。

Example: Styling Multiple Elements
示例:样式化多个元素

.highlight { 
    /* Styles for elements with class="highlight" */
    background-color: #ffd700; 
    color: #333; 
}
  • This CSS rule targets all elements with the class highlight
  • Applies a yellow background and dark text color to all elements with the class highlight
    应用黄色背景和深色文字颜色到所有具有类 highlight 的元素

Summary 摘要

In this reading, you've gained essential insights into CSS selectors, understanding their purpose and importance in web development. The reading emphasized key concepts such as Tags, IDs, and Classes, offering powerful tools to precisely style HTML elements. Tags enable styling entire groups, IDs target unique elements, and Classes allow shared styling patterns. This knowledge equips web developers with a robust toolkit, enhancing flexibility and control over a website's visual presentation.
在本阅读中,您已获得对 CSS 选择器的关键见解,理解其在网页开发中的目的和重要性。阅读强调了如标签、ID 和类等关键概念,提供了精确样式化 HTML 元素的有力工具。标签可对整个组进行样式化,ID 针对独特元素,类允许共享的样式模式。这些知识为网页开发者提供了强大的工具集,增强了网站视觉呈现的灵活性和控制力。

Congratulations! You have completed this reading and are ready for the next topic. 恭喜!您已完成本阅读,准备好学习下一个主题。

Author 作者

Rajashree Patil 拉贾什里·帕蒂尔

Source: https://author-ide.skills.network/render?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZF9pbnN0cnVjdGlvbnNfdXJsIjoiaHR0cHM6Ly9jZi1jb3Vyc2VzLWRhdGEuczMudXMuY2xvdWQtb2JqZWN0LXN0b3JhZ2UuYXBwZG9tYWluLmNsb3VkL0lCTURldmVsb3BlclNraWxsc05ldHdvcmstQ0QwMTAxRU4tU2tpbGxzTmV0d29yay9sYWJzL1JlYWRpbmdzL01vZHVsZV8zL1VuZGVyc3RhbmRpbmdfQ1NTX1NlbGVjdG9ycy5tZCIsInRvb2xfdHlwZSI6Imluc3RydWN0aW9uYWwtbGFiIiwiYXRsYXNfZmlsZV9pZCI6MjU0MzgsImFkbWluIjpmYWxzZSwiaWF0IjoxNzMwMTUzNDIxfQ.mimxAKgu3eJ1spFFg8a6XCEduHpEnshF00YxdIGC1rI