我在php中有一个小脚本:
<section>
<?php
$sql = "SELECT * FROM bilder";
$result=mysqli_query($db,$sql);
$counter = 1;
while($row=mysqli_fetch_assoc($result)){
if($counter == 1){
echo "<img class='image' src='$row[bild_pfad]' alt='$row[bild_name]' style='$row[bild_werte]'>";
}
else{
echo "<img class='image image-margin' src='$row[bild_pfad]' alt='$row[bild_name]' style='$row[bild_werte]'>";
}
if($counter == 6){
$counter = 1;
}
else{
$counter = $counter + 1;
}
}
?>
</section>这个脚本创建了“许多”图像。图像的信息在数据库中。在正常情况下,<section></section>是925px宽度。1图像为150px x 150px大。
.image{
object-position: center; /* Center the image within the element */
height: 150px;
width: 150px;
object-fit: cover;
}
.image-margin{
margin-left: 5px;
}在正常情况下,6张图片并排,边缘左边5 5px。这就是为什么我在脚本中使用$counter的原因,因为一行由6张图片组成,第一张图片不需要留下空白。现在我使用了一些媒体查询。
@media only screen and (max-width : 924px) {
section{
margin: 0px auto;
width: 770px;
margin-top: 55px;
}
}
@media only screen and (max-width : 770px) {
section{
margin: 0px auto;
width: 615px;
margin-top: 55px;
}
}
@media only screen and (max-width : 615px) {
section{
margin: 0px auto;
width: 460px;
margin-top: 55px;
}
}
@media only screen and (max-width : 460px) {
section{
margin: 0px auto;
width: 305px;
margin-top: 55px;
}
}每个媒体查询类都会使行收缩1,图片将转到下一行。我的问题是:在脚本中,我已经连续设置了6张图片,如何为媒体查询定制这些图片呢?( 5-,4-,3-,2-一排图片)
发布于 2015-03-17 21:48:33
不要使用计数器变量来确定页边距应该在哪里。
使用nth-子CSS选择器来确定哪些图像应该有边距.
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child
https://css-tricks.com/how-nth-child-works/
将您的边距设置在右边,并从一行中的最后一个图像中删除边距。
@media only screen and (max-width : 924px) {
/* Five images in each row */
section{
margin: 0px auto;
width: 770px;
margin-top: 55px;
}
section img{
margin-right:5px;
}
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;
}
}https://stackoverflow.com/questions/29109868
复制相似问题