CSS last-child selector: select last-element of specific class, not last child inside of parent?

前端 未结 6 781
夕颜
夕颜 2020-11-22 01:45
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 02:34

    If you are floating the elements you can reverse the order

    i.e. float: right; instead of float: left;

    And then use this method to select the first-child of a class.

    /* 1: Apply style to ALL instances */
    #header .some-class {
      padding-right: 0;
    }
    /* 2: Remove style from ALL instances except FIRST instance */
    #header .some-class~.some-class {
      padding-right: 20px;
    }
    

    This is actually applying the class to the LAST instance only because it's now in reversed order.

    Here is a working example for you:

    
    CSS Test
    
    
    
    
    
    
    

提交回复
热议问题