Bootstrap div不在正确的位置

时间:2018-06-04 00:11:13

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

enter image description here有人可以向我解释为什么会这样吗?我的移动视图工作正常,但是当我展开窗口时,它会破坏附加的图片。两张卡的代码完全相同,如下所示:

<!-- This is the view for the page: -->
<div class="col-12 col-md-9">
  <div class="col-12 d-flex align-items-center mb-3">
    <span class="h2"> Spread the Knowledge </span>
    <%= link_to fa_icon("edit"), new_discussion_path, class: "profile-link fa-2x"  %>
  </div>
  <div class="col-xs-12 col-md-9 mb-4">
    <div class="card">
      <div class="card-header">
        <h4>Answered Questions</h4>
      </div>
      <div class="list-group list-group-flush">
        <%= render "answered_disc" %>
      </div>
      <div class="card-footer text-center d-none">
        See all
      </div>
    </div>
  </div>
  <div class="col-xs-12 col-md-9 mb-4">
    <div class="card">
      <div class="card-header">
        <h4>Unanswered Questions</h4>
      </div>
      <div class="list-group list-group-flush">
        <%= render "unanswered_disc" %>
      </div>
      <div class="card-footer text-center d-none">
        See all
      </div>
    </div>
  </div>
</div>

两个部分显示以下代码:

<% @discussions.each do |discussion| %>
    <% if discussion.parent_id.nil? && discussion.resolved == true %>
        <div class="list-group-item">
          <div class="row">

             <div class="wadinfo col-xs-12 col-md-9 mb-4">
                 <%= link_to discussion_path(discussion), class: 'profile-link' do %>
                        <h6 class="small"><%= discussion.content  %></h6>
                    </div>
                <% end %>
             </div>

             <div class="actions">
                        <% if discussion.resolved == false %>
                        <%= link_to 'Answer', discussion_path(discussion), class: 'profile-link' %>
                        <% else %>
                        <%= link_to 'Contribute', discussion_path(discussion), class: 'profile-link' %>
                        <% end %>
             </div>

          </div>
        </div>
    <% end %>
<% end %>

<!-- Code for menu -->
<div class="row row-sidebar">
  <div class="col-12 mb-3 text-center">
  </div>

  <div class="col-12">
    <ul class="list-group">
      <% sidebar_elements.each do |element| %>
        <a class="list-group-item list-group-item-action" href="<%= element[:link] %>">
          <%= element[:name] %>
        </a>
      <% end %>
    </ul>
  </div>
</div>

侧边栏在清单文件中呈现为部分:

<% content_for :sub_layout do %>
  <div class="row mt-4">
    <div class="d-none d-md-flex col-3 align-items-start">
        <%= render "wads/sidebar" %>
    </div>

    <%= yield %>
  </div>
<% end %>

<%= render template: 'layouts/application' %>

因代码相同而非常困惑

2 个答案:

答案 0 :(得分:1)

当我尝试对您的部分erb代码进行相当格式化时,它似乎已被破坏。我想这可能是因为浏览器会尝试纠正破坏的HTML,因此结构可能会出错

<!-- This block seems broken -->
  <div class="wadinfo col-xs-12 col-md-9 mb-4">
      <%= link_to discussion_path(discussion), class: 'profile-link' do %>
             <h6 class="small"><%= discussion.content  %></h6>
         </div>
 <% end %><!-- The link_to block has an incorrect closing </div> -->
</div>

我认为在纠正之后,您的代码应该按预期工作。

答案 1 :(得分:0)

尝试将带有col-xs-12类的div放在不同的行中,即在带有row类的div中:

您的代码:

<div class="col-xs-12 col-md-9 mb-4">
    <div class="card">
        ...

固定代码:

<div class="row">
    <div class="col-xs-12 col-md-9 mb-4">
        <div class="card">
            ...