search
HomeWeb Front-endHTML Tutorial《图解CSS3核心技术与案例实战》读书笔记--CSS3基本选择器_html/css_WEB-ITnose

1、认识CSS选择器

要使某个样式应用于特定的HTML元素,首先需要找到元素。在CSS中,执行这一任务的表现规则称为CSS选择器

1、1 CSS3选择器的优势

CSS3新增了属性选择器,伪类选择器、过滤选择器。可以帮助您在开发中减少对HTML的类名或ID名的依赖,以及对HTML元素的结构依赖,使编写代码更加简单轻松。

1、2 CSS3选择器分类

  • 基本选择器
  • 层次选择器
  • 伪类选择器
  • 伪元素
  • 属性选择器

    伪类选择器又分为六种:动态伪类选择器、目标伪类选择器、语言伪类选择器、UI元素状态伪类选择器、结构伪类选择器、否定伪类选择器。

2、基本选择器

2、1基本选择器的语法

选择器 类型 功能描述
* 通配选择器 选择文档中所有的HTML元素
E 元素选择器 选择指定的类型的HTML元素
#id ID选择器 选择指定ID属性值为“id”的任意类型元素
.class 类选择器 选择指定class属性值为“class”的任意类型的任意多元素
selector1,selectorN 群组选择器 将每一个选择器匹配的元素合并

实战体验

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>使用CSS3基本选择器</title>    <style type="text/css">        *{margin:0;  padding: 0;}              .clearfix:after,.clearfix:before{display: table;content:""}               .clearfix:after{clear: both;overflow: hidden}              .demo{width: 250px;border: 1px solid #cccccc;padding: 10px;margin: 20px auto;}        li{list-style: none outside none;  float: left;  height: 20px;         line-height: 20px;width: 20px;border-radius: 10px;         text-align: center;background: #f36;  color: white; margin-right: 5px;}       </style></head><body>  <ul class="clearfix demo">     <li class="first" id="first">1</li>    <li class="active">2</li>       <li class="important item">2</li>       <li class="important">4</li>       <li class="item">5</li>      <li>6</li>      <li>7</li>      <li>8</li>     <li>9</li>     <li class="last" id="last">10</li> </ul></body></html>

页面初始效果

2、2通配选择器

通配选择器(*)用来选择所有元素

*{margin:0;padding:0}  //此代码在Reset的样式文件中经常见到,表示所有元素的margin和padding都设置为0

代码示例

  *{margin:0;  padding: 0;}              .clearfix:after,.clearfix:before{display: table;content:""}               .clearfix:after{clear: both;overflow: hidden}              .demo{width: 250px;border: 1px solid #cccccc;padding: 10px;margin: 20px auto;}        li{list-style: none outside none;  float: left;  height: 20px;         line-height: 20px;width: 20px;border-radius: 10px;         text-align: center;background: #f36;  color: white; margin-right: 5px;}  .demo * {background:orange} //使元素类名为demo下的所有元素都将背景色设置为橙色

通配选择器使用效果

2、3 元素选择器

元素选择器是CSS中最基本的选择器,接下来通过ul选择器改变整个列表的背景色

  *{margin:0;  padding: 0;}              .clearfix:after,.clearfix:before{display: table;content:""}               .clearfix:after{clear: both;overflow: hidden}              .demo{width: 250px;border: 1px solid #cccccc;padding: 10px;margin: 20px auto;}        li{list-style: none outside none;  float: left;  height: 20px;         line-height: 20px;width: 20px;border-radius: 10px;         text-align: center;background: #f36;  color: white; margin-right: 5px;}  .demo * {background:orange}ul{background:grey}//列表ul的背景色将变成灰色

元素选择器使用效果

2、4 ID选择器

在使用ID选择器(#id)之前,需要在HTML文档中给对应的元素设置id属性并设置其值,才能找到对应的元素。ID选择器具有唯一性。

 *{margin:0;  padding: 0;}              .clearfix:after,.clearfix:before{display: table;content:""}               .clearfix:after{clear: both;overflow: hidden}              .demo{width: 250px;border: 1px solid #cccccc;padding: 10px;margin: 20px auto;}        li{list-style: none outside none;  float: left;  height: 20px;         line-height: 20px;width: 20px;border-radius: 10px;         text-align: center;background: #f36;  color: white; margin-right: 5px;}  .demo * {background:orange}ul{background:grey}#first {background:lime;color:#000}#last {background:#000;color:lime}

ID选择器使用效果

2、5 类选择器

类选择器(.class)是以独立于文档元素的方式来指定元素样式。于ID选择器最大的不同之处是:类选择器在一个页面中可以有多个相同的类名,而ID选择器其ID值在整个页面中是唯一的一个

 *{margin:0;  padding: 0;}              .clearfix:after,.clearfix:before{display: table;content:""}               .clearfix:after{clear: both;overflow: hidden}              .demo{width: 250px;border: 1px solid #cccccc;padding: 10px;margin: 20px auto;}        li{list-style: none outside none;  float: left;  height: 20px;         line-height: 20px;width: 20px;border-radius: 10px;         text-align: center;background: #f36;  color: white; margin-right: 5px;}  .demo * {background:orange}ul{background:grey}#first {background:lime;color:#000}#last {background:#000;color:lime}.item {background:green;color:#fff;font-weight:bold}//设置背景为绿色,并且加粗文字

类选择器使用效果

若在代码后面再添加一行

.item.important{background:red;}//列表3同时具有important和item类名,所以才会执行代码

多类名选择器使用效果

2、6群组选择器

群组选择器(selector1,selectorN)是将具有相同样式的元素分组在一起,每个选择器之间用(,)隔开。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the root tag in an HTML document?What is the root tag in an HTML document?Apr 29, 2025 am 12:10 AM

TheroottaginanHTMLdocumentis.Itservesasthetop-levelelementthatencapsulatesallothercontent,ensuringproperdocumentstructureandbrowserparsing.

Are the HTML tags and elements the same thing?Are the HTML tags and elements the same thing?Apr 28, 2025 pm 05:44 PM

The article explains that HTML tags are syntax markers used to define elements, while elements are complete units including tags and content. They work together to structure webpages.Character count: 159

What is the significance of <head> and <body> tag in HTML?What is the significance of <head> and <body> tag in HTML?Apr 28, 2025 pm 05:43 PM

The article discusses the roles of <head> and <body> tags in HTML, their impact on user experience, and SEO implications. Proper structuring enhances website functionality and search engine optimization.

What is the difference between <strong>, <b> tags and <em>, <i> tags?What is the difference between <strong>, <b> tags and <em>, <i> tags?Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

Please explain how to indicate the character set being used by a document in HTML?Please explain how to indicate the character set being used by a document in HTML?Apr 28, 2025 pm 05:41 PM

Article discusses specifying character encoding in HTML, focusing on UTF-8. Main issue: ensuring correct display of text, preventing garbled characters, and enhancing SEO and accessibility.

What are the various formatting tags in HTML?What are the various formatting tags in HTML?Apr 28, 2025 pm 05:39 PM

The article discusses various HTML formatting tags used for structuring and styling web content, emphasizing their effects on text appearance and the importance of semantic tags for accessibility and SEO.

What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?Apr 28, 2025 pm 05:39 PM

The article discusses the differences between HTML's 'id' and 'class' attributes, focusing on their uniqueness, purpose, CSS syntax, and specificity. It explains how their use impacts webpage styling and functionality, and provides best practices for

What is the 'class' attribute in HTML?What is the 'class' attribute in HTML?Apr 28, 2025 pm 05:37 PM

The article explains the HTML 'class' attribute's role in grouping elements for styling and JavaScript manipulation, contrasting it with the unique 'id' attribute.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),