How to select first 2
  • elements using css
  • 后端 未结 4 1513
    悲&欢浪女
    悲&欢浪女 2020-12-13 08:26

    Hi i want to apply css for first 2 elements (one,two) that are adjacent to first

    element.

    4条回答
    •  鱼传尺愫
      2020-12-13 08:52

      For first 2 li elements inside ul p try:

      .one ul li:nth-child(-n+3){
          // your style
      }
      

      See on jsfiddle

      And as other mates mentioned: you have invalid markup.

      If you removed p element, try:

      .one ul li:nth-child(-n+2){
          // your style
      }
      

      See on jsfiddle

      Update: My suggestion is to use another ul instead of p: So you have valid markup and same result:

      HTML

      • 0
        • One
        • Two
        • 3
      • Four
      • Five

      CSS:

      .one ul {
          padding: 0;
          list-style-type: none;
      }
      .one ul ul li:nth-child(-n+2){
          // your style
      }
      

      Updated jsfiddle

      Note: As your last comment, If you have only 2 special li element, Why not define a class name simply...

    • Do it simple

      ul li.bold {
          // your style
      }
      

    提交回复
    热议问题