Is there an easier HTML Path method than repeating ../? - html

Really basic question, some advise would be great please.
All of my web images and CSS are of course organised in my root. I then have a catalogue folder, which branches out into 3-4 deeper sub categories. In my HTML its becoming a bit messy sourcing my images/css because I'm using a lot of:
../
<img src="../../../images/myimage.png" style="width:200px; height:auto;">
What do you guys recommend? Do you do this? Or should I sort my images relevant to that file into their sub categories? Thanks, as always.
C:\Users\sam\Desktop\webroot\catalogue\folder1\folder2\folder3

You can use base for this see MDN
The HTML element specifies the base URL to use for all relative URLs contained within a document. There can be only one element in a document.
For example if your folder structure is like yourdomain/images set
<base href="yourdomain"/>

This is the correct method of doing it. Rather than sorting images in relevant folder you can keep it in the separate folder only as you did.
There is a way of minimizing this no of times of ../
Don't assign the src directly in line of the tag.
Always prefer separate css file or javascript file to do that.
You can put all your css files in one folder and you can keep that folder nearer to your images folder.
So that you can use only one ../. it'll look like ../images/yourimage.png
Suppose your html file's location is like
\1\2\3\4\5\6\7\8\sample.html
And your image files are in \1\images\
you can place all the javascript files in a folder which is nearer to the images folder like this \1\scripts\
Now suppose in your sample.html file there were lot of image tags like below
<img height="200" width="200" class="nature"/>
<img height="200" width="200" class="nature"/>
<img height="200" width="200" class="nature"/>
you can just import the javascript file like this
<script src="../../../../../../../../scripts/myscripts.js"></script>
Note: this is the only place you are going to use lengthy ../
In myscripts.js file you can simply use
$(".nature").attr("src","../images/yourimage.png");
Similarly you can do this for the rest of the tags also.
So in that `javascript you need not to use lengthy locations.
You may have 100s of img tags. you can simply import this js file only one time and use it.

Related

Cant get image to display in subpage: HTML

Alright, so I have been attempting to get an image to display in one of my subpages in HTML. I am sure that this is a very simple and stupid issue in regards to how I am referencing the image in the code. The image file is called crab.jpg and it is inside the pics folder which is all contained in the Websites folder. I have no problems with similar code displaying images within the index file which is the main page for the website.
<img src="Websites/pics/crab.jpg"
width="400"
height="400"/>
Not sure if this will work, unless it's in a subfolder. But try adding /
before the Websites
if that doesn't work try ../
and if that doesn't work try ../../

HTML won't load the image on my web server that I just downloaded from a zip file

web page with my uploaded image here
I was trying to find the path my computer was using. I tried the basic code that
I learned in a program I'm currently in, but it didn't seem to work. The path is desktop/my-skillcrush-project/101-skillcrush-project-images/images-icons/html-icon.png
The program directed to download the zip file of the image on my computer and create a folder. With the root directory associated with the file including the image.Then to use this code. <img src="img/html-icon.png" alt="HTML icon"/ (closing tag disappears when I try to type it. Sorry, it's in my code.) and that was it. It seems too simple in my opinion. How should the files be saved so that it will show up??????
What is wrong
The problem is, in the src, you put a relative path. In HTML, a relative path is a path without a slash(/) at the beginning. So, HTML was expecting a folder called desktop in the 101-skillcrush-project-code folder which had all of the other folders and the image.
What you should do
You do not need to put the full(absolute) path for the image. You can put the relative path. That is, relative to where the index.html is located.
Solution
So, in the src of the image, you can put 101-skillcrush-project-images/image-icons/HTML Icon.png.
More Info
HTML File Paths on W3 Schools
HTML File Paths on GeeksforGeeks
It definitely is much easier if you make a clear structure for all of your html assets. That also makes it much easier to handle your paths. So for example start with a root folder - lets name it html, where you put all your html pages in. Inside html create a sub folder for e.g. for your images and css. Folder structure can look like that:
/html image path from html folder: <img src="img/html-icon.png">
|- img save "html-icon.png" here
|- css
|- js
|- fonts
|- etc
To access an image from another folder e.g. css folder, you first have to go one level up with .. and then, go into the img folder. e.g. <img src="../img/html-icon.png">
If you have your images somewhere outside your "web folder" the paths can get a pain. So just organize your assets - it is much more effective and much easier for you to find and work with it.

One file .html with css style and images inside

I have the index.html file, where iside I put CSS as inline style. I'd also have inside links to images, not to path to images. Finally, I'd like to have index.html file which will contains evrything inside as one file. How can I make not to get images from computer path but have a links to my images?
Thanks for help!
If I understand you correctly, you are seeking to build one huge file "index.html" which contains all the information needed to display what you want to display.
Think that this is something extremely unusual. Why would you need that? Probably there are better ways to achieve what you want to achieve.
If you really want to go for it, ideas that come to mind are:
* use simple graphics in form of .SVG images, these can be described with the HTML SVG tag
* use compiled HTML (.CHM) (https://en.wikipedia.org/wiki/Microsoft_Compiled_HTML_Help) - but not for the web...
when u want to call image from outside then call like below when you use http in url it will never look for local file path
<img src="http://www.domainname.com/yourimage.png" alt="image">
You can make a folder like images where is your index.html then put your all images in images folder & link your images like this
<img src="images/logo.png" />
This is folder path not from your computer, When you will upload it then this path will be automatically global
or you can use direct link from server like this:
<img src="https://www.example.com/logo.png" />
The best way would be to include all files relationally in a /images folder however if you need a one file solution then you could encode the images in base64 and use that in the src=""

Linking Files In Html

so i'm fairly new to html, so i have always just left everything in the same folder so i don't have to link things but am making a professional website for myself and portfolio. so i want to sort my files properly.
so lets say i have a folder called website, then in that folder i have 3 folders, html, css and images. my html documents are in the html folder what do i do to link images from the images folder in the html document that is in the html folder. pleas note that images isn't a sub folder of html they are both sub folders of website.
any help is helpful (self explanatory i know)
If the html folder and images folder are siblings, the path from a file in the html folder to an asset in the images folder would look like this:
"../images/mypic.jpg"
You can do this in the html page by adding the img tag:
<img src="../images/yourImage.png" alt="" />
But the better method would be to use CSS:
<img alt="" />
img {
background-image: url("../images/yourImage.png");
}
Notice that the "../" is used to go up one level from the current directory of your html page or css file.
You can simply prepend your urls with / which will always reference the root folder of your website.
Assuming yoursite.com loads the folder contents of website, you can link to files in the images directory like this:
/images/image.jpg which will produce http://yoursite.com/images/image.jpg
Example:
<img src="/images/image.jpg" />
There are various ways that the links could work whether from the root:
<a href="http://www.yourdomain.com/images/image.jpg">
Or perhaps once you get a little more experienced with managing your folders, you can set them up a little more dynamic (making them automatically work on live/development transfers):
<a href="images/image.jpg">
Does that help?
PS: The html or public_html folder is typically your root with a host, meaning you don't need to includ this in your paths. Consider whereever your functional index file is the root folder.
U can use .. to go to previous folder.In your case while pointing path for images in html file you can do as metioned below.
If the image is within the same folder then just use:
<img src="image.jpg">
If the image is within a sub folder then just use:
<img src="subfolder/image.jpg">
If the image is within a parent folder then just use:
<img src="../otherfolder/image.jpg">
or if it is just located in the parent folder
<img src="../image.jpg">
This is going to explain it in detail:
http://www.htmlkit.com/minit/pages/imgtag1.html

How to access files that are at the top of the hierarchy?

I'm new to HTML and I had a question about accessing files with pictures.
I know that, to access the pictures that are within other folders, the code looks something like what is shown below and my HTML file would be right before the pictures file:
<img src="pictures/mountain.jpg" alt="mountain"/>
My question is: how do I access pictures that are placed in files that are outside of my HTML file? How do I link a picture but backwards through a file that is the file that holds these files?
Do you mean going up a folder?
<img src="../pictures/mountain.jpg" alt="mountain"/>
Also, if you wanted to go up an additional folder you would do this:
<img src="../../pictures/mountain.jpg" alt="mountain"/>
The files you want to refer to in a html file, being images or other html files or JavaScript files, must be within the root of your server. So if the root of your server is c:\users\httpserver and you want to link to an image in c:\users\kristy\pictures\mountain.jpg, the answer is that it is not possible, you will need to copy the file to a Directory within c:\users\httpserver and link it either relatively, as ketchupisred showed or absolutely.
i.e if the html-file is located in c:\users\httpserver\html and the picture is c:\users\httpserver\pictures\mountain.jpg, either
<img src="/pictures/mountain.jpg" alt="mountain"/>
note the starting / that refers to the root of the server or
<img src="../pictures/mountain.jpg" alt="mountain"/>
the .. taking you one Level up before going into the pictures folder
Since going up one or two folders has already been explained, I'll just add that if you want to access a file in the root directory you can do this:
<img src="/pictures/mountain.jpg" alt="mountain"/>