Hello. There is a block with links. The number of links in the block can be different. I want to highlight the first and last link, or rather change the border-radius. If the number of links would be fixed, I would do it like this:

.menu_left { display: inline-block; float: left; margin-top: 15px; border-radius: 10px; } .menu_left a { display: block; width: 210px; background-color: #EEEEEE; padding: 6px; } .menu_left a:nth-child(1) { border-radius: 10px 0 0 0; } .menu_left a:nth-child(4) { border-radius: 0 0 0 10px; } 
  <div class="menu_left"> <a href="/">ΠΠ»ΡŽΠΌΠΈΠ½ΠΈΠ΅Π²Ρ‹Π΅ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Ρ‹</a> <a href="/">БимСталличСскиС Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Ρ‹</a> <a href="/">ΠšΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚ΡƒΡŽΡ‰ΠΈΠ΅ ΠΊ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Π°ΠΌ</a> <a href="/">Π Π΅ΠΊΠΎΠΌΠ΅Π½Π΄Π°Ρ†ΠΈΠΈ ΠΏΠΎ Π²Ρ‹Π±ΠΎΡ€Ρƒ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Π°</a> </div> 

How to be in my case?

  • 2
    use first-child and last-child - Alexey Shimansky

4 answers 4

For this there are special pseudo-classes :first-child and :last-child .

 .menu_left { display: inline-block; float: left; margin-top: 15px; border-radius: 10px; } .menu_left a { display: block; width: 210px; background-color: #EEEEEE; padding: 6px; } .menu_left a:first-child { border-radius: 10px 0 0 0; } .menu_left a:last-child { border-radius: 0 0 0 10px; } 
 <div class="menu_left"> <a href="/">ΠΠ»ΡŽΠΌΠΈΠ½ΠΈΠ΅Π²Ρ‹Π΅ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Ρ‹</a> <a href="/">БимСталличСскиС Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Ρ‹</a> <a href="/">ΠšΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚ΡƒΡŽΡ‰ΠΈΠ΅ ΠΊ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Π°ΠΌ</a> <a href="/">Π Π΅ΠΊΠΎΠΌΠ΅Π½Π΄Π°Ρ†ΠΈΠΈ ΠΏΠΎ Π²Ρ‹Π±ΠΎΡ€Ρƒ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Π°</a> </div> 

  • thank. I will know now - iKey

Use pseudo-classes

first-child

last-child

 a:first-child { border-radius: 10px 0 0 0; } a:last-child { border-radius: 0 0 0 10px; } 

    Still have that option

    first-of-type

    last-of-type

    Differences between: nth-child and: nth-of-type

     .menu_left { display: inline-block; float: left; margin-top: 15px; border-radius: 10px; } .menu_left a { display: block; width: 210px; background-color: #EEEEEE; padding: 6px; } .menu_left a:first-of-type { border-radius: 10px 0 0 0; } .menu_left a:last-of-type { border-radius: 0 0 0 10px; } 
     <div class="menu_left"> <a href="/">ΠΠ»ΡŽΠΌΠΈΠ½ΠΈΠ΅Π²Ρ‹Π΅ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Ρ‹</a> <a href="/">БимСталличСскиС Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Ρ‹</a> <a href="/">ΠšΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚ΡƒΡŽΡ‰ΠΈΠ΅ ΠΊ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Π°ΠΌ</a> <a href="/">Π Π΅ΠΊΠΎΠΌΠ΅Π½Π΄Π°Ρ†ΠΈΠΈ ΠΏΠΎ Π²Ρ‹Π±ΠΎΡ€Ρƒ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Π°</a> </div> 

      : first-child and : last-child - these pseudo-classes are responsible for referring to the first and last element of the parent.

       .menu_left { display: inline-block; float: left; margin-top: 15px; border-radius: 10px; } .menu_left a { display: block; width: 210px; background-color: #EEEEEE; padding: 6px; } .menu_left a:first-child { border-radius: 10px 0 0 0; } .menu_left a:last-child { border-radius: 0 0 0 10px; } 
       <div class="menu_left"> <a href="/">ΠΠ»ΡŽΠΌΠΈΠ½ΠΈΠ΅Π²Ρ‹Π΅ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Ρ‹</a> <a href="/">БимСталличСскиС Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Ρ‹</a> <a href="/">ΠšΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚ΡƒΡŽΡ‰ΠΈΠ΅ ΠΊ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Π°ΠΌ</a> <a href="/">Π Π΅ΠΊΠΎΠΌΠ΅Π½Π΄Π°Ρ†ΠΈΠΈ ΠΏΠΎ Π²Ρ‹Π±ΠΎΡ€Ρƒ Ρ€Π°Π΄ΠΈΠ°Ρ‚ΠΎΡ€Π°</a> </div>