博主信息
博文 28
粉丝 0
评论 1
访问量 27894
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
简单选择器,上下文选择器 结构伪类选择器 :target, :not, :before, :after, :focus
南宫
原创
751人浏览过

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. }
批改老师:WJWJ

批改状态:合格

老师批语:写的很认真仔细,建议重要代码加上效果图会更好!
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学