html - 如何限制内联显示的子 div 的数量?

标签 html css

有没有办法使用 CSS3 来限制在一行(或内联)中显示的子 div 的数量?

<div class="parent">

  <div class="child">
    <img>
    <p>Text explaining img</p>
  </div>

  <div class="child">
    <img>
    <p>Text explaining another img</p>
  </div>

  <!-- If I only want two per line, let's push the next two down -->

  <div class="child">
    <img>
    <p>Text explaining another img</p>
  </div>

  <div class="child">
    <img>
    <p>Text explaining another img</p>
  </div>

</div>

我考虑过只应用宽度和边距,但我还希望一行中的最后一个 child 有 margin-right:0;

有什么想法吗?

最佳答案

如果你知道元素的大小,你可以这样做:

.parent {
    border: 2px solid #c00;
    padding: 4px;
    display: inline-block;
}
.child {
    border: 2px solid #0c0;
    padding: 4px;
    display: block;
    width: 64px;
    height: 64px;
    margin: 0 76px 0 0;
}
.child:nth-child(2n) {
    margin: -76px 0 0 76px;
}

http://jsfiddle.net/b4f5c/

关于html - 如何限制内联显示的子 div 的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12613374/

相关文章:

javascript - 如何使用 javascript 设置动态创建的按钮的样式?

javascript - 如何在约束 div 内将溢出的图像居中

jquery - 减少引导时间选择器大小

html - 谷歌地图在 iOS 上通过框溢出

html - 将屏幕拆分为网格并相对垂直和水平对齐元素

html - 主要内容(对于仪表板)有滚动错误

css - DotNetNuke solpartmenu 子菜单对齐

java - 以UTF -8格式提交POST表单数据

javascript - 如何让我的幻灯片在缩小时保护其纵横比?

html - 在 Firefox 上选择时缺少右边框