【问题标题】:Arranging rows with alternating counts of items with CSS in a responsive layout在响应式布局中使用 CSS 排列具有交替项数的行
【发布时间】:2013-12-23 01:37:40
【问题描述】:

在尝试排列放置在多行中的项目时遇到了一个奇怪的问题,每行包含 5 或 4 个项目(交替 5 个项目、4 个项目、5 个项目等......)。但是,在 480 像素的屏幕宽度下,无论每行有多少,布局都会简单地切换为 2 个项目宽。这会在 4 项行和 5 项行之间产生布局问题。当它们堆叠在一起时,4 行工作正常(2x2),但 5 行在盒子的末端有一个悬挂物品/空白点(2x2x1)。

4/5 行布局:

2x2 行布局(有悬挂物品问题)

HTML:

<section id="rowArea">
   <div class="row fiveRow clearfix">
        <div class="rowItem"></div>
        <div class="rowItem"></div>
        <div class="rowItem"></div>
        <div class="rowItem"></div>
        <div class="rowItem"></div>
   </div>
   <div class="row fourRow clearfix">
        <div class="rowItem"></div>
        <div class="rowItem"></div>
        <div class="rowItem"></div>
        <div class="rowItem"></div>
   </div>
   <div class="row fiveRow clearfix">
        <div class="rowItem"></div>
        <div class="rowItem"></div>
        <div class="rowItem"></div>
        <div class="rowItem"></div>
        <div class="rowItem"></div>
   </div>
</section>

CSS:

/* Screen Width > 480px */
#rowArea {width:810px; height:auto; }
   .row {height:100px; }
   .fourRow {width:80%; padding:0 10%; }
      .fourRow .rowItem {width:25%; height:100px; float:left; }
   .fiveRow {width:90%; padding:0 5%; }
      .fiveRow .rowItem {width:20%; height:100px; float:left; }

/* Screen Width < 810px */
#rowArea {width:100%; height:auto; }

/* Screen Width < 480px */
#rowArea {width:100%; height:auto; }
   .row {height:100px; }
   .fourRow, .fiveRow {width:100%; padding:0 0; }
      .fourRow .rowItem, .fiveRow .rowItem {width:50%; height:100px; float:left; }

我如何重组这个/设置这个样式以消除 5 项行末尾的空白点?非常感谢任何帮助!

【问题讨论】:

  • 是否可以让以下集合中的第一行占据该空白点,或者您希望将三个项目行分开?
  • 我添加了问题的图片以进行澄清

标签: html css responsive-design css-float


【解决方案1】:

可能是 clearfix 类导致了这些差距。 clearfix 类旨在强制元素包含任何浮动的子元素。删除该类,并将 .row 上的移动 CSS 设置为 height: auto,子元素应相互浮动。

基本示例:http://jsfiddle.net/gv5jL/1/

【讨论】:

    猜你喜欢
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多