代码之家  ›  专栏  ›  技术社区  ›  alemjerus

多行跨行为元素

  •  9
  • alemjerus  · 技术社区  · 14 年前

    我想显示一个复杂但大小固定的多行元素列表,假设它们在到达行尾时将换行,使它们在 n 在每行中,当 n 取决于页面宽度。类似于:

    Mary had    Mary had    Mary had
    a little    a little    a little
    LAMB        LAMP        WHISKEY
    
    
    Mary had
    a little
    TOO MUCH
    

    我该怎么做?

    2 回复  |  直到 8 年前
        1
  •  14
  •   T.J. Crowder    8 年前

    inline-block Spans应该做到:

    body {
      font-family: sans-serif;
    }
    #container span {
      display: inline-block;
      width: 5em;
      border: 1px solid black;
    }
    <div id='container'>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
      <span>Mary had a little lamb</span>
    </div>
    </body>
    
    </html>
        2
  •  5
  •   Andy E    14 年前

    结合使用 display: block; float:left; 和你想要的 width height .

    span {
        display: block;
        float: left;
        height: 100px;
        width: 100px;
    }