Blogger Information
Blog 28
fans 0
comment 1
visits 21327
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
简单选择器,上下文选择器 结构伪类选择器 :target, :not, :before, :after, :focus
南宫
Original
612 people have browsed it

1.简单选择器

1.类选择器:对应html标签中的class属性

  1. <a class="item">点击</a>
  1. .item{
  2. border: 1px solid #000;
  3. }

2.多个类复合应用

  1. <div class="item">1</div>
  2. <div class="center">222</div>
  1. .item.center{
  2. background-color:lightgreen;
  3. }

3.id选择器

  1. <div id="first">测试</div>
  1. #first{
  2. background-color:lightpink;
  3. }

层叠样式表, 相同元素,后面追加的样式会覆盖前面的样式

  1. #first{
  2. background-color:red;
  3. }
  4. /* 后面再次添加css样式会覆盖前面的 */
  5. #first{
  6. background-color:yellow;
  7. }

属性元素级别

元素 < class < id

id选择器使用越来越少了,目前应用在两个场景:1.表单元素 2.锚点

1.上下文选择器

1.后代选择器: 空格

  1. <div class="container">
  2. <div>
  3. 111122
  4. <div>
  5. 44444
  6. </div>
  7. </div>
  8. <div>
  9. <div>
  10. 33333
  11. </div>
  1. .container div{
  2. /* 给container下面所有div标签 添加了样式*/
  3. border: 1px solid #000;
  4. }

2.父子选择器: >

  1. <div class="container">
  2. <div>
  3. 111122
  4. <div>
  5. 44444
  6. </div>
  7. </div>
  8. <div>
  9. <div>
  10. 33333
  11. </div>
  1. .container > div{
  2. /* 只给container下面子div标签 添加了样式 44444不会添加样式*/
  3. border: 1px solid #000;
  4. }

3.同级相邻选择器

  1. <div class="container">
  2. <div class="item">1</div>
  3. <div class="item">2</div>
  4. <div class="item">3</div>
  5. <div class="item">4</div>
  6. <div class="item center">5</div>
  7. <div class="item">6</div>
  8. <div class="item">7</div>
  9. <div class="item">8</div>
  10. <div class="item">9</div>
  11. <div>
  1. /* 给6添加样式 */
  2. .item.center + .item{
  3. background-color:red;
  4. }

4.同级所有选择器

  1. <div class="container">
  2. <div class="item">1</div>
  3. <div class="item">2</div>
  4. <div class="item">3</div>
  5. <div class="item">4</div>
  6. <div class="item center">5</div>
  7. <div class="item">6</div>
  8. <div class="item">7</div>
  9. <div class="item">8</div>
  10. <div class="item">9</div>
  11. <div>
  1. /* 选择 5 以后标签 */
  2. .item.center ~ .item{
  3. background-color:lightsalmon;
  4. }

3.结构伪类选择器

1. 匹配第一个子元素

  1. <div class="container">
  2. <div class="item">1</div>
  3. <div class="item">2</div>
  4. <div class="item">3</div>
  5. <div class="item">4</div>
  6. <div class="item center">5</div>
  7. <div class="item">6</div>
  8. <div class="item">7</div>
  9. <div class="item">8</div>
  10. <div class="item">9</div>
  11. <div>
  1. /* 选择 1 标签 */
  2. .container > :first-child{
  3. background-color:lightsalmon;
  4. }

2.最后一个子元素

  1. <div class="container">
  2. <div class="item">1</div>
  3. <div class="item">2</div>
  4. <div class="item">3</div>
  5. <div class="item">4</div>
  6. <div class="item center">5</div>
  7. <div class="item">6</div>
  8. <div class="item">7</div>
  9. <div class="item">8</div>
  10. <div class="item">9</div>
  11. <div>
  1. /* 选择 最后一个 标签 */
  2. .container > :last-child{
  3. background-color:lightsalmon;
  4. }

3.选择第几个元素:注意索引是从1开始

  1. /* 选择 选择第3个 标签 */
  2. .container > :nth-child(3){
  3. background-color:lightsalmon;
  4. }

4.只选择偶数单元格 : even

  1. .container > :nth-child(even){
  2. background-color:lightsalmon;
  3. }

5.只选择奇数单元格: odd

  1. .container > :nth-child(odd){
  2. background-color:lightsalmon;
  3. }

6.从某一标签开始后剩下所有标签

  1. /* 从4开始以后标签 */
  2. .container > :nth-child(n + 4){
  3. background-color:lightsalmon;
  4. }

7. 只取前三个

  1. .container > :nth-child(-n + 3){
  2. background-color:lightsalmon;
  3. }
  4. /* -0 + 3 = 3
  5. -1 + 3 = 2
  6. -2 + 3 = 1 */

8.只取最后三个

  1. .container > :nth-last-child(-n + 3){
  2. background-color:lightsalmon;
  3. }

取倒数使用案例

  1. /* 获取倒数第二个 */
  2. .container > :nth-last-child(2){
  3. background-color:lightsalmon;
  4. }

9.分组结构伪类

选择步骤:1.元素按类型进行分组 2.在分组中根据索引进行选择

  1. /* 获取同类div标签中最后一个 */
  2. .container > div:last-of-type{
  3. background-color:lightsalmon;
  4. }
  5. /* 在分组中匹配任何一个 */
  6. .container div:nth-of-type(2){
  7. background-color:lightsalmon;
  8. }
  9. /* 前三个 */
  10. .container div:nth-of-type(-n + 3){
  11. background-color:lightsalmon;
  12. }
  13. /* 最后2个 */
  14. .container div:nth-last-of-type(-n + 2){
  15. background-color:lightsalmon;
  16. }

10. :target :focus ::selection :not() ::before ::after

  1. <button onclick="location='#login-form'">点击登录</button>
  2. <form action="" method="post" id="login-form">
  3. <label for="email">邮箱:</label>
  4. <input type="email" name="email" id="email" />
  5. <label for="password">密码:</label>
  6. <input type="password" name="password" id="password" />
  7. <button>登录</button>
  8. </form>
  1. #login-form{
  2. display:none;
  3. }
  4. /* :target: 与id一起使用,实现锚点操作 */
  5. /* 当前#login-form的表单元素被button的锚点激活的时候执行 */
  6. #login-form:target{
  7. display:block;
  8. }
  9. /* :focus: 获取焦点时候修改样式 */
  10. input:focus{
  11. background-color:yellow;
  12. }
  13. /* ::selection: 设置选中文本的前景色与背景色 */
  14. input::selection{
  15. color:white;
  16. background-color:red;
  17. }
  1. <ul class="list">
  2. <li>item1</li>
  3. <li>item2</li>
  4. <li>item3</li>
  5. <li>item4</li>
  6. </ul>
  1. /* :not(): 排除某些元素后标签 */
  2. .list > :not(:nth-of-type(3)){
  3. color:red;
  4. }
  5. /* ::before 在某个标签之前添加元素*/
  6. .list::before{
  7. content:"购物车";
  8. color:blue;
  9. font-size:1.5rem;
  10. border-bottom:1px solid #000;
  11. }
  12. /* ::after 在某个标签之后添加元素*/
  13. .list:after{
  14. content:"结算中...";
  15. color:red;
  16. font-size:1.1rem;
  17. }
Correcting teacher:WJWJ

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