【问题标题】:html entries horizontally, two across - line up evenlyhtml条目水平,两个交叉 - 均匀排列
【发布时间】:2014-12-13 23:32:35
【问题描述】:

我有一大组“li”元素(由一个图像和几行文本组成)位于内容 div 中。我的“li”元素的大小会有所不同(有些有长链接,有些有长标题等)

如果可能,我希望有两列;我需要做出响应,所以当我使用移动设备时,我可能会有一栏。我认为有一个固定大小的“li”可以解决问题,但显然不是。每次标题或链接中的一个比另一个长时,它都会抛出所有块。

使用“li”元素会更好地使用 div 吗?

<div>
    <img src="test.png" />
    <h2>One -  average title is here</h2>
    <h3><a href="link 1">Link is here  and this might be long also</a></h3>
</div>

如果没有“li”的固定高度,有没有更好的方法来做到这一点?为什么我的第一个元素总是显得更低?我知道这不是火箭科学,但我似乎想不出解决办法。

jsfiddle

【问题讨论】:

  • 如果你只是将float:left 添加到lis 中,它可以使用固定高度。如果您不想固定高度,我不确定您希望它看起来如何
  • 尝试使用bootstrap
  • 请查看下面的答案,如果它解决了您的问题,请将答案标记为已接受。

标签: html html-lists


【解决方案1】:

我认为下面的布局更好:

(尝试将浏览器窗口的大小调整为小于480px 以查看移动布局。)

*{
  margin: 0px;
  padding: 0px;
}

html, body{
  width: 100%;
}

li{
  width: 40%;
  vertical-align: top;

}

@media screen and (max-width: 480px){
  li{
    width: 80%;
  }    
}

ul,li { 
  padding: 0; 
  margin: 0; 
}

#links {  
  font-size: 0; 
  text-align: center;
}

#links li { 
  font-size: 12px; 
  display: inline-block; 
  border: 1px solid #000000; 
  padding: 2px;  
  background: #c0c0c0; 
  margin: 5px; 
}

#links li h2 { 
  font-size: 1em; 
}
<ul id="links">
  <li>
    <img src="test.png" />
    <h2>One -  average title is here</h2>
    <h3><a href="link 1">Link is here  and this might be long also</a></h3>
  </li>
  <li>
    <img src="test.png" />
    <h2>Two - this title is here and is really, really long</h2>
    <h3><a href="link 1">Link is here </a></h3>
  </li>
  <li>
    <img src="test.png" />
    <h2>Threee -title is here</h2>
    <h3><a href="link 1">Link is here </a></h3>
  </li>
  <li>
    <img src="test.png" />
    <h2>Four - this title is here and is really, really long</h2>
    <h3><a href="link 1">Link is here </a></h3>
  </li>
  <li>
    <img src="test.png" />
    <h2>Five title is here</h2>
    <h3><a href="link 1">Link is here </a></h3>
  </li>
  <li>
    <img src="test.png" />
    <h2>Six title is here</h2>
    <h3><a href="link 1">Link is here and this area can be long also </a></h3>
  </li>    


</ul>

Updated jsFiddle Demo

阅读:CSS @media queries | MDN

【讨论】:

    【解决方案2】:

    去掉那个固定的大小,换成float: left

    #links li { 
      font-size: 12px; 
      border: 1px solid #000000; 
      padding: 0;  
      background: #c0c0c0; 
      
      float: left;     /*change here*/
      margin: 2%;      /*responsive margin*/
      width: 45%;      /*and width*/  
    }

    现在清除每个 2n + 1 div 的左侧:

    #links li:nth-child(2n + 1){
        clear: left;
    }

    并添加媒体查询:

    @media screen and (max-width: 400px){
        #links li {
            margin: 2% 0;
            width: 97%;
        }
    }

    JSFIDDLE.

    【讨论】:

    • 我真的很想接受这两个答案,因为两者都同样有效,但不知道该怎么做。感谢你们两位如此快速的回复 -
    猜你喜欢
    • 2016-08-06
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2014-05-29
    • 1970-01-01
    • 2012-01-14
    • 2013-04-01
    相关资源
    最近更新 更多