Image as background in span not rendering - html

I have the following class
.stats {
content: url('images/stats.gif');
background-repeat: no-repeat;
width: 23px;
height: 20px;
float: right;
}
and the following span
<span class="stats" ng-click="showTabDialog(player)"></span>
This renders fine in Chrome but I do not see the image in IE11 even though I can click in the place where the image should be and it works correctly
Why do I not see the image in IE?

Why do you have content: url('images/stats.gif')? The content CSS property only works with pseudo-elements ::before and ::after. I would suggest to try setting it as a background-image:
.stats {
background-image: url('http://onlywm.ru/vbfs_aBlackRed/misc/stats.gif');
background-repeat: no-repeat;
background-size: contain;
width: 23px;
height: 20px;
float: right;
}
<span class="stats" ng-click="showTabDialog(player)"></span>

content CSS property only works with the ::before and ::after pseudo-elements:
https://developer.mozilla.org/en-US/docs/Web/CSS/content
Chrome doesn't respect the specifications in that regard.
You should use background-image.

Add display-block to your css. The background-image CSS property only puts an image as the background. The width and height of an object is always defined either by static settings via CSS/inline styling, or by the actual size of the content displayed in it. In your case, since you haven't added any content between your tags, its x/y dimensions will be 0
.stats {
width: 23px;
height: 20px;
background-image: url('http://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg') ;
background-repeat: no-repeat;
display: block;
float : right;
}
JSFIDDLE

three mandatory attributes in the css for rendering the image (other than the background-image):
height: 40px; //change size as needed
width: 40px; //change size as needed
display: block; //Tend to miss this one out
Without height and width, if your asset takes time to render, the element will not be alloted an region space. Without display: block, the image only exists in the background. (tried too many things before discovering this subtle mistake).
Other attributes are optional and needed based on your requirement of positioning the image.

Related

why the google chrome css background images only show when the image span has content

I define a code block to show an icon in the source html page in google chrome extension like this:
<div id="translate-btn" style="">
<button type="button" class="bp3-button">
<span class="btn-icon">Translate</span>
</button>
</div>
when the user click the words on the original web page, show the icon. I changed the div visible or hidden in the javascript code. this is the css define:
#translate-btn {
position: absolute;
z-index: 9999999999;
left: 0;
top: 0;
width: 48px;
height: 48px;
}
#translate-btn .bp3-button {
padding: 2px;
min-width: 0;
min-height: 0;
width: 48px;
height: 48px;
}
#translate-btn .btn-icon {
width: 38px;
height: 38px;
background-image: url('chrome-extension://__MSG_##extension_id__/resource/image/logo.png');
background-size: contain;
}
.bp3-dark #translate-btn .btn-icon {
width: 38px;
height: 38px;
background-image: url('chrome-extension://__MSG_##extension_id__/resource/image/logowhite36.png');
}
#translate-btn.show {
display: block;
}
now I found the problem is that the span element added Translate words, the background image show with the words Translate, when did not add the Translate word, the background image did not show. why did this happen? what should I do to fix this problem to make the image always show no matter add the Translate or not?
Why no background-image
span is a generic inline container. In this case, it practically means that it'll collapse when there's no content inside. That is, background-image doesn't have any space to fill.
How to address
Should you want the background image always visible without any content inside, you could consider:
Adding it to the parent button element
Using a block level container for .btn-icon
Also, You could reconsider the use of background-image altogether. Is it really a background? Why can't it be an img instead? Or perhaps a Base64 encoded inline content?

Image is loaded but not showing in localhost application on firefox

I already googled this problem and I found several answers. Most of them recommended to clean cache and cookies, which I did but it didn't work.
I want to display an image in my application. When I check the developer settings in Firefox, my image is loaded, but it doesn't show. It also says, its content is 0x20, even if I set the width and height to 100px and 80px. I even tried with the !important statement, but it still shows 0x20.
In Google Chrome it works totally fine but in Firefox it doesn't.
This problem actually seems pretty easy to solve but I really can't find out why it won't work and I tried so many options but none of them seem to work.
Any ideas?
EDIT
html
<i class="icon"></i>
css
.icon {
width: 100px;
height: 80px;
content: url(../imgs/icon.png);
}
You try to add a content to some element.
content is only supported by :before and :after. See W3C.
And <i> is an inline element, for giving it width/height through css add it as inline-block.
Change your CSS to the following should work, maybe add a display: inline-block;
.icon:before {
width: 100px;
height: 80px;
content: url(../imgs/icon.png);
}
This will change the size of the before, only when using responsive SVGs the image size will change.
I prefer to use a background approach:
HTML:
<i class="icon"></i>
CSS:
.icon {
display: inline-block;
width: 100px;
height: 80px;
}
.icon:before {
display: block;
content: "";
width: 100%;
height: 100%;
background: url(//via.placeholder.com/150x75) no-repeat;
background-size: contain;
}
This will change the image according to the container size. With background-size: contain the proportions are preserved. A content is needed when using before.
Try out.

How can I make my SVG show up in the <i> tag with no content?

I am trying to make my svg show up as a background image in my <i> tag. I'm using the <i> tag for the sake of uniformity with my other code.
If I do not include any content inside the i tag, the svg background-image fails to show up at all. I have tried setting height and width of the <i> tag and that has not worked. I understand you could easily do this with a div but I am trying to make it work the same with this.
HTML
<div class="card-panel">
<i class="icon-advanced"></i>
</div>
CSS
.icon-advanced {
background: url('icon/icon-advanced.svg');
background-repeat: no-repeat;
}
Try adding display: inline-block; before setting the height and width
.icon-advanced {
background: url('icon/icon-advanced.svg');
background-repeat: no-repeat;
background-size: cover; /* stretch the background to cover the whole element */
/*
still inline, but has block features
meaning height and width can be set
*/
display: inline-block;
height: 20px;
width: 20px;
}

How to remove the border of image when it is shown as unavailable?

Is there a way to remove the border when an image is not yet loaded but the height and width are set?
I am creating a lazy loading so the icon will be centred inside as a background image, until image loads.
http://codepen.io/anon/pen/bgoqYv
same code in the snippet also
body {
padding: 50px;
}
img {
height: 200px;
width: 200px;
/* border: 0 !important; */
background-image: url(https://i0.wp.com/cdnjs.cloudflare.com/ajax/libs/semantic-ui/0.16.1/images/loader-large.gif);
background-repeat: no-repeat;
background-position: 50% 50%;
}
<img src="http://www.site/nonexistant.jpg" />
You can't style the "broken image". But you can work around it. For example you can replace the broken image with some little javascript:
<img src="idonotexist.jpg" onerror="this.src='trans.png';">
Of course you can write it cleaner in js instead inside the img tag.
Alternatively, you can work with pseudo classes to hide the broken image etc. Have a look here: https://bitsofco.de/styling-broken-images/

How make a div container responsive?

I have a div with a background-image assigned in the CSS3 file.
The image is responsive, so it scales according to the screen size BUT the container keeps the height at all screen sizes.
I need to know if there is a way to make the container responsive as well as the background image.
HTML:
<div class="responsive> </div>
CSS3:
.responsive {
background: url('https://s20.postimg.org/o09gf7fvx/bag.jpg') no-repeat center top;
border: 1px solid red;
background-size: contain;
width: 100%;
height: 270px;
}
I must use background-image selector and no img src tag.
Here is the fiddle file.
Thank you.
Update - February 3rd, 2021
Since I wrote the original answer a new CSS property has been introduced - 'aspect-ratio' - to solve this problem.
<div id="responsive">some text</div>
CSS:
#container {
width: 100%;
background: hotpink;
aspect-ratio: 100 / 29;
}
At the time of writing this CSS property doesn't yet have widespread browser support.
Working Example: https://jsfiddle.net/fu0nL57t/
Ref: https://web.dev/aspect-ratio/
=====================================================
Original Answer
This can be done an additional dummy element, inside the element you want to keep at a fixed ratio. If you specify a padding-top or padding-bottom as a percentage, that is in terms of the width of the container element, and this then keeps the height of the container element at a fixed ratio.
<div id="responsive">
some text
<div id="dummy"></div>
</div>
CSS:
#responsive {
display: inline-block;
position: relative;
width: 100%;
background: url('https://s20.postimg.org/o09gf7fvx/bag.jpg') no-repeat center top;
background-size: contain;
}
#dummy {
padding-top: 29%;
}
Working Example:
https://jsfiddle.net/098jj61q/
Credits:
http://ansciath.tumblr.com/post/7347495869/css-aspect-ratio
http://alistapart.com/article/creating-intrinsic-ratios-for-video
Yes its correct. According to #Paulie_D, you can't do that with background image.As per your requirement you can do that using img tag only.
What you have to do, without using the div just make the image responsive by treating it as a block element as,
.img-responsive {
display: block;
max-width: 100%;
height: auto;
}
or if you insist to use division with background image then cover the backgound image and set min-height as,
div.mydiv {
background:url(background_image.jpg);
background-repeat:no-repeat !important;
background-size: cover !important;
background-position:center center !important;
min-height:300px;
}