2

I have tried to search online for an answer to this question, but to no avail. I'm following Michael Hartl's Ruby on Rails tutorial, chapter 5, and I went off on my own and made a new view page that uses the method "image_tag." However when I look at the view on localhost:3000 the image is not centered. How can I center the image using "image_tag"?

almel
  • 7,178
  • 13
  • 45
  • 58

3 Answers3

2

in Rails 3 you can simply use:

<%=image_tag("blahblah.png", :align=> "middle")%>

John Powel
  • 1,394
  • 3
  • 17
  • 30
1

image_tag is just a helper method to generate a <img> tag so you should probably use some CSS to center the image. Take a look a the API documentation for image_tag there is an example of how to add a class attribute to the generated image tag for example.

Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57
  • I'm pretty new to CSS and HTML. So would I do something like the following to get it centered; add a code line in the image_tag helper as follows """<%=image_tag("blahblah.png", :class => "main_photo")%>""" and then go to my CSS file and type """main_photo {align = center}""" – almel Aug 03 '12 at 22:16
  • Yes something like that. Use the view source function in your browser if you want to see what Rails generates for you. Maybe this questions http://stackoverflow.com/questions/7055393/css-center-image-using-text-align-center can help you with centering the image. Note that you should probably have a dot before "main_photo" to match on class (else you will match on tag name). – Mattias Wadman Aug 03 '12 at 22:59
1

In rails 5, I used (with Bootstrap)

    <%= link_to image_tag("image.jpg", class: "img-responsive center-block") %>