admin管理员组文章数量:1415145
I am loading an image through css background
property , instead of using src
attribute. But in that case the alt
text is also showing up. How can I stop showing alt
text & show it only if image is not loaded
.test {
display: block;
width: 200px;
height: 82px;
background-size: 200px 82px;
background-image: url(":ANd9GcQJr81CRJeZGFiBsA9_AOyyxegiIPctdcbNfHThnpnclmFH-mJwoQ");
}
<img class="test" alt="My background image">
I am loading an image through css background
property , instead of using src
attribute. But in that case the alt
text is also showing up. How can I stop showing alt
text & show it only if image is not loaded
.test {
display: block;
width: 200px;
height: 82px;
background-size: 200px 82px;
background-image: url("https://encrypted-tbn0.gstatic./images?q=tbn:ANd9GcQJr81CRJeZGFiBsA9_AOyyxegiIPctdcbNfHThnpnclmFH-mJwoQ");
}
<img class="test" alt="My background image">
Share
Improve this question
asked Sep 6, 2017 at 5:40
brkbrk
50.4k10 gold badges59 silver badges84 bronze badges
5
- Try to use div tags other than img tag to avoid showing alt text. Better to use img tag incase of image failure... – Nandakumar Commented Sep 6, 2017 at 5:43
- Check the answer here stackoverflow./questions/4216035/… – Ovidiu Unguru Commented Sep 6, 2017 at 5:46
- @brk Could you tagged jquery in your question? – Ataur Rahman Munna Commented Sep 6, 2017 at 5:55
-
@brk, If an image is loaded or not, you can check it with image
naturalHeight
andnaturalWeight
. but if your url is invalid then it also returned a image with 1px dot(1x1)
[for only your image hosting url]. So you should check if the imagenaturalHeight
is greater than 1 for image loaded properly.Then you can removed alt attribute value. See the jsfiddle. jsfiddle/ataur63/4w5aywzc – Ataur Rahman Munna Commented Sep 6, 2017 at 6:54 -
naturalWeight
should benaturalWidth
. My mistake in typo. :) – Ataur Rahman Munna Commented Sep 6, 2017 at 7:02
2 Answers
Reset to default 2You can do a little trick and hide the "text" inside the image if the image has no src
attribute (or its empty).
(You can hide the text in many ways I choose one)
.test {
display: block;
width: 200px;
height: 82px;
background-size: 200px 82px;
background-image: url("https://encrypted-tbn0.gstatic./images?q=tbn:ANd9GcQJr81CRJeZGFiBsA9_AOyyxegiIPctdcbNfHThnpnclmFH-mJwoQ");
}
img:not([src]) {
font-size: 0;
position: relative;
}
img:not([src]):after {
font-size: 18px;
border: 1px solid black;
box-sizing: border-box;
content: attr(alt);
z-index: -1;
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0
}
<img class="test" alt="My background image">
The pletion for the css (the :after
part) received from @Ihazkode.
For displaying the alt
after the image loading you can load (with javascript) the image first, then, put it in the image and display the alt
. (Based on this answer)
$('[data-image]').each(function() {
var $this = $(this);
var alt = $this.attr('alt');
var src = $(this).attr('data-image');
$this.removeAttr('alt');
$('<img/>').attr('src', src).on('load', function() {
$(this).remove();
// simulate the delay for heavy image
setTimeout(function(){
$this.css('background', 'url(' + src + ')').attr('alt', alt);
}, 1000);
}).on('error', function() {
$this.attr('alt', alt);
});
});
.test {
display: block;
width: 200px;
height: 82px;
background-size: 200px 82px;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img class="test" alt="My background image" data-image="https://encrypted-tbn0.gstatic./images?q=tbn:ANd9GcQJr81CRJeZGFiBsA9_AOyyxegiIPctdcbNfHThnpnclmFH-mJwoQ">
First of all I need to know, is there any purpose of setting background image to <img>
tag?
If yes, there is no point to use <img>
tag here user <div>
or any other tags instead of using <img>
.
As per the W3C standard tag should contain alt="some text"
, if <img>
source not found.
本文标签: javascriptHow to prevent showing image alt text if image is loaded using cssStack Overflow
版权声明:本文标题:javascript - How to prevent showing image alt text if image is loaded using css - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745214135a2648061.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论