0

The below was the html and css code, and I want to ask the question: why the first-child does not work? Who can tell me,I will appreciate for your answer.

.footerLinks {
  margin:0 auto;
  list-style: none;
}
.show{
  display: inline-block;
  zoom:1;
  *display:inline;
  color:#eee;
  padding:4px 14px;
  border-top:1px solid rgba(0,0,0,0);
  border-left:1px solid red;
}
.footerLinks li a:hover{
  color: red;
  border-top:1px solid #fff;  
}
.footerLinks .show:first-of-type{
  border-left:none;
}
<nav role='navigation'>
  <ul class="footerLinks">
    <li class="show-first"><a href="#">Home</a></li>
    <li class="show"><a href="#">About</a></li>
    <li class="show"><a href="#">Clients</a></li>
    <li class="show"><a href="#">Contact Us</a></li>
  </ul>
</nav>
Feng Li
  • 9
  • 3

4 Answers4

1

.footerLinks {
  margin:0 auto;
  list-style: none;
}
.show{
  display: inline-block;
  zoom:1;
  *display:inline;
  color:#eee;
  padding:4px 14px;
  border-top:1px solid rgba(0,0,0,0);
  border-left:1px solid red;
}
.footerLinks li:first-child{
 border-left:none;
}
<nav role='navigation'>
  <ul class="footerLinks">
    <li class="show first"><a href="#">Home</a></li>
    <li class="show"><a href="#">About</a></li>
    <li class="show"><a href="#">Clients</a></li>
    <li class="show"><a href="#">Contact Us</a></li>
  </ul>
</nav> 
  • I have a condition, every line has different style, but contain two line has the same style, and the second line's margin-bottom was none, so this solution does not suit me. thanks very much – Feng Li May 05 '17 at 10:07
0

In your case, it should be :first-of-type instead of :first-child since you have a different class as the first-child of the ul.

Example:

&:first-of-type {
      border-left:none;
  }

Or you can move the first li element out of the ul like below.

.footerLinks {
  margin:0 auto;
  list-style: none;
}
.show{
  display: inline-block;
  zoom:1;
  *display:inline;
  color:#eee;
  padding:4px 14px;
  border-top:1px solid rgba(0,0,0,0);
  border-left:1px solid red;
  &:hover {
      color:#fff;
      border-top:1px solid #fff;
  }
  
}
.show:first-child {
   border-left:none;
}
<nav role='navigation'>
    <div class="show-first"><a href="#">Home</a></div>
  <ul class="footerLinks">
    <li class="show"><a href="#">About</a></li>
    <li class="show"><a href="#">Clients</a></li>
    <li class="show"><a href="#">Contact Us</a></li>
  </ul>
</nav>  

The reason is that, if you use :first-child selector with class, it behaves more like li:first-child.class-name. So if your first-child does not have a correct class name, it fails.

Gabriel Cheung
  • 526
  • 3
  • 9
0

You have wrong syntax. &:hover {} is not valid css code. You can use & in SASS or LESS or other preproccesor, but not pure CSS. You probably don't need that "show-first" class and do it with :first-child.

Working code:

.footerLinks {
  margin:0 auto;
  list-style: none;
}
.show{
  display: inline-block;
  zoom:1;
  display:inline;
  color:#eee;
  padding:4px 14px;
  border-top:1px solid rgba(0,0,0,0);
  border-left:1px solid red;
}
.show:hover {
    border-top:1px solid #fff;
}
.show:hover a {
    color:#fff;
}
.show:first-child {
    border-left:none;
}
<nav role='navigation'>
  <ul class="footerLinks">
    <li class="show"><a href="#">Home</a></li>
    <li class="show"><a href="#">About</a></li>
    <li class="show"><a href="#">Clients</a></li>
    <li class="show"><a href="#">Contact Us</a></li>
  </ul>
</nav>
Eugene D
  • 31
  • 2
  • I runed in less environment, post it into the snippet, forget to change it into css, I already changed the question。Thanks for you help me to find the error。 – Feng Li May 05 '17 at 09:09
0

Your main problem here is the class of your first li item. It is set to .show-first , when all the others are set to only .show. You must change your class to .show.first so that .first can be a modulator-class and not a whole new class name as .show-first.

The first-child selector works fine, it just needs to see that first .show class, thats all.

.footerLinks {
  margin:0 auto;
  list-style: none;
}
.show{
  display: inline-block;
  zoom:1;
  *display:inline;
  color:#eee;
  padding:4px 14px;
  border-top:1px solid rgba(0,0,0,0);
  border-left:1px solid red;
}
.footerLinks li:hover{
  color: red;
  border-top:1px solid #fff;  
}
.footerLinks .show:first-child {
  border-left:none;
}
<nav role='navigation'>
  <ul class="footerLinks">
    <li class="show first"><a href="#">Home</a></li>
    <li class="show"><a href="#">About</a></li>
    <li class="show"><a href="#">Clients</a></li>
    <li class="show"><a href="#">Contact Us</a></li>
  </ul>
</nav>
Tanasos
  • 3,928
  • 4
  • 33
  • 63