【问题标题】:full width responsive image with image tag in Rails 4 appRails 4应用程序中带有图像标签的全宽响应图像
【发布时间】:2016-08-05 10:26:25
【问题描述】:

我正在使用下面的代码使标题图像随机出现在我正在制作的应用程序中。我希望图像跨越屏幕的整个宽度,但不知何故它不起作用。

图片大小为宽1200px X height360px

我已将这一行 style: 'height:auto;width:100%;' 添加到图像标签中,希望它能使图像填满屏幕的宽度,但它没有成功。

我感觉有点迷失在这里,我错过了什么吗?还是我完全处于黑暗之中?

在我的类别/show.html.erb 我有这个代码

<header id="necklace_header" class="img-responsive">

  <img src="/assets/rand_headers/<%= @random_image%>", style: 'height:auto;width:100%;' >
    <h1>
      <%= @category.name %>
    </h1>
</header>

在 categories_controller.rb 我有

 def show
     @products = @category.products
     @images  = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"]
     @random_no = rand(5)
     @random_image = @images[@random_no]
  end

然后在 categories.scss 我有这个代码

#necklace_header {
    width: 100%;
    background-repeat:no-repeat;
    background-position: top center;

    background-size: cover;  

    height: 360px;
    margin-bottom: 20px; 

}

提前感谢,任何帮助将不胜感激

【问题讨论】:

  • style: 'height:auto;width:100%;' 替换为 style='height:auto;width:100%;' 。您将 Ruby 语法与 HTML 语法混合在一起。
  • 另外,这不是 Rails 问题,只是 HTML 和 CSS。这也取决于周围的 HTML 和 CSS; “100% 宽度”的含义取决于包含图像的内容。
  • 成功了@Arup Rakshit,谢谢伙计

标签: css ruby-on-rails ruby image random


【解决方案1】:

试试这个

#necklace_header {
    width: 100%;
 height: 360px;
background-image: url(your_image_path.jpg);
background-size:     cover;                      
background-repeat:   no-repeat;
background-position: center center; 

}

【讨论】:

  • 这不是随机的
猜你喜欢
  • 2015-01-19
  • 2016-03-03
  • 2014-12-30
  • 1970-01-01
  • 2014-02-27
  • 2013-03-27
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多