search

Home  >  Q&A  >  body text

css3不包括

我这下面有一组li,我想给他们加个红色背景,但是就是除了最后一个不加要怎么写?
我用下面那样写没用

li:not(li:nth-last-child){
   background:red;
}

<ul>
<li></li>
<li></li>
<li></li>
</ul>
大家讲道理大家讲道理2778 days ago526

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-17 11:29:48

    Isn’t that so?

    li:not(:last-of-type){
    }
    /**:last-of-type指的是相对父元素的最后一个li元素**/

    The answer on the second floor can also be used

    li:not(:last-child){
    }
    /**:last-child指的是li元素而且这个li元素是父元素的最后一个元素**/
    
    /**等同于楼主的**/
    li:not(:nth-last-child(1)){
    }

    What’s the difference between the two?

    When the child elements of the ul element are all li elements, the two are the same

    <ul>
        <li>
        <li>
        <li><!--第一个和第二个选择器都会忽略这个-->
    <ul>

    But if not all the child elements of the ul element are li and the last element is not li either

    <ul>
        <li>
        <li>
        <p>
        <li> <!--这时候第一个选择器会忽略这个,而第二个选择器会全选上-->
        <i>
    <ul>

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 11:29:48

    li:not(:last-child){
    }

    reply
    0
  • Cancelreply