Craiginwlaes
Craiginwlaes

Reputation: 39

Specify width in link_to image_tag

I am using the following code to link to logo on the nav bar:

 <%= link_to " #{image_tag ("logo3.svg")}".html_safe, root_url %>

I need to specify the width of the logo, but have got a little confused. Tried in/out of the brackets, with a trailing comma - breaks every time.

Any ideas?

Upvotes: 1

Views: 2951

Answers (5)

hgsongra
hgsongra

Reputation: 1484

You can use the image_tag as it define here in link_to tag

Ex:

 <%= link_to image_tag("logo3.svg", height: 'you-height', width: 'your-width'), root_url %>

Hope you get the more idea about it.

Upvotes: 1

Qaisar Nadeem
Qaisar Nadeem

Reputation: 2434

You don't need to add image tag in quotes. simply do it like this

<%= link_to image_tag("logo3.svg",width: 500,class: 'your_class'), root_url %>

There is another way to do the same.

<%= link_to root_url do %>
 <%=image_tag("logo3.svg",width: 500)%>
<%end%>

Upvotes: 1

monsur
monsur

Reputation: 601

Basically you can add a class name and then add css rules for width like below

 <%= link_to " #{image_tag ("logo3.svg")}".html_safe, root_url, :class  => "my-logo" %>

Then in your css file add something like below:

app/assets/stylesheets/some_file.css

.my-logo {

  width: your-image-width;

}

Those solutions would work fine but you should avoid writing inline css that is best practice. Hope that helps.

Upvotes: 0

Hasmukh Rathod
Hasmukh Rathod

Reputation: 1118

Try this:

 <%= link_to " #{image_tag ("logo3.svg",  height: '32', width: '32')}".html_safe, root_url %>

Upvotes: 0

Szymon
Szymon

Reputation: 252

Try out this:

<%= link_to " #{image_tag ("logo3.svg", size: "16x10")}".html_safe, root_url %>

Obviously you can customize size - i gave it as an example

Upvotes: 0

Related Questions