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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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,

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

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

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

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

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

This article explains the HTML5 <time> 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

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