zoukankan      html  css  js  c++  java
  • 【CSS3】---结构性伪类选择器-first-child+last-child

    结构性伪类选择器—first-child

    “:first-child”选择器表示的是选择父元素的第一个子元素的元素E。简单点理解就是选择元素中的第一个子元素,记住是子元素,而不是后代元素。

    示例演示

    通过“:first-child”选择器定位列表中的第一个列表项,并将序列号颜色变为红色。

    HTML代码:

    <ol>
      <li><a href="##">Link1</a></li>
      <li><a href="##">Link2</a></li>
      <li><a href="##">link3</a></li>
    </ol>

    CSS代码:

    ol > li{
      font-size:20px;
      font-weight: bold;
      margin-bottom: 10px;
    }
    
    ol a {
      font-size: 16px;
      font-weight: normal;
    }
    
    ol > li:first-child{
      color: red;
    }

    演示结果:

    结构性伪类选择器—last-child

    “:last-child”选择器与“:first-child”选择器作用类似,不同的是“:last-child”选择器选择的是元素的最后一个子元素。例如,需要改变的是列表中的最后一个“li”的背景色,就可以使用这个选择器,

    ul>li:last-child{background:blue;}

    示例演示

    在博客的排版中,每个段落都有15px的margin-bottom,假设不想让博客“post”中最后一个段落不需要底部的margin值,可以使用“:last-child”选择器。

    HTML代码:

    <div class="post">
      <p>第一段落</p>
      <p>第二段落</p>
      <p>第三段落</p>
      <p>第四段落</p>
      <p>第五段落</p>
    </div>
    .post {
      padding: 10px;
      border: 1px solid #ccc;
      width: 200px;
      margin: 20px auto;
    }
    .post p {
      margin:0 0 15px 0;
    }
    
    .post p:last-child {
      margin-bottom:0;
    }

    设置每个 <ul> 的首个子元素,并设置其样式:

    ul>:first-child
    {
    background:yellow;
    }

    #menu ul li:first-child > a   ----------  ul 中第一个li元素中的所有a;

  • 相关阅读:
    提交上了,却在iTunes Connect没有新版本的任何消息
    真机调试 —— An unknown error occurred.
    UI第二节——UIButton详解
    UI第一节—— UILable
    OC第九节——协议与代理
    补10月26日
    我看互联网第一约战
    接受自己的不完美---写在毕业之后的总结
    写给自己的学习之道
    越过山丘
  • 原文地址:https://www.cnblogs.com/yuer20180726/p/11157168.html
Copyright © 2011-2022 走看看