How to show the first N elements of a block and hide the others in css?

前端 未结 4 1244
野趣味
野趣味 2020-12-05 06:22

I am trying to hide the first 3 elements having the class .row inside the block .container.

What I\'m doing is hiding all the .row

相关标签:
4条回答
  • 2020-12-05 06:48

    Also, like Giovanni's solution, something like this could also work.

    .container > .row:nth-child(3) ~ .row {
        /* this rule targets the rows after the 3rd .row */
        display: none;
    }
    
    0 讨论(0)
  • 2020-12-05 06:56

    You don't even need CSS3 selectors:

    .row + .row + .row + .row {
        display: none;
    }
    

    This should work even in IE7.
    Updated fiddle

    0 讨论(0)
  • 2020-12-05 07:02
    1. You have a .notarow as the first child, so you have to account for that in your :nth-child() formula. Because of that .notarow, your first .row becomes the second child overall of the parent, so you have to count starting from the second to the fourth:

      .row:nth-child(-n+4){
          display:block;
      }
      

      Updated fiddle

    2. What you're doing is fine.

    0 讨论(0)
  • 2020-12-05 07:06

    I found this answer by Googling "css show first n elements", but the question now seems to be the opposite (hiding first n elements)

    :nth-child(n+4)
    

    ^ this will work if you're looking for what I was looking for

    0 讨论(0)
提交回复
热议问题