代码之家  ›  专栏  ›  技术社区  ›  Josh Rodgers

左对齐库的最后一行

  •  0
  • Josh Rodgers  · 技术社区  · 7 年前

    我有一个图片库,看起来像: enter image description here

    <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,它需要检查并查看行中有多少项,然后添加缺少的列。

    我不确定哪种方法最简单,或者是否有更好的解决方案……有人有什么想法,或者有人能为我指出正确的方向吗?

    谢谢
    乔希

    2 回复  |  直到 7 年前
        1
  •  0
  •   Stephan-v    7 年前

    您可以使用flexbox解决方案实现更流畅、更灵活的布局,请参阅下面代码笔中的解决方案。

    .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;
    }
    

    https://codepen.io/ahmedi/pen/KvVgQW
    
        2
  •  0
  •   2oppin    7 年前

    首先,用html中的class=“squares”替换所有id=“squares” 和#平方和.平方

    -是页面上DOM元素的唯一标识符

    然后添加到css中:

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