Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image height and width when image not found

Tags:

html

css

php

I have the following code:

<div>
  <img src="../images/curry.jpg" height="242" width="300" 
       alt="Can't find image (http://localhost/images/curry.jpg)">
</div>

I am trying to precisely lay out a page so, even if the image is not found, I would like the Alt text to occupy the given height & width.

1) is that now how HTML/CSS works?

2) if not, since I generating the HTML from PHP, is there anything I can do to achieve what I want? For instance, would it work if I check for non-existence and instead of an IMG tage, generate a Fieldset of the desired size, containing my Alt text?

like image 662
Mawg says reinstate Monica Avatar asked Feb 24 '23 09:02

Mawg says reinstate Monica


2 Answers

The answer to you first question is Yes. Alt attribute is for SEO purposes, and behaves as you have seen it.

It is possible although I must warn you of additional overhead, there is a function file_exists() to check if the image is existent.

If that function returns false instead of echoing the image just echo "<p>Can't find image (http://localhost/images/curry.jpg)</p>" and add a style attribute to the wrapping div that states your desired width and height

<div style='height: 242px;width: 300px'>
like image 116
lock Avatar answered Feb 26 '23 23:02

lock


If you're generating the page using PHP you can just fix the width / height of the div.

As for the "Alt Text" could you show a small screenshot of what you want? I don't really understand by what you mean by occupying the given size.

like image 20
moskovich Avatar answered Feb 26 '23 23:02

moskovich