background-position is removed on page load - html

I have a HTML page that tries to display some icons from a sprite image.
I added the css file, and also put the sprite image in the current working directory. For reference, one of the icon has the definition like as shown below,
.locicon{
background-position: -61px -110px ;
width: 20px;
height: 20px;
background: url(htsprite1.png) no-repeat;
}
Problem: However, when the page is loaded, the icons are not getting displayed.
When inspecting on chrome and firefox, I can see the sprite image, and this is the runtime definition of the class locicon :
.locicon{
width: 20px;
height: 20px;
background: url(htsprite1.png) no-repeat;
}
Everything except the background-position. Why is it happening like this?
I checked if this property is overriden somewhere and couldn't find any such instance while inspecting on the element.
Note: Before posting here, I even tried with a plain HTML file , including the css file, and tested, still the same issue .
background-position is getting removed at runtime!
Note: The Sprite wont appear in my case even after resolving this because of this linked issue, which is rectified now : Just for reference: CSS sprite not appearing in Firefox, but displaying in Chrome

You background-position is overwritten by background. Try to set the background-positionafterwards:
background: url(htsprite1.png) no-repeat;
background-position: -61px -110px;
A cleaner solution would be to set the background properties separately:
background-image: url(htsprite1.png);
background-repeat: no-repeat;
background-position: -61px -110px;

Related

Why does the background-image property in css not working?

So, I'm planning to give a div a background image.
Here's the path for the picture i want to use :
website\images\logo.png
And here's the path for the html and css file :
website/index.html
website/style.css
Here's the html code :
<header>
<div class="logo"></div>
</header>
Here's the css code :
header .logo {
width: 45px;
height: 40px;
background-image: url('../images/logo.png');
}
But the image still won't display in the background of the div and instead I get this error when i inspect and click the consol tab :
Not allowed to load local resource:
I also tried different variations such as :
background-image: url(../images/logo.png);
background-image: url(./images/logo.png);
background-image: url('./images/logo.png');
background-image: url('/images/logo.png');
background-image: url(/images/logo.png);
background-image: url('images/logo.png');
background-image: url(images/logo.png);
and all of them doesn't seem to work
I also tried to give the div a background color and it seems to work just fine.
What is the solution? Thank you
I think this might be the issue with getting source from localhost, if you are using VScode then try installing this extension called Live Server May be, it could fix the issue. take a look: https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer

Transparent background becomes white on image resize

I have an HTML page where I need to render 3 images, one on top of each other.
<div id="preview">
<img src="transparent-image">
</div>
with the following CSS
#preview{
background-image:url(layer0-image), url(layer1-image);
background-size:100%, 100px;
background-blend-mode: multiply;
width: 820px;
height: 350px;
}
The fist 2 images gets blended together using the CSS property background-blend-mode: multiply, on top of this I put the last image (transparent-image) with a transparent background (Imagine a photo of a room, with the wall area being transparent).
The result is exactly what I want but when I try to put
#preview{
width:100%
}
to make it responsive; It works flawlessly, but on ios the transparency is lost.
I tested this behaviour on Chrome, Safari and Firefox (iOS). No problem at all on Chrome, Safari, Firefox on OSX, Android, Windows.
Is there something I'm missing? Thanks for your time.
EDIT:
layer0-image and transparent-image are the same image, layer0-image has no transparency and gets multiplied with layer1-image then on top of this blended image I put transparent-image.
I tried to change transparent-image with another one, the problem seems to be the blending between layer0-image and layer1-image, but due to the fact that layer0-image and transparent-image are the same, at first I thought there was a transparency problem.
The transparency is there, but the div #preview has 0px height when put to
#preview{
width:100%;
height:auto;
}
Showing no blending.
EDIT2:
I'm adding an example to show and replicate the error: https://jsfiddle.net/dyqnghdo/3/
Ok, I finally found the problem. Having:
#preview{
background-image:url(layer0-image), url(layer1-image);
background-size:100%, 100px;
background-blend-mode: multiply;
width: 820px;
height: 350px;
}
and then setting:
#preview{
width:100%;
}
casused the error. To fix it I tried setting:
#preview{
width:100%;
background-size:cover, 100px;
}
The problem is gone even if it's not clear to me why it was happening.

Background image is not showing in IE and works fine with other browsers

Background image is not showing in IE and works fine with other browsers.
My line of code in CSS:
background: url(../images/box.png)no-repeat;
Add a space between image src and no-repeat.
url(../images/box.png) no-repeat
You should review background's shorthand implementation:
background: (color) url(../images/box.png) no-repeat (position-x) (position-y);
^^^^^
there needs to be
a space here too
I'd recommend moving to individual declaration if you aren't going to specify everything in the shorthand form of background, i.e.:
background-image: url(../images/box.png);
background-repeat: no-repeat;

Bootstrap CSS - Can't Get Background Color and Image Together

I'm using Bootstrap with Rails and can't get the brackground color and image to show up together. I have a custom CSS stylesheet where I'm trying to edit the body to have a dark background with a small image in the bottom right corner, like this:
body {
padding-top: 55px;
background-color: #2e2e2e;
background-attachment: fixed;
background-image: url('waves.png');
background-repeat: no-repeat;
background-position: right bottom;
}
Problem is that the image doesn't show up, just the background. If I remove the 'background-color' it shows up and looks perfect, but I can't get them together. I know I'm missing something small (probably stupid too). Appreciate the help.
Have you tried this way?
body {
padding-top: 55px;
background: #2e2e2e url(waves.png) right bottom no-repeat;
background-attachment: fixed;
}
make sure the file extension is .css.erb
body {
padding-top: 55px;
background: #2e2e2e url('<%= asset_path('waves.png') %>') fixed no-repeat right bottom;
}
Not sure about the environment you're in, but have you tried !important ? It adds priority in case there are styles with different values applied somewhere later in code.
body {
...
background-color: #2e2e2e !important;
background-image: url('waves.png') !important;
...
}
Try to put the waves.png file in /assets/images and replace url('waves.png') with this:
background-image: image-url('waves.png');
I'm assuming you're using assets pipeline and your CSS file is preprocessed by Sass (the stylesheet filename ends with .css.scss), for more information I suggest you to read the Asset Pipeline Rails Guide.

CSS style for attachments - icons before text automatically

I was wondering how hard it is to make a class for downloads that places an icon right before the text and both text and icon are part of the link to the attached file. Is there a simple way to do this with CSS?
Assuming that all links to downloads are a elements, of class-name 'downloadLink':
.downloadLink {
background-image: url(path/to/image.png);
background-position: 0 50%;
background-repeat: no-repeat;
}
If those links are of the format:
document.pdf
image.png
Then the css:
.downloadLink[href$=pdf] {
background-image: url(path/to/pdf_background_image.png);
background-position: 0 50%;
background-repeat: no-repeat;
}
.downloadLink[href$=png] {
background-image: url(path/to/png_background_image.png);
background-position: 0 50%;
background-repeat: no-repeat;
}
Will give specific backgrounds to links with a href attribute that ends with pdf or png. This approach is based around CSS3's attribute-selectors, which might reduce cross-browser compatibility but, according to Quirksmode's compatibility table not by much (basically everything above, and including, IE7 should support this (albeit it doesn't specify which of the selectors are supported) in IE or other browsers).
References:
attribute-ends-with $= selector.