last-child not working css/html

为君一笑 提交于 2021-02-16 20:08:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!