How to select all children except first and last with CSS selectors

前端 未结 5 1507
花落未央
花落未央 2020-12-24 10:30

How would I select all the children in a table except for the first and last? I\'ve tried this, but it ends up selecting all children that aren\'t first and all children tha

相关标签:
5条回答
  • 2020-12-24 10:57

    I think this should work for you:

    table.programs tr td:not(:first-child,:last-child)
    
    0 讨论(0)
  • 2020-12-24 11:00

    It works in this way

        table.programs tr td:nth-child(1n+2):not(:last-child)
    
    0 讨论(0)
  • 2020-12-24 11:07

    The jQuery solution is fine, but if you want to do it in pure CSS I'd suggest you to apply the rules to the whole table and then reset them for the first and last children. i.e:

    table.programs tr td { 
      /* your rules here */
    }
    
    table.programs tr:first-child td:first-child, 
    table.programs tr:last-child td:last-child {
      /* reset your rules here */
    }
    

    jsFiddle demo

    0 讨论(0)
  • 2020-12-24 11:10

    Use two :not() selectors combined, thereby referring to elements that match both of them, i.e. to elements that match neither of the selectors used as operands of :not():

    table.programs td:not(:first-child):not(:last-child)
    
    0 讨论(0)
  • 2020-12-24 11:23

    like this.

    li:nth-child(n+2):nth-last-child(n+2) { background: red; }
    

    for your requirement

    table.programs tr td:nth-child(n+2):nth-last-child(n+2) { /* Your Style */ }
    
    0 讨论(0)
提交回复
热议问题