Rails 4 Gem for Centering Images and Content

screen shot of https://rubygems.org/gems/centering_helper

When I made the gem for generating column widths last week, it was so I could avoid having to litter my code with divs to center the columns if my database contained fewer columns of data than would fill the screen width.

So, of course, this week, I’ve made a gem so I don’t have to fill my code with divs to center anything!

With the gem installed, you can just put this before the content you want to center:

<%= start_centering %>

and put this after it:

<%= end_centering %>

Resulting in much more readable code:

<%= start_centering %>
  Whatever I want to center.
<%= end_centering %>

Which is soooo much nicer than:

<div class="outer_wrapper">
  <div class="inner_wrapper">
    <div class="element_wrapper">
      Whatever I want to center.
    </div>
  </div>
</div>

Creating it meant learning how to create an “asset gem,” which is easy … if you already know what you’re doing, but can be a bit of a challenge if you’re a noob trying to figure out how to do it for rails 4, which is not covered in the vast, vast majority of internet tutorials and stack overflow posts.

My next post will provide detailed instructions on how to make an asset gem for rails 4. For now, here’s where you can find the gem:

https://rubygems.org/gems/centering_helper

and here are the instructions for using it:

https://github.com/liantics/centering_helper

Skip to content