Table of Contents
太阳是最舒服的" >早晨九点的太阳是最舒服的
Home Web Front-end HTML Tutorial html进阶css(2)_html/css_WEB-ITnose

html进阶css(2)_html/css_WEB-ITnose

Jun 24, 2016 am 11:34 AM

选择器的类型

<!doctype html><html><head>           <meta charset="utf-8">           <title>css的选择器</title>           <style type="text/css">            .stress{color:red;}/*类前面要加入一个英文圆点*/<br />            #setgreen{color:green}           </style></head><body>           <p>什么是<span class="stress">类选择器</span></p><br />           <p><span id="setgreen">id选择器</span>又是什么呢?</p></body></html>
Copy after login

 类选择器在css样式编码中是最常用到的,如上图可以实现“类选择器”字体设置为红色。

.类选择器名称{css样式代码;} 英文圆点开头,其中类选择器名称可以任意起名不要用中文数字。

许多方面id选择器都类似域类选择符,但也有一些重要的区别:

1)为标签设置id="id名称",而不是class=“类名称”

2)id选择器的前面是井(#)号,而不是英文圆点(.)

类和id选择器的区别:相同点 :可以应用于任何元素

不同点:id选择器只能在文档中使用一次,可以拥有多个id但是id名称不能有冲突。 类选择器可以使用很多次。

可以使用类选择器词列表方法为一个元素设置多个样式。但只能用类选择器的方法实现。id选择器是不可以的。

<!doctype html><html><head>           <meta charset="utf-8">           <title>子选择器..</title>           <style type="text/css">           .food>li{border:1px solid red;}<br />           .first span{color:red;}<br />           *{font-size:20px;}           </style></head><body>         <p class="first">子选择器与<span>包含选择器</span></p>           <ul class=" food" >                  <li>水果                        <ul>                               <li>苹果</li>                               <li>香蕉</li>                               <li>橘子</li>                        </ul>                  </li>                  <li>                       <ul>蔬菜                              <li>白菜</li>                              <li>青菜</li>                              <li>空心菜</li>                       </ul>                  </li>           </ul></body></htmL> 
Copy after login

如上,子选择器是用(>)大于符号,用于选择指定标签元素的第一代子元素。

.food>li{border:1px solid red;}<br />这行代码会使用class为food下的子元素li加入红色实线边框。<br />包含选择器,即加入空格,用于选择指定标签元素下的后辈元素,.first  span{color:red;}<br />这行代码中“包含选择器”字体颜色变为红色。<br />子选择器是指他的直接后代,或者可以理解为作用于子元素的第一代后代,而包含选择器是用于所有子后代。包含选择器通过空格来进行选择,而子选择器通过>进行选择。<br />所以,>用于元素的第一代后代。空格用作于元素的所有后代。想试试效果的话把“.food>li”修改为“.food li”看看下效果把。<br /><br />*{font-size:20px;} 通用选择器。通用选择器是功能最强大的选择器,它使用一个(*)号指定,它的作用是匹配HTML中所有标签元素。
Copy after login
Copy after login

<!doctype html><html><head>           <meta charset="utf-8">           <title>伪类选择器</title>           <style type="text/css">            a:hover{                     color:red;                     font-size:20px;                        }            a#spe_a:hover{color:black;}           .first,#second span{color:green;}<br /></style><br /></head><br /><body>          <br /><br />            <h1 id="早晨九点的-a-href-链接地址-太阳-a-是最舒服的">早晨九点的<a  href="链接地址">太阳</a>是最舒服的</h1>            <p id="second">早晨八点是<span>上班上学</span>的<a id="spe_a" href="链接的地址">高峰期</p><br /></body></html><br /><br />
Copy after login

什么是伪类选择器,它允许给html不存在的标签(标签的某种样式)设置样式,比如说我们给html中一个标签元素的鼠标滑过的状态来设置字体颜色与大小。<br />a:hover{color:red;font-size:20px}<br />上面一行代码就是a标签鼠标滑过的状态设置字体颜色变红,字体变大。但是这样是包含所有的a标签,如想让另一段不采用这样色彩可以改变它的。
Copy after login
<a id="spe_a" href="链接的地址"></a>    a#spe_a:hover{color:black;}<br />这样就可以了,加id标签,在用a#加id地址在加上伪类选择器就ok了。
Copy after login
分组选择符<br />
Copy after login
.first,#second span{color:green;}
Copy after login
当你想为html中多个标签元素设置同一个样式时,可以使用分组选择器(,)上面代码表示.first标签第一段文字在用(,)连接”#second“在用空格连接span。这样第一段文字与id选择器中的span都是自己想要的文字效果了。
Copy after login

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

See all articles