Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to set img src="about:blank"?

Tags:

html

image

tags

Background: I need to have an inline element to which I can apply width and height via CSS. AFAIK, img is the only way to have this behavior, currently.

I would rather not have the image point to a transparent pixel GIF on the server. It may be cached, but browsers queue it nevertheless, slowing down overall page speed. Other clients might not be caching at all.

PS No, floating div is not sufficient, it behaves differently from inline elements.

EDIT Sorry, I should have inserted the term "cross browser" somewhere. It must at least be working for FF≥2, IE≥7 and current Safari.

like image 988
PeterP Avatar asked Jan 18 '10 12:01

PeterP


People also ask

Can img src be empty?

The src attribute must be present, and must contain a valid URL referencing a non-interactive, optionally animated, image resource that is neither paged nor scripted. If the base URI of the element is the same as the document's address, then the src attribute's value must not be the empty string.

How do I know if my img src is valid?

src); img.To check if an img src is valid: Add an error event listener on the img element. If the src is invalid, set it to a backup image. Alternatively, hide the image.

Is IMG an empty element?

The img element is used to insert images into an HTML document. It is an empty tag (meaning it has no closing tag). The img element has two required attributes: src : The source location (URL) of the image file.

Is src an empty tag?

Answer. Yes it is an empty tag.


3 Answers

You could use the "data:" URI scheme to embed an image.

Other replaced elements might also work. Setting display to "inline-block" might also be worth looking into.

like image 54
outis Avatar answered Oct 06 '22 08:10

outis


Can you set:

display:inline-block;
width:50px;
height:10px;

IIRC, images are an "inline block" element, thus they can be rendered inline in text strings, but still have block-like properties.

like image 1
scunliffe Avatar answered Oct 06 '22 07:10

scunliffe


I guess it will be valid in the W3C validator sense, because the validator does not check whether the link is a resource or not.

However, valid in the broader sense, I would say it is not. An src attribute is required in the IMG tag, and I would say must point to a valid image resource.

I find outis`s "data: URI" idea the best way.

If that doesn't work, a transparent image is your best bet. It's one call, it's a few bytes at best, and will be cached by most clients.

like image 1
Pekka Avatar answered Oct 06 '22 06:10

Pekka