如何在 Ruby on Rails 中设置图像标签的样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10185516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to style an image tag in Ruby on Rails
提问by Elias7
I have an image tag as follows:
我有一个图像标签如下:
<%= image_tag @user.photo.url(:large) %>
How could I style a border color to one side of that image in CSS? What html would that produce?
如何在 CSS 中为该图像的一侧设置边框颜色的样式?那会产生什么html?
回答by u445908
by using "style" option:
通过使用“样式”选项:
<%= image_tag @user.photo.url(:large), :style => "border: 1px solid red" %>
for further info, please check the API.
如需更多信息,请查看 API。
回答by DanS
Add a class or id to your image_tag:
将类或 id 添加到您的 image_tag:
<%= image_tag @user.photo.url(:large), :class => "style_image" %>
Then use css to style it:
然后使用 css 对其进行样式设置:
.style_image {
border-right: 1px solid #000;
}
回答by Kashiftufail
you can also give inline style as well
您也可以提供内联样式
<%= image_tag @user.photo.url(:large), :style => "float :left;border 1px solid #00000;" %>
Try it.....
尝试一下.....
回答by Matt
In newer versions of Rails, there is a "better" way:
在较新版本的 Rails 中,有一种“更好”的方法:
<%= image_tag('filename_in_public_folder', alt: 'Description', style: 'display: block; margin: auto; width: 40%;')%>
回答by Patricia Hung
This is a simple way to do it.
这是一个简单的方法。
<%= image_tag("example.png", :style => 'border-right: 1px solid #000;')%>
回答by camdixon
For others who come across this on Google. If you use an image tag in a different way, you can still add css class to it, but it's just different syntax. Sometimes you need to use an external server. (Deployment on Heroku? => Amazon s3)
对于在 Google 上遇到此问题的其他人。如果您以不同的方式使用图像标签,您仍然可以向其添加 css 类,但只是语法不同。有时您需要使用外部服务器。(在 Heroku 上部署?=> Amazon s3)
<%= image_tag("https://s3-us-west-2.amazonaws.com/mybucketname/user_photo.png", class: "style_image") %>

