【问题标题】:Left-align last gallery row左对齐画廊的最后一行
【发布时间】:2017-07-30 01:56:44
【问题描述】:

我有一个图片库,看起来像:

代码如下:

<div class="wrapper">
    <div id="squares">
        <a href="yourlinkhere1.php"><img src="images/galleryimage1.jpg"></a>
        <h5>IMG_114</h5>
    </div>
    <div id="squares">
        <a href="yourlinkhere2.php"><img src="images/galleryimage2.jpg"></a>
        <h5>IMG_115</h5>
    </div>
</div>

CSS 看起来像:

.wrapper {
    background: #ff0000;
    text-align: center;
}

#squares {
    background: #00ff00;
    display: inline-block;
    vertical-align: top;
    width: 300px;
}

#squares img {
    max-height: 200px;
    width: auto;
}

#squares h5 {
    margin: 20px 0;
}

我喜欢它的结果,但我希望IMG_117 在IMG_114 下对齐,同时保持绿色区域居中并且 div 中的内容居中...不知道如何让该 div 不左对齐不居中其他东西。

我在想这可能是对我的 css 或一些 jQuery 的快速调整,以便在 IMG_117 行中再添加两列。我看到的问题是这是动态内容,我并不总是知道最后一行有多少……可能是一、二或三,如果有一些 jQuery,它需要检查并查看有多少项目在行中,然后添加缺少的列。

我不确定哪种方法最简单,或者是否有更好的解决方案...有没有人有任何想法或谁能指出我正确的方向?

谢谢,
乔什

【问题讨论】:

    标签: jquery css alignment gallery


    【解决方案1】:

    您可以使用 flexbox 解决方案以获得更流畅和灵活的布局,请参阅下面的 codepen 中的解决方案。

    .wrapper {
        background: #ff0000;
        text-align: center;
        width: 100%;
        height:auto;
        display: -webkit-flex;
        display: flex;
        -webkit-flex-wrap: wrap;
        flex-wrap: wrap;
        padding: 0 5% 0;
    }
    

    Codepen

    https://codepen.io/ahmedi/pen/KvVgQW
    

    【讨论】:

    • 我从字面上复制并粘贴了您的代码,一切看起来都不错!我什至没有考虑 flex 属性!
    • 谢谢! Josh Rodgers FlexBox 是 css3 的绝佳属性,您也可以在许多布局中使用它。
    【解决方案2】:

    首先在 html 中将所有 id="squares" 替换为 class="squares" 和带有 .squares 的 #squares

    id - 表示页面上 DOM 元素的唯一标识符

    然后添加到你的css:

    .squares {
       float: left; /* all elements will tend to left*/
    }
    .wrapper {
      clear: both; /* reset rules of floating for the next elements*/
    }
    

    【讨论】:

    • 这非常接近,不幸的是整个列表(绿色部分)没有保持居中......但我感谢您的贡献:-)
    猜你喜欢
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    相关资源
    最近更新 更多