我目前有一个图像网格,可以根据窗口和容器的大小适当缩放。
http://jsfiddle.net/AndyMP/2zND2/13/
到目前为止,我已经尝试过在不破坏现有结构的情况下将 DIV 包裹在图像周围,但未能成功。我该怎么做?
CSS
#photos {
/* Prevent vertical gaps */
line-height: 0;
width: 100%;
}
#photos img {
/* Just in case there are inline attributes */
width: 20%;
height: auto;
float:left;
overflow:hidden;
}
@media (min-width:1200px) {
#photos img {
/* 5 images wide */
width: 19.2% ; margin: 0 1% 1% 0;
}
#photos img:nth-child(5n) { margin: 0 0 1% 0; }
}
@media (min-width:800px) and (max-width: 1200px) {
#photos img {
/* 5 images wide */
width: 19.2% ; margin: 0 1% 1% 0;
}
#photos img:nth-child(5n) { margin: 0 0 1% 0; }
}
@media (min-width:400px) and (max-width: 800px) {
#photos img {
/* 4 images wide */
width: 24.25% ; margin: 0 1% 1% 0;
}
#photos img:nth-child(4n) { margin: 0 0 1% 0; }
}
@media (min-width:300px) and (max-width: 400px) {
#photos img {
/* 2 images wide */
width: 49% ; margin: 0 2% 2% 0;
}
#photos img:nth-child(2n) { margin: 0 0 2% 0; }
}
@media (max-width: 300px) {
#photos img {
/* 1 image wide */
width: 100% ; margin: 0 0 2% 0;
}
}
最佳答案
首先,这是您在应用我即将提到的步骤后应该得到的最终结果:little link .
1) 将每个 img
包裹在 div
中。
2) 添加 CSS 规则:
#photos div {
display: inline-block;
}
3) 将所有选择器 #photos img
更改为 #photos div
。
4) 添加:
#photos div img {
width: 100%;
}
关于css - 在保留现有网格灵活性的同时将图像包装在 DIV 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12357454/