php - 每4张图片清晰

标签 php css

我有一堆图片;我需要将它们向左浮动,每 4 张图像清除它们并转到下一行(如表格):

for ($i = 1; $i <= 24; $i++) {
    if ($i % 4 == 0) {
        echo "<img src='" . base_url('/images/question.jpg') . "' alt='Smiley face' height='65' width='65' id='" . ($i-1 ) . "' class='guess last_in_row'><div class='img_container last_in_row'></div>";
    } else {
        echo "<img src='" . base_url('/images/question.jpg') . "' alt='Smiley face' height='65' width='65' id='" . ($i-1 ) . "' class='guess '><div class='img_container'></div>";
    }
}

我尝试了很多,但无法获得所需的输出:我最后一次尝试是:

.guess{
    float: left;
    display: block;
}
.last_in_row:after{
        clear: left;

}

Fiddle with the rendered HTML

最佳答案

作为纯 CSS 解决方案,您可以使用 :nth-of-type():nth-of-type(4n+1) 这样的伪类将 clear: left; CSS 声明应用于每个 4 图像元素从 1 开始:

EXAMPLE HERE

#images img:nth-of-type(4n+1) {
    clear: left;
}

规范规定:

6.6.5.4. :nth-of-type() pseudo-class

The :nth-of-type(an+b) pseudo-class notation represents an element that has an+b-1 siblings with the same expanded element name before it in the document tree, for any zero or positive integer value of n, and has a parent element. See :nth-child() pseudo-class for the syntax of its argument. It also accepts the ‘even’ and ‘odd’ values.

值得注意的是:nth-of-type()伪类is supported in IE9+ .

关于php - 每4张图片清晰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23275122/

相关文章:

php - 在 php file_get_contents 中获取 referer

php - Vim:将 omnicomplete 显示的方法限制为 ctags 中存在的方法

javascript - 表单发布不接受链接

html - 如何使用 css 定位 id ="last_name[]_field"

javascript - 如何在选择框中隐藏选定的选项文本,Angular 4

html - 可以仅使用 HTML/CSS 进行测验吗?

php - 无法将 user_id 和 product_id 插入购物车。拉维尔 5

php - 在 PHP 中通过 JSONArray 进行 Foreach

html - 如何在不移动表格标题的情况下滚动表格的主体?

html - CSS 平行四边形切割边框图案