dongqiyou0303 2015-03-18 14:16
浏览 78

媒体查询中的PHP nth-child

On MyPage are many pictures. In the normal case are 6 pictures in one row. If you change the size of the browser the number of pictures in each row will change. I solved this problem with media queries. In each row the last picture shouldnt get a margin-right: 5px;. But the nth-child wont change its every time the same, thats why the margin isnt correct. What did i wrong?

Here is my php output for the images:

<?php
    $sql = "SELECT * FROM bilder";
    $result=mysqli_query($db,$sql);
    while($row=mysqli_fetch_assoc($result)){
        echo "<img class='image' src='$row[bild_pfad]' alt='$row[bild_name]' style='$row[bild_werte]'>";
    } 
?>

Here is the css:

section{
    margin: 0px auto;
    width: 925px;
    margin-top: 55px;
}

.image{
    object-position: center; /* Center the image within the element */
    height: 150px;
    width: 150px;
    object-fit: cover;
    margin-right: 5px;
}

.image-margin{
    margin-right: 5px;
}

section img:nth-child(6n+6){
    margin-right:0;
}

@media only screen and (max-width : 924px) {
/* Five images in each row */
section{
    margin: 0px auto;
    width: 770px;
    margin-top: 55px;
}
  section img:nth-child(5n+5){
    margin-right:0;
  }
}

@media only screen and (max-width : 770px) {
  /* Four images in each row */
section{
    margin: 0px auto;
    width: 615px;
    margin-top: 55px;
}
  section img:nth-child(4n+4){
    margin-right:0;
  }
}

@media only screen and (max-width : 615px) {
section{
    margin: 0px auto;
    width: 460px;
    margin-top: 55px;
}
      section img:nth-child(3n+3){
    margin-right:0;
  }
}
@media only screen and (max-width : 460px) {
section{
    margin: 0px auto;
    width: 305px;
    margin-top: 55px;
}
      section img:nth-child(2n+2){
    margin-right:0;
  }
}

</div>
  • 写回答

1条回答 默认 最新

  • duande3134 2015-03-18 20:16
    关注

    I solved my problem. I had to reset the old nth-child code like this:

    section img:nth-child(6n+6){
        margin-right:0;
     }
    
    @media only screen and (max-width : 924px) {
    /* Five images in each row */
    section{
        margin: 0px auto;
        width: 770px;
        margin-top: 55px;
    }
      section img:nth-child(6n+6){
        margin-right:5px;
     }
      
      
      section img:nth-child(5n+5){
        margin-right:0;
      }
    }

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题