Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Relative URLs are URLs that don't specify a complete host and path. Let's go ahead and open up relative.html. I'm not going to make a working copy here because we're not going to be changing anything, but I want to show it to you in the editor. And here we have a normal little HTML document. You'll notice that this link here, instead of having a whole URL, it just has a file name, page1.html. So what happens with that is that the browser comes along and it says, oh, a relative URL. So it'll construct a complete URL and it'll use this as the basis of that, and so what it does is it says, well, where did I get this document? I got this document on this host and at this path.
I am going to take that host and path all the way up to the file name and I am going to replace the file name with whatever is here. So we have a path here. Let me go ahead and open this in the browser so you can see what that looks like. It's a file path. See, it starts with the file scheme and then it's got this path/users/billweinman blah, blah, blah, all the way up to Chap07/relative.html. So what the browser will do is it says, I'm looking for this page1.html in the path where the current document is, and so just take this relative.html and it'll replace that with page1.html.
And if I hover my mouse over this, you'll notice down here at the bottom of the browser, you'll see that constructed URL. It's everything up to Chap07 and it's page1.html. So when I click on that, I get this page1 document. Let's take a look at that. We'll open that in our text editor, page1.html, and we see here, it's the same document basically, and we have a couple of things. We have a link to, and here it is, a link to page2, but you'll notice that this has a subdirectory.
Again it's a relative URL. It doesn't begin with a slash. It doesn't begin with HTTP or anything like that, and it says subdir/page2.html. So the browser will go through the same process. It'll take the current path to this page1.html that it's opened up and it's found this URL in, and it'll replace page1.html with whatever it sees here, which starts with "subdir/page2.html." So if we look at this in the browser, see, we have our current path has everything up to Chap07.
And if I hover with this page 2, you see down here at the bottom it says Chap07/subdir/page2. And if we look here in our file system, you see we have a subdir, and there is the page2.html. So when I click on this link, it brings up that page2. And let's just bring that up in the editor, and we can look at that. And you'll notice a couple of things in here. One is the style sheet. You'll notice back in our other documents, if I bring up page1.html, you notice our style sheet here, it says main.css in the href. And if I bring up page2 you'll notice it says ../main.css, and the link back to page1 has ../page1. So this ..
is a special thing. Actually, it comes from UNIX file systems, and it means the directory of the previous level relative to this document. In other words, when we look up here at this whole path up to page 2 and we see that the current directory is subdir, what it'll do is it sees that .. so it takes one out, and it'll go back to Chap07, and it'll construct that URL, so you see the URL says Chap07 page1.html down there at the bottom of the screen and here it says ../page1.
The same thing for the CSS, because our CSS file is in the previous directory. See, it's right there. So this href for the CSS works exactly the same way, and you can have relative URLs there too as well. We've been doing that all along. If we look at the URL in page1, it just says main.css. That's a relatively URL. It means in the current directory. So let's go ahead and click on our back-to-page-1 link, and you see now we're back in Chap07 and click on original document. See, now we are back at the original document.
So relative URLs are a great way to refer to objects within the same file space. Be careful though; it takes some effort to maintain relative links as you move your documents around on your site.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97389 Viewers
61 Video lessons · 84630 Viewers
71 Video lessons · 68834 Viewers
56 Video lessons · 101294 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.