49

我有一个图像标签如下:

    <%= image_tag @user.photo.url(:large) %>

如何在 CSS 中为该图像的一侧设置边框颜色?那会产生什么html?

4

6 回答 6

82

通过使用“样式”选项:

<%= image_tag @user.photo.url(:large), :style => "border: 1px solid red" %>

如需更多信息,请查看 API。

于 2012-04-17T05:23:21.767 回答
55

将类或 id 添加到您的 image_tag:

<%= image_tag @user.photo.url(:large), :class => "style_image" %>

然后使用 css 对其进行样式设置:

.style_image {
  border-right: 1px solid #000;
}
于 2012-04-17T05:22:49.187 回答
13

您也可以提供内联样式

<%= image_tag @user.photo.url(:large), :style => "float :left;border 1px solid #00000;" %>

尝试一下.....

于 2012-04-17T05:31:19.270 回答
12

在较新版本的 Rails 中,有一种“更好”的方式:

<%= image_tag('filename_in_public_folder', alt: 'Description', style: 'display: block; margin: auto; width: 40%;')%>
于 2016-08-05T14:19:00.417 回答
6

这是一种简单的方法。

<%= image_tag("example.png", :style => 'border-right: 1px solid #000;')%>
于 2015-06-11T19:15:18.233 回答
2

对于在 Google 上遇到此问题的其他人。如果您以不同的方式使用图像标签,您仍然可以向其添加 css 类,但这只是语法不同。有时您需要使用外部服务器。(在 Heroku 上部署?=> Amazon s3)

<%= image_tag("https://s3-us-west-2.amazonaws.com/mybucketname/user_photo.png", class: "style_image") %>
于 2015-02-20T07:23:01.813 回答