【问题标题】:I am having difficulty trying to strap a background url image solely to my home.html.erb on my RAILS 4.2.4 project我很难在我的 RAILS 4.2.4 项目中尝试将背景 url 图像单独绑定到我的 home.html.erb
【发布时间】:2016-01-20 16:14:45
【问题描述】:

在我的 RAILS 4.2.4 项目中尝试将背景 url 图片单独绑定到我的 home.html.erb 时遇到困难。

这个应该导入吗?

A) bootstrap_and_customization.css.scss

B) pages.scss

C) 应用程序.css

下面是我的代码。

body {
background: url('../images/japan.png');
}

【问题讨论】:

    标签: css ruby-on-rails-4 background


    【解决方案1】:

    你可以做的是为主页加载一个特定的样式表

    有很多方法可以做到这一点,我会在 application.html.erb 的 <head> 中使用这样的块

    <%= yield :head %>
    

    最好在顶部的 home.html.erb 中放置这样的块:

    <%= content_for :head do %>
      <%= stylesheet_link_tag "home" %>
    <% end %>
    

    这将在您插入yield :head的位置插入块的内容

    这样样式表只包含在主页上。

    现在对于图像,在 home.css.scss 中为身体添加你的 css:

    body {
      background: image-url('japan.png');
    }
    

    image-url 方法是一个辅助方法,它与资产管道一起工作。在开发中,它将插入带有app/assets/images/japan.png 的路径,而在使用编译资产的生产环境中,它将插入 japan.png 图像的编译资产路径

    【讨论】:

    • 请调用图像的语法在哪里,它在哪里。现在什么都没有加载
    • 请在 app/assets/stylesheets 中创建一个名为 home.css.scss 的文件,并将背景图片的 css 放在那里
    • 图像被称为 japan.png 这段代码在哪里。我对你的笔记感到困惑
    • 代码进入app/assets/stylesheets/home.css.scss
    • 感谢您的帮助和礼貌。我已经标记了答案。
    猜你喜欢
    • 2018-01-16
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    相关资源
    最近更新 更多