Column layout


At this point, this is what my (and, most likely, your) Posts Index page looks like:

This is nice, but in most applications we would to display the content on the index page in a more compact form. For this, we will be using a Bootstrap grid.

Three columns on large devices

Since Bootstrap's grid size is 12 columns and we want to display 3 items in a single row, the width of one item should be 4 Bootstrap columns.

Let's wrap our panel in a div with a size of 4 columns of content for large devices. Go into the app/views/posts/index.html.erb and wrap the <div class="panel panel-default"></div> in a <div class="col-lg-4"></div> set of tags.

Also add btn btn-default btn-xs pull-right classes to your image_tag so that images fit nicely into the panel box and change their size correspondingly depending on the screen resolution.

<%= image_tag(post.image_link, width: 600, class: "img-responsive img-thumbnail") %>

This is what I got:

<div class="container">
  <h1>Posts</h1>
  <ul>
    <% Post.all.each do |post| %>
      <div class="col-lg-4">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><%= post.title  %></h3>
          </div>
          <div class="panel-body">
            <p><%= post.description %></p>
            <%= image_tag(post.image_link, width: 600, class: "img-responsive img-thumbnail") %>
          </div>
          <div class="panel-footer">
            <span class="label label-default"><%= "Created by: #{post.user.try(:email)}"%></span>
            <% if current_user == post.user %>
              <%= link_to(edit_post_path(post), class: "btn btn-default btn-xs pull-right") do %>
                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Edit
              <% end %>
              <%= link_to(post_path(post), method: :delete, class: "btn btn-danger btn-xs pull-right") do %>
                <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete
              <% end %>
            <% end %>
          </div>
        </div>
      </div>
    <% end %>
  </ul>
</div>

Two columns on tablets

If we resize the window to what would be the size of a tablet screen, we see that Bootstrap will snaps to a different layout:

We would like instead a column layout of two columns of content for medium-sized screen. Since we want two columns of content on a tablet and the size of a Bootstrap row is 12 columns, we'll want 6 columns for each of our posts. Let's edit the line:

<div class="col-lg-4">

And set the column size to 6 for medium devices:

<div class="col-lg-4 col-md-6">

The result looks like this:

One column on phone screens

On extra small devices, we would like one single item to take the full width of the screen, so we will allow it to fill 12 columns. On small devices we will display two columns of content per row.

This is achieved by changing:

<div class="col-lg-4 col-md-6">

to

<div class="col-lg-4 col-md-6 col-sm-6 col-xs-12">

results matching ""

    No results matching ""