【问题标题】:CSS background image not showing up on rails siteRails:CSS背景图像未显示在rails网站上
【发布时间】:2012-05-19 08:48:26
【问题描述】:

我正在尝试将 background-image css 属性与我正在使用 Ruby on Rails 构建的网站一起使用。

我尝试了几种不同的 background-image: url、background: url、带引号和不带引号以及图片的几种路径。图片当前位于 app/assets/images/upload.png。

background: url(upload.png);

custom.css.scss

/* UNIVERSAL */

body {
background-color: black;
padding-top: 8px;
width: 800px;
margin-left: auto;
margin-right: auto;
font-family: arial, sans-serif;
font-size: 16px;
}

.content {
background-color: white;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
-khtml-border-radius: 10px;
border-radius: 10px;
padding: 1px 15px;
}

h2 {
font-size: 18px;
}

p {
font-size: 16px;
}   

ol {
font-size: 16px;
}


/* NAVIGATION */

ul {
padding: 0px;
}

#navtop {
float: right;
display: inline-block;
position: relative;
bottom: 47px;
right:4px;
letter-spacing:1px;
}

#topnavcontainer {
margin-bottom: -40px;

}

#navtop li, #navbottom li, #navbuttons li {
display: inline;
margin: 0px;
padding: 0px;
}

#navbuttons {
word-spacing: 25px;
position: relative;
left: 400px;
bottom: 60px;
display: inline-block;
}

#navbottom {
text-align: center;
word-spacing:90px;
letter-spacing:1px;
margin-top:25px;
}

#navtop a:link, #navbottom a:link, #navtop a:visited, #navbottom a:visited  {
text-decoration: none;
font-size: 12px;
color: #999999;
}

#navtop a:hover, #navbottom a:hover {
color: white;
}

#uploadbutton a:link, #uploadbutton a:visited, #viewbutton a:link, #viewbutton a:visited  { 
text-decoration: none;
border-style:solid;
border-width:3px;
border-color:#666666;
padding: 5px;   
color: white;
font-size: 14px;
font-weight: bold;
letter-spacing:2px;
}

#uploadbutton a:hover {
color: #FF0000;
}

#viewbutton a:hover {
color: #0066FF;
}   

/*style the main menu*/
.myMenu {
margin:0;
padding:0;
text-align: left;
}

.myMenu li {
list-style:none;
float:left;


}

.myMenu li a:link, .myMenu li a:visited {
display:block;
text-decoration:none;

padding: 0.5em 1em;
margin:0;


}

.myMenu li a:hover {
background-color:black; 
}

/*style the sub menu*/
.myMenu li ul {
position:absolute;
visibility:hidden;
margin:0;
padding:0;
}

.myMenu li ul li {
display:inline;
float:none;
}

.myMenu li ul li a:link, .myMenu li ul li a:visited {
background-color:black;
width:55px;
}

.myMenu li ul li a:hover {
background-color:#999999;
}

/* HOME PAGE */

#homepage {
text-align: center;
}

#homeheadline {
color: white;
background-color: #333333;
font-weight:normal;
border-style:solid;
border-width:6px;
border-color:#333333;
letter-spacing:1px;
margin-bottom: 45px;
}

a#clickhere , a#clickhere:visited {
text-decoration: none;
color: #0066FF;
}

#videotitle {
color: #FF0000;
position: absolute;;
bottom: 70px;
display: inline;
font-size: 19px;
left: 60px;
}

#videolist {
position: absolute;
left: 40px;
display: inline-block;
}

#audiencetitle {
color: #0066FF;
position: absolute;
bottom: 70px;
left: 340px;
display: inline;
font-size: 19px;
}

#audiencelist {
position: absolute;
display: inline-block;
left: 320px;
}

a.greenbutton, a.greenbutton:visited, {
font-size: 14px;
color: white;
font-weight: bold;
text-decoration: none;
background-color: #009900;
border-style:solid;
border-width:7px;
border-color:#009900;
letter-spacing:1px;
}

#homead {
margin-bottom: 25px;
margin-top: 45px;
display: block;
}

#lists {
width: 538px;
height: 100px;
position: relative;
margin-bottom: 35px;
margin-left: auto;
margin-right: auto;
text-align: left;
}

/* FORMS */

.greenbutton {
font-family: arial, sans-serif;
font-size: 14px;
color: white;
font-weight: bold;
text-decoration: none;
background-color: #009900;
border-style:solid;
border-width:3px;
border-color:#009900;
letter-spacing:1px;
margin-bottom: 40px;
}

.textinput {
border: 1px solid #bbb;
height: 18px;
width: 300px;
margin-bottom:30px;
}

.signform {
text-align: center;
width:300px;
margin-left: auto;
margin-right: auto;

}

.signformfields {
text-align: left;
}

#error_explanation {
color:#f00;
font-size: 16px;
ul { list-style: none;
   margin: 0 0 18px 0;
   }
}

.field_with_errors {
 @extend .control-group;
 @extend .error;
 }

/* Upload Menu */

#uploadpage {
height: 580px;

}
.uploadnav ul {
list-style-type: none;
}

.uploadnav {
width: 200px;
text-align: center;
float:left;


}

.uploadbox {
background-color: black;
background-image: url( image_path ("upload.png"));
border-style:solid;
border-width:25px;
border-color: black;
margin-top: 20px;

}

.uploadgreybox {
background-color: #CCCCCC;
border-style:solid;
border-width:25px;
border-color: #CCCCCC;
margin-top: 20px;
}

.uploadtext {
color: white;
background-color: black;
padding: 5px;
}

.uploadgreytext {
color: #666666;
background-color: #CCCCCC;
padding: 5px;
}



.uploadpagecontent {

float:right;
}

【问题讨论】:

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


    【解决方案1】:

    最正确的方法:

    background-image: url(<%= asset_path 'upload.png' %>)
    

    有关详细信息,请参阅指南:http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets(第 2.2.1 节 CSS 和 ERB)

    注意:如果您在 application.css 中执行此操作,则将 .erb 扩展名添加到它以成为 application.css.erb

    【讨论】:

    • Rails 在 url() 中提供了什么路径?尝试通过localhost:3000/the_path访问它
    • 我无法理解这个......我对 Rails 很陌生,所以请耐心等待。我现在不断收到语法错误。我将“custom.css.scss”文件更改为“custom.css.scss.erb”。自从我在“background-image: url()”中发布以来,我一直收到语法错误。这是正确的道路吗?
    • 你没有提到使用 SCSS。在这种情况下使用:background-image: url(image_path('upload.png'));。此处无需更改扩展名!
    • 它对我有用:background-image: url(image_path("upload.png"));。我刚刚从您的代码中删除了空格。
    【解决方案2】:

    如果有人过来并仍在寻找答案,而您正在使用 SASS 和引导程序,您可以尝试

    background: image-url('bgImage.jpg');
    

    这对我有用。你也可以看看 jdoe 提到的http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

    【讨论】:

      【解决方案3】:

      您需要正确指向图片路径。应该是:

      background: url(/assets/url.png);
      

      尝试使用 Google Chrome 中的检查器 - 对于检查此类由 CSS 调用的资源很有用。

      【讨论】:

        【解决方案4】:

        我也遇到过类似的问题,发现下面的代码有效

        background-image: url('/assets/xyz.jpg');
        

        【讨论】:

          【解决方案5】:

          您可能需要更改图像的路径。与 images 文件夹相关的 css 文件在哪里,例如 app/assets/css/style.css。

          如果它在这里,那么您的 upload.png 路径将为 background: url(../images/upload.png); 背景:url(/images/upload.png);background: url(images/upload.png);

          【讨论】:

          • @trickeedickee,作者说他使用资产文件夹,因此他必须使用管道助手来指定资产的路径。否则样式中的路径将不会在生产中转换为在其名称中使用摘要。
          【解决方案6】:

          您不必更改 custom.css.scss 的扩展名,或者重要的是无需将 erb 添加到 scss 文件中

          在 /assets/stylesheets 目录中的 application.css 文件中 你只需要一个元素 html{ 背景:url('/images/test.jpg'); 文本对齐:居中; 颜色:#fff; }

          拥有你的css文件,移动

          【讨论】:

          • 我刚刚发布了我的 css 文件。有什么想法吗?
          【解决方案7】:

          试试

          background: white;z-index:-1;
          

          【讨论】:

            猜你喜欢
            • 2015-10-30
            • 2017-05-27
            • 1970-01-01
            • 2015-12-10
            • 1970-01-01
            • 1970-01-01
            • 2015-07-02
            • 1970-01-01
            • 2011-11-07
            相关资源
            最近更新 更多