CSS condition for when there is no SRC in the image

5

I need that, when an image does not have the src attribute, css recognizes this automatically and hides that element with display:none

What selector should I use to do this?

Example:

 <img src="tem_imagem" class="image" />
 <img class="image" />
    
asked by anonymous 26.11.2015 / 12:01

3 answers

5

img[src=""], img:not([src]) {
  display: none;
}
<img src="">
<img src="https://www.google.com.br/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"><imgclass="teste" border="1">
    
26.11.2015 / 12:04
2

Use a attribute selector , and pseudo-class :not() :

img {
  width: 150px;
  height: 150px;
}
.no-src {
  background-color: #00f;
}
.empty-src {
  background-color: #0f0;
}

img[src=""],
img:not([src]) {
  display: none;
}
<img class="no-src"/>
<img src="" class="empty-src"/>
<img src="http://placehold.it/150x150"/>
    
26.11.2015 / 12:06
-2

Puts a none display in ALL of your imgs and the display block in those with src:

img[src] {
    display: block;
}
    
26.11.2015 / 12:52