【问题标题】:Create foundation grid within list elements在列表元素中创建基础网格
【发布时间】:2016-10-31 15:48:46
【问题描述】:

我正在尝试创建一个有序列表,其中该列表的每个 li 都包含 Foundation 5.5.3 网格的一部分。

我遇到的问题是我的li 元素在其子网格列之前的顶部插入了一些空间。

示例:

经过一番调查,我发现这是由于 Foundation CSS 规则将 content: " "; display: table; 放置在我行的 ::before 伪元素上。

但是,覆盖/删除它会弄乱行本身的间距。

我尝试将row 类从li 本身中移除并插入一个子div.row,但我仍然看到同样的问题。

重申一下,我想在ol/ul 列表的每个li 中创建一个两列网格,但是这样做会在每个li 的顶部添加一些垂直空间。我怎样才能摆脱这个空间,或者,我应该采取另一种方法来在几个lis 内实现这个 2 列网格。

谢谢。


示例:

li {
  background-color: #ddd;
  padding: 5px;
}

li + li {
  margin-top: 5px !important;
}

li > span {
  background-color: yellow;
}

/* Trying to override ::before and ::after on .row */
.row.psuedo-override::before, .row.psuedo-override::after {
  content: none !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.3/css/foundation.css" rel="stylesheet"/>
<div class="row">
  <div class="small-12 columns">
    <h1>My List</h1>
    <h5><kbd>li</kbd> tags are <kbd>"row collapse"</kbd></h5>
    <ol>
      <li class="row collapse">
        <span class="small-9 columns">Some long text goes here that should wrap to the next line if it is sufficiently long enough which this should qualify</span>
        <span class="small-3 columns text-right">$100</span>
      </li>
      <li class="row collapse">
        <span class="small-9 columns">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nulla mauris, iaculis vel ante eget, vestibulum ornare est. </span>
        <span class="small-3 columns text-right">$50</span>
      </li>
      <li class="row collapse">
        <span class="small-9 columns">Phasellus quis odio ac sapien congue aliquam sit amet ornare magna. Quisque consequat mauris nec turpis finibus, id bibendum dolor tincidunt.</span>
        <span class="small-3 columns text-right">$75</span>
      </li>
    </ol>
  </div>
</div>

<hr>

<div class="row">
  <div class="small-12 columns">
    <h1>My List</h1>
    <h5><kbd>li</kbd> tags are <kbd>"row collapse"</kbd>, with no psuedo content. Vertical height gets messed up though...</h5>
    <ol>
      <li class="row collapse psuedo-override">
        <span class="small-9 columns">Some long text goes here that should wrap to the next line if it is sufficiently long enough which this should qualify</span>
        <span class="small-3 columns text-right">$100</span>
      </li>
      <li class="row collapse psuedo-override">
        <span class="small-9 columns">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nulla mauris, iaculis vel ante eget, vestibulum ornare est. </span>
        <span class="small-3 columns text-right">$50</span>
      </li>
      <li class="row collapse psuedo-override">
        <span class="small-9 columns">Phasellus quis odio ac sapien congue aliquam sit amet ornare magna. Quisque consequat mauris nec turpis finibus, id bibendum dolor tincidunt.</span>
        <span class="small-3 columns text-right">$75</span>
      </li>
    </ol>
  </div>
</div>

【问题讨论】:

  • 如果你想要的只是一个两列的网格,为什么还要为 Foundation 的臃肿而烦恼呢?您不能只定义自己的类(和样式)吗?您的内容不是更适合表格而不是列表吗?

标签: html css layout zurb-foundation


【解决方案1】:

负边距方法

作为一种避免空格的简单解决方法,您可以为您的内容&lt;span&gt;标签设置一个负上边距,与您的行高一样高(因为这是您的伪元素使用content: " ";产生的高度:

li > span {
  margin-top: -1.6rem;
}

权衡:这仅在您的line-height 没有改变时才有效(例如,由于响应能力,在这种情况下,您必须根据您的媒体查询调整此值)。

li {
  background-color: #ddd;
  padding: 5px;
}
li + li {
  margin-top: 5px !important;
}
li > span {
  background-color: yellow;
  margin-top: -1.6rem;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.3/css/foundation.css" rel="stylesheet" />
<div class="row">
  <div class="small-12 columns">
    <h1>My List</h1>
    <h5><kbd>li</kbd> tags are <kbd>"row collapse"</kbd></h5>
    <ol>
      <li class="row collapse">
        <span class="small-9 columns">Some long text goes here that should wrap to the next line if it is sufficiently long enough which this should qualify</span>
        <span class="small-3 columns text-right">$100</span>
      </li>
      <li class="row collapse">
        <span class="small-9 columns">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nulla mauris, iaculis vel ante eget, vestibulum ornare est. </span>
        <span class="small-3 columns text-right">$50</span>
      </li>
      <li class="row collapse">
        <span class="small-9 columns">Phasellus quis odio ac sapien congue aliquam sit amet ornare magna. Quisque consequat mauris nec turpis finibus, id bibendum dolor tincidunt.</span>
        <span class="small-3 columns text-right">$75</span>
      </li>
    </ol>
  </div>
</div>

清除浮动方法

另一种方法是像使用content: none !important; 一样删除伪元素。现在的问题是&lt;li&gt; 标签中只剩下浮动内容。因此,您必须在每个 &lt;li&gt; 标签内容的末尾插入一个元素,该元素应用了 clear: both; 规则,例如&lt;br&gt;

<br class="clear" />
.clear {
  clear: both;
}

权衡:您必须更改标记,然后您的枚举就会消失。

li {
  background-color: #ddd;
  padding: 5px;
}
li + li {
  margin-top: 5px !important;
}
li > span {
  background-color: yellow;
  display: inline-block;
}
.row::before, .row::after {
    content: none !important;
}
.clear {
  clear: both;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.3/css/foundation.css" rel="stylesheet" />
<div class="row">
  <div class="small-12 columns">
    <h1>My List</h1>
    <h5><kbd>li</kbd> tags are <kbd>"row collapse"</kbd></h5>
    <ol>
      <li class="row">
        <span class="small-9 columns">Some long text goes here that should wrap to the next line if it is sufficiently long enough which this should qualify</span>
        <span class="small-3 columns text-right">$100</span>
        <br class="clear" />
      </li>
      <li class="row">
        <span class="small-9 columns">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nulla mauris, iaculis vel ante eget, vestibulum ornare est. </span>
        <span class="small-3 columns text-right">$50</span>
        <br class="clear" />
      </li>
      <li class="row">
        <span class="small-9 columns">Phasellus quis odio ac sapien congue aliquam sit amet ornare magna. Quisque consequat mauris nec turpis finibus, id bibendum dolor tincidunt.</span>
        <span class="small-3 columns text-right">$75</span>
        <br class="clear" />
      </li>
    </ol>
  </div>
</div>

【讨论】:

  • 再次感谢您。我最终选择了您的第一个解决方案(“负边距”方法),但后来发现它在 Firefox(v51)中无法正常工作。 clearbreak 方法确实有效,因此我将标记切换为该方法。
【解决方案2】:

这是一个非常奇怪的错误。在不更改 HTML 中的任何内容的情况下,这是我能想到的最佳解决方案:

li {
  background-color: #ddd;
  padding: 5px;
}

li + li {
  margin-top: 5px !important;
}

li > span {
  background-color: yellow;
}
.row.collapse:before {
  display: block !important;
  height: .02px;
}

.row.collapse {
  padding: 0; /* Did you want padding? */
  width: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.3/css/foundation.css" rel="stylesheet"/>
<div class="row">
  <div class="small-12 columns">
    <h1>My List</h1>
    <h5><kbd>li</kbd> tags are <kbd>"row collapse"</kbd></h5>
    <ol>
      <li class="row collapse">
        <span class="small-9 columns">Some long text goes here that should wrap to the next line if it is sufficiently long enough which this should qualify</span>
        <span class="small-3 columns text-right">$100</span>
      </li>
      <li class="row collapse">
        <span class="small-9 columns">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nulla mauris, iaculis vel ante eget, vestibulum ornare est. </span>
        <span class="small-3 columns text-right">$50</span>
      </li>
      <li class="row collapse">
        <span class="small-9 columns">Phasellus quis odio ac sapien congue aliquam sit amet ornare magna. Quisque consequat mauris nec turpis finibus, id bibendum dolor tincidunt.</span>
        <span class="small-3 columns text-right">$75</span>
      </li>
    </ol>
  </div>
</div>

<hr>

<div class="row">
  <div class="small-12 columns">
    <h1>My List</h1>
    <h5><kbd>li</kbd> tags are <kbd>"row collapse"</kbd>, with no psuedo content. Vertical height gets messed up though...</h5>
    <ol>
      <li class="row collapse psuedo-override">
        <span class="small-9 columns">Some long text goes here that should wrap to the next line if it is sufficiently long enough which this should qualify</span>
        <span class="small-3 columns text-right">$100</span>
      </li>
      <li class="row collapse psuedo-override">
        <span class="small-9 columns">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nulla mauris, iaculis vel ante eget, vestibulum ornare est. </span>
        <span class="small-3 columns text-right">$50</span>
      </li>
      <li class="row collapse psuedo-override">
        <span class="small-9 columns">Phasellus quis odio ac sapien congue aliquam sit amet ornare magna. Quisque consequat mauris nec turpis finibus, id bibendum dolor tincidunt.</span>
        <span class="small-3 columns text-right">$75</span>
      </li>
    </ol>
  </div>
</div>

W3C 说我的 CSS 是有效的,尽管它有点像 hack。
我对这个问题完全感到困惑。

【讨论】:

    【解决方案3】:

    CSS 计数器方法:

    1) 回到您尝试将行类从 li 本身移除并插入子 div.row 的位置。我认为这避免了与列表的默认布局和与行的默认布局冲突的列表项有关的许多问题。

    2) 使用 CSS 插入您自己的带有计数器的列表编号,而不是使用默认的 &lt;ol&gt; 编号。这将允许您将号码放置在您想要的位置。

    ol {list-style-type:none; margin-left:0; padding-left:0;}
    li {padding-left:2em; counter-increment:li;}
    li::before {content:counter(li) "."; position:absolute; margin-left:-2em;}
    

    小提琴:https://jsfiddle.net/9qw2akkj/

    【讨论】:

    • 通过不设置counter-reset 属性,计数器范围也将应用于嵌套的&lt;ol&gt; 元素,您可以在此处看到:jsfiddle demo。我建议设置此类属性以在 &lt;ol&gt; 元素内创建 &lt;li&gt; 计数器范围:demo
    【解决方案4】:

    只需添加此 css 即可解决问题...

    .row .row.collapse::before, .row .row.collapse::after {
        display: block!important;
        height: 0;
    }
    

    这是完整的代码:)

    li {
      background-color: #ddd;
      padding: 5px;
    }
    
    li + li {
      margin-top: 5px !important;
    }
    
    li > span {
      background-color: yellow;
    }
    
    /* Trying to override ::before and ::after on .row */
    .row .row.collapse::before, .row .row.collapse::after {
        display: block!important;
        height: 0;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.3/css/foundation.css" rel="stylesheet"/>
    <div class="row">
      <div class="small-12 columns">
        <h1>My List</h1>
        <h5><kbd>li</kbd> tags are <kbd>"row collapse"</kbd></h5>
        <ol>
          <li class="row collapse">
            <span class="small-9 columns">Some long text goes here that should wrap to the next line if it is sufficiently long enough which this should qualify</span>
            <span class="small-3 columns text-right">$100</span>
          </li>
          <li class="row collapse">
            <span class="small-9 columns">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nulla mauris, iaculis vel ante eget, vestibulum ornare est. </span>
            <span class="small-3 columns text-right">$50</span>
          </li>
          <li class="row collapse">
            <span class="small-9 columns">Phasellus quis odio ac sapien congue aliquam sit amet ornare magna. Quisque consequat mauris nec turpis finibus, id bibendum dolor tincidunt.</span>
            <span class="small-3 columns text-right">$75</span>
          </li>
        </ol>
      </div>
    </div>
    
    <hr>
    
    <div class="row">
      <div class="small-12 columns">
        <h1>My List</h1>
        <h5><kbd>li</kbd> tags are <kbd>"row collapse"</kbd>, with no psuedo content. Vertical height gets messed up though...</h5>
        <ol>
          <li class="row collapse psuedo-override">
            <span class="small-9 columns">Some long text goes here that should wrap to the next line if it is sufficiently long enough which this should qualify</span>
            <span class="small-3 columns text-right">$100</span>
          </li>
          <li class="row collapse psuedo-override">
            <span class="small-9 columns">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nulla mauris, iaculis vel ante eget, vestibulum ornare est. </span>
            <span class="small-3 columns text-right">$50</span>
          </li>
          <li class="row collapse psuedo-override">
            <span class="small-9 columns">Phasellus quis odio ac sapien congue aliquam sit amet ornare magna. Quisque consequat mauris nec turpis finibus, id bibendum dolor tincidunt.</span>
            <span class="small-3 columns text-right">$75</span>
          </li>
        </ol>
      </div>
    </div>

    希望有帮助:)

    【讨论】:

      猜你喜欢
      • 2014-06-05
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多