Rails: How to set a background image in rails from css?
In your CSS:
background-image: url(background.jpg);
or
background-image: url(/assets/background.jpg);
In environments/production.rb
:
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
How to add background image in Ruby on Rails
Try in your css:
#boxy{
background: image-url('background1.jpg') no-repeat;
}
Or directly in your view:
<%= image_tag('background1.jpg', style: "height: 30px; width: 30px") %>
add background image with SCSS in Ruby on Rails
When working with CSS files and images you have two options url
and path
:
image-url("logo.png") # url(/assets/logo.png)
image-path("logo.png") # "/assets/logo.png"
If you want to work with images, font, video, audio, JavaScript and/or stylesheet files then you can use the generic form
asset-url("logo.png") # url(/assets/logo.png)
asset-path("logo.png") # "/assets/logo.png"
See the ActionView::Helpers::AssetUrlHelper
for further information.
Adding a background image in Ruby on Rails 2 in CSS
The webserver in rails takes public
folder as the base of the application. Hence its looking for the specific image under /public/images/
instead of app/assets/images/"dep.jpg"
& since its not there over there you cannot get the image. Try to put your image in /public/images/
folder then it would work.
Edit: If you are using rails 3.1 then this would be different as Rails 3.1 uses the concept of asset pipeline for the assets of your application so then then path app/assets/images/dep.jpg
would obviously work.
How to add a background image inline style to link tag Rails
You can do something like this:
:style => "background-image: url(#{image_path "avatar.png"})"
To find more about image_path.
Related Topics
Vertically Center Rotated Text with CSS
How to Make Content Appear Beneath a Fixed Div Element
How to Use the Matrix Transform and Other Transform CSS Properties
How to Convert .Svg Files to a Font
Allow a Div to Cover the Whole Page Instead of the Area Within the Container
Differencebetween the Initial and Unset Values
Using CSS to Transition the Fill Property of an Svg Path on Hover
Removing the Ie10 Select Element Arrow
CSS Div Width Percentage and Padding Without Breaking Layout
Center the Nav in Twitter Bootstrap
How to Expand a Child Div to 100% Screen Width If the Container Div Is Smaller
How to Hide Anchor Text Without Hiding the Anchor
How to Override Z-Index Inheritance from Parent Element
Border-Image: Workaround for Ie
Why Do I Need an Empty 'Content' Property on an ::After Pseudo-Element