问题
I'm trying to have a row of four divs.
.frontpage {
width: 240px;
margin-bottom: 20px;
margin-right: 50px;
float: left;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.frontpage img {
margin: 10px auto;
}
.frontpage a:last-child img {
margin-right: 0;
}
<div class="frontpage">RETRO
<br>
<a href="retro/index.html">
<img src="images/name1.jpg" alt="">
</a>
<p>TEXT</p>
<div class="readmore"><a href="retro/index.html">READ MORE</a>
</div>
<div class="frontpage">RETRO
<br>
<a href="retro/index.html">
<img src="images/name1.jpg" alt="">
</a>
<p>TEXT</p>
<div class="readmore"><a href="retro/index.html">READ MORE</a>
</div>
<div class="frontpage">RETRO
<br>
<a href="retro/index.html">
<img src="images/name1.jpg" alt="">
</a>
<p>TEXT</p>
<div class="readmore"><a href="retro/index.html">READ MORE</a>
</div>
<div class="frontpage">RETRO
<br>
<a href="retro/index.html">
<img src="images/name1.jpg" alt="">
</a>
<p>TEXT</p>
<div class="readmore"><a href="retro/index.html">READ MORE</a>
</div>
As you can see im trying to get that last div a 0px margin using last-child. But i can’t seem to get rid of the margin.
回答1:
You have to move selector to .frontpage
.
.frontpage:last-child {
margin-right: 0;
}
回答2:
adding the missing closing div to each solved the problem
.frontpage {
width: 240px;
margin-bottom: 20px;
margin-right: 50px;
float: left;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.frontpage img {
margin: 10px auto;
}
.frontpage a:last-child img {
margin-right: 0;
}
<div class="frontpage">RETRO
<br>
<a href="retro/index.html">
<img src="images/name1.jpg" alt="">
</a>
<p>TEXT</p>
<div class="readmore"><a href="retro/index.html">READ MORE</a>
</div>
</div>
<div class="frontpage">RETRO
<br>
<a href="retro/index.html">
<img src="images/name1.jpg" alt="">
</a>
<p>TEXT</p>
<div class="readmore"><a href="retro/index.html">READ MORE</a>
</div>
</div>
<div class="frontpage">RETRO
<br>
<a href="retro/index.html">
<img src="images/name1.jpg" alt="">
</a>
<p>TEXT</p>
<div class="readmore"><a href="retro/index.html">READ MORE</a>
</div>
</div>
<div class="frontpage">RETRO
<br>
<a href="retro/index.html">
<img src="images/name1.jpg" alt="">
</a>
<p>TEXT</p>
<div class="readmore"><a href="retro/index.html">READ MORE</a>
</div>
</div>
来源:https://stackoverflow.com/questions/27511190/last-child-not-working-css-html