CSS background-image - What is the correct usage?
The path can either be full or relative (of course if the image is from another domain it must be full).
You don't need to use quotes in the URI; the syntax can either be:
background-image: url(image.jpg);
Or
background-image: url("image.jpg");
However, from W3:
Some characters appearing in an unquoted URI, such as parentheses, white space characters, single quotes (') and double quotes ("), must be escaped with a backslash so that the resulting URI value is a URI token: '\(', '\)'.
So in instances such as these it is either necessary to use quotes or double quotes, or escape the characters.
When to use IMG vs. CSS background-image?
Proper uses of IMG
- Use
IMG
if you intend to have
people print your page and you want the image to be included by default.
—JayTee - Use
IMG
(withalt
text) when the image has an important semantic meaning, such as a warning icon. This ensures that the meaning of the image can be communicated in all user-agents, including screen readers.
Pragmatic uses of IMG
- Use
IMG
plus alt attribute if the image
is part of the content such as a logo or diagram or person (real person, not stock photo people).
—sanchothefat - Use
IMG
if you rely on browser scaling to render an image in proportion to text size. - Use
IMG
for multiple overlay images in IE6. UseIMG
with az-index
in order
to stretch a background image to fill its entire window.
Note, this is no longer true with CSS3 background-size; see #6 below.- Using
img
instead ofbackground-image
can dramatically improve performance of animations over a background.
When to use CSS background-image
- Use CSS background images if the
image is not part of the content.
—sanchothefat - Use CSS background images when
doing image-replacement of text eg. paragraphs/headers.
—sanchothefat - Use
background-image
if you intend to have
people print your page and you do not want the image to be included by default.
—JayTee - Use
background-image
if you need to improve download times, as
with CSS sprites. - Use
background-image
if you need for only a portion of the image to be visible, as with CSS sprites. - Use
background-image
withbackground-size:cover
in order to stretch a background image to fill its entire window.
HTML CSS best way to use background image
You could use linear-gradient
with color stops to create bands like that. The syntax is simple (explained in inline code comments below):
background-image: linear-gradient(to right, /* gradient from left to right */
#f00, #f00 25%, /* start with red, end with red at 25% */
#00f 25%, #00f 50%, /* blue at 25% continue up to 50% */
#0f0 50%, #0f0 75%, /* green at 50% continue up to 75% */
#000 75%, #000 100% /* black at 75% continue up to 100% */
);
To keep it simple, in the example below there are two div
s for the bands and a middle div
for the content. You can then take it to next level by using ::before and ::after
pseudo-elements on the content and eliminate separate div
s for the bands.
Example Fiddle: http://jsfiddle.net/abhitalks/nve9v0mn/1/
Example Snippet:
div.line { height: 6px; background-image: linear-gradient(to right, #f00, #f00 25%, #00f 25%, #00f 50%, #0f0 50%, #0f0 75%, #000 75%, #000 100% );}div.content { min-height: 60vh; background-color: #eee;}
<div class="line"></div><div class="content"></div><div class="line"></div>
What is the correct path to use for a background image in a css file in a Rails application that takes advantage of the asset pipeline?
Both PATHS are correct to use in css.
With reference to rails 3.2.13.Sass-Rails
internally uses asset-url's
logic same as asset_path
(rails helper method). And image-url
calls rails image_path
helper method which again calls asset_path
of rails helper method. So both methods are just calling asset_path
of rails helper and you can use either one. So there is only syntax difference.
P.S.- Any feedback or input is most welcome.
Related Topics
Equal Height Rows in CSS Grid Layout
Css '≫' Selector; What Is It
Does the Order of Classes Listed on an Item Affect the Css
Css Opacity Only to Background Color, Not the Text on It
How to Conditionally Apply a Class
Why Don't :Before and :After Pseudo Elements Work With 'Img' Elements
How to Conditionally Apply CSS Styles in Angularjs
Column Order Manipulation Using Col-Lg-Push and Col-Lg-Pull in Twitter Bootstrap 3
Rotated Elements in CSS That Affect Their Parent'S Height Correctly
Css Attribute Selectors: the Rules on Quotes (", ' or None)
Specificity of Inherited CSS Properties
How to Identify Unused CSS Definitions from Multiple CSS Files in a Project
Which CSS Properties Are Inherited
How to Prevent CSS Inheritance
How to Change Whole Page Background-Color in Angular