Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

img tag not working with relative path in src

Tags:

html

css

image

This is not working:

<img src="../assets/images/image.jpg" alt="Alternative Text">

But this is working:

<img src="http://localhost/abc/def/geh/assets/images/image.jpg" alt="Alternative Text">

In my scenario, I just cannot work with absolute path. I have to use relative path.

like image 480
PHP Developer Avatar asked Jul 14 '15 03:07

PHP Developer


People also ask

Why is my img src not working?

Img src Not Working That means, when a web page loads, the browser has to retrieve the image from a web server and display it on the page. The broken link icon means that the browser could not find the image. If you've just added the image, then check that you included the correct image URL in the source attribute.

What does img src => do?

The img src stands for image source, which is used to specify the source of an image in the HTML <img> tag.

Does an IMG tag need a src?

All <img> tags must have a defined src attribute. This defines the image to be displayed. Typically, the src is a URL, but a data representation of the image can also be used in some cases.


2 Answers

<img src="assets/images/image.jpg" alt="Alternative Text">

should be working.You shouldn't have put '../' at the begining of the image path.

For better understanding about relative paths vs. absolute paths, refer this link

like image 175
Tharushi Geethma Avatar answered Oct 01 '22 18:10

Tharushi Geethma


"../assets/images/image.jpg" -This means

  1. '../' go up one directory from where I am now
  2. find the 'assets/' folder
  3. find the 'images' folder
  4. find the 'image.jpg' file.

That relative link will only work if your page is in a subfolder in

"http://localhost/abc/def/geh/"

If the location of your page really is

"localhost/asdf/asdf/asdf/asdf/index.php"

(which seems ridiculous) to get to the assets folder relatively you would have to go all the way to the root.

'../../../../abc/deh/geh/assets/images/image.jpg;

Alternatively you could use a base tag in your head tag to make the URL in the actual src attribute more friendly.

like image 25
user2782001 Avatar answered Oct 01 '22 20:10

user2782001