Blogger Information
Blog 6
fans 0
comment 0
visits 5951
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
关于css引入方式和选择器的实例代码
好好学习
Original
563 people have browsed it

一、实例演示css规则的三种引入到html文档中的方式

1、内部样式(仅对当前文档的元素有效,使用 style 标签,写在head头部)

3、行内样式: 仅适用于当前的页面中的指定的元素,使用style属性


二、实例演示标签选择器,class选择器,id选择器,上下文选择器, 结构伪类选择器,重点是结构伪类选择器

1、标签选择器

2、class选择器

3、id选择器(选择一个,浏览器不检查ID选择器的唯一性,须由开发者自行保证)

4、上下文选择器(空格:所有层级;>:父子级;+:相邻的兄弟;~:所有的相邻兄弟)

  • 后代选择器(所有层级)

  • 子元素选择器(仅父子层级)

  • 同级相邻选择器(仅选中与之相邻的第一个兄弟元素)

  • 同级所有选择器(选中与其相邻后面所有的元素)

5、结构伪类选择器

  • 匹配任意位置的元素( :nth-of-type(an+b),an:起点,b:偏移量,n的取值从0开始):nth-of-type()针对具有一组兄弟节点的标签, 用 n 来筛选出在一组兄弟节点的位置。

    示例:

  1. <ul>
  2. <li>1</li>
  3. <li>2</li>
  4. <li>3</li>
  5. <li>4</li>
  6. <li>5</li>
  7. <li>6</li>
  8. <li>7</li>
  9. <li>8</li>
  10. <li>9</li>
  11. <li>10</li>
  12. </ul>
  • 选择第四个元素
  1. ul li:nth-of-type(4){
  2. background-color: red;
  3. }

  • 选择所有元素
  1. ul li:nth-of-type(1n){
  2. background-color: red;
  3. }

  • 选择第四个元素及后面的元素
  1. ul li:nth-of-type(n+4){
  2. background-color: red;
  3. }

  • 选择所有索引为偶数的元素
  1. ul li:nth-of-type(2n){
  2. background-color: red;
  3. }

或使用关键字(even)

  1. ul li:nth-of-type(even){
  2. background-color: red;
  3. }
  • 选择所有索引为奇数的元素
  1. ul li:nth-of-type(2n+1){
  2. background-color: red;
  3. }

或使用关键字(odd)

  1. ul li:nth-of-type(odd){
  2. background-color: red;
  3. }
  • 选择第一个元素(语法糖)
  1. ul li:first-of-type{
  2. background-color: red;
  3. }
  • 选择最后一个元素(语法糖)
  1. ul li:last-of-type{
  2. background-color: red;
  3. }

  • 选择元素最后面的三个元素(反向::nth-last-of-type()
  1. ul li:nth-last-of-type(-n+3){
  2. background-color: red;
  3. }

  • 选择倒数第三个元素
  1. ul li:nth-last-of-type(3){
  2. background-color: red;
  3. }
  • 匹配父元素中唯一的子元素:only-of-type
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!