Blogger Information
Blog 10
fans 0
comment 1
visits 3173
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
上下文选择器及状态伪类实现一个下拉菜单功能
P粉753609736
Original
413 people have browsed it

一、上下文选择器

根据元素位置与层级匹配元素

1.父子 >大于号
2.后代 空格
3.兄弟 +加号
4.同级 ~波浪线

实战演示

1.父子>

  1. <ul class="list">
  2. <li class="item">item1</li>
  3. <li class="item">item2</li>
  4. <li class="item">item3</li>
  5. </ul>
  1. .list > .item {
  2. border: thin solid;
  3. }

2.后代空格

  1. <ul>
  2. <li class="item">item1</li>
  3. <li class="item">item2</li>
  4. <li class="item">item3
  5. <ul>
  6. <li class="item">item1</li>
  7. <li class="item">item2</li>
  8. <li class="item">item3</li>
  9. </ul>
  10. </li>
  11. </ul>
  1. .list .item {
  2. border: thin solid;
  3. }

3.兄弟 +加号

  1. <ul class="list">
  2. <li class="item">item1</li>
  3. <li class="item start">item2</li>
  4. <li class="item">item3</li>
  5. </ul>
  1. .list > .item.start + * {
  2. background-color: bule;
  3. }

4.同级 ~波浪线

  1. <ul class="list">
  2. <li class="item">item1</li>
  3. <li class="item start">item2</li>
  4. <li class="item">item3</li>
  5. </ul>
  1. .list > .item.start ~ * {
  2. background-color: green;
  3. }

二、状态伪类实现一个下拉菜单功能

  1. <div class="box">
  2. <label for="menu">下拉演示</label>
  3. <input type="checkbox" name="xxx" id="menu" />
  4. <ul>
  5. <li><a href="">item1</a></li>
  6. <li><a href="">item2</a></li>
  7. <li><a href="">item3</a></li>
  8. </ul>
  9. </div>
  1. #menu {
  2. display:none;
  3. }
  4. #menu + ul {
  5. display: none;
  6. }
  7. #menu:checked + ul {
  8. display:block;
  9. }
Correcting teacher:PHPzPHPz

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