【问题标题】:Using Bootstrap with Rails 4. Why do I get empty space between <div> with each_slice( )?在 Rails 中使用 Bootstrap 4. 为什么在 <div> 和 each_slice( ) 之间会出现空白?
【发布时间】:2015-01-27 11:54:06
【问题描述】:

我正在使用带有 Rails 4 的 Bootstrap。我正在使用 each_slice() 创建两列。但是,使用以下代码,我在左侧列上得到一个空的大空间。

<% @projects.each_slice(1) do |projects| %>
      <div class="col-md-6">
           <% projects.each do |project| %>
                <p><%= project.name %></p>
           <% end %>
     </div>
<% end %>

所以为了进一步解释,我得到以下html:

<div class="row">
    <div class="col-md-6">This is displayed on the left</div>
    <div class="col-md-6">This is displayed on the right</div>
    <div class="col-md-6">This is displayed on the right</div>
    <!-- Here there is a huge empty space, between the first and fourth div -->
    <div class="col-md-6">This is displayed on the left</div>
    <div class="col-md-6">This is displayed on the right</div>
    <div class="col-md-6">This is displayed on the left</div>
    <div class="col-md-6">This is displayed on the right</div>
    <div class="col-md-6">This is displayed on the left</div>
</div>

div 的长度不同。我怎样才能均匀地显示它们,而有时 div 之间不会有很大的空间?我检查了所有的填充和边距,这不是因为它们。

【问题讨论】:

  • 它们是带有文本对齐的内联块元素吗:在父行上对齐?
  • div 只是块,来自 Bootstrap 的父 .row 只有左右边距。没有文本对齐:对齐...

标签: html css ruby-on-rails twitter-bootstrap ruby-on-rails-4


【解决方案1】:

我认为问题在于第二个和第三个 div 之间没有“左”div。 each_slice 将一个数组分组为子数组,但我不明白将它们分成一个元素的组有什么意义,这就是您调用@projects.each_slice(1) 时所做的事情。这可以简化为

<% @projects.each do |project| %>
  <div class="col-md-6">
    <p><%= project.name %></p>
  </div>
<% end %>

但是,我认为这不是你的问题。如果您列出的不是您想要的,您实际上想要生成什么 html?

【讨论】:

  • 感谢您的回复,马克斯。这也适用于显示两列,但是我仍然在一些 div 之间得到一个空白空间。我希望所有 div 之间总是有相等的空间。例如。上边距:20px;
  • 两列是什么意思?您如何将这些显示为两列?你能把html和相关的css放在jsfiddle上吗?在不知道您对.col-md-6 进行什么样式设置的情况下,很难解决这个问题
猜你喜欢
  • 2012-03-02
  • 2018-11-28
  • 2012-08-29
  • 2018-08-16
  • 1970-01-01
  • 2018-03-09
  • 2022-11-28
  • 2018-04-25
  • 1970-01-01
相关资源
最近更新 更多