Examples of relative URLs

Examples of relative URLs

02.Oct.2021

How do you provide relative paths to your images?

Many web pages contain external graphics that link to documents on other servers. For example, an image of a company logo or widget might be stored on their server. It is common practice to create URLs for each graphic using the URL of the document containing the image as the base for the path. This allows the designer/programmer to relocate images without changing all links. The following are some examples of images with different sources:

Image 1 was provided by http://wwwimages1/mylogo.gif via <img src="http://images1/mylogo.gif"> . Image 2 was provided by http://wwwimages2/bigwidget3d-thumbnail.jpg via <img src="http://wwwimages2/bigwidget3d-thumbnail.jpg"> . Image 3 was provided by http://wwwimages3/myfield1.gif via <img src="http://wwwimages3/myfield1.gif"> .

 

How do you provide relative paths to your JavaScript files?

Many web pages make use of JavaScript code libraries such as jQuery and MooTools, which are served from a content delivery network (CDN). Calling the JavaScript code using an absolute URL is not recommended since it would prevent reuse by other pages that want to link to it:

Bad: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

Good: <script src="../../libs/jquery/1.3/jquery.min.js"></script>

When linking to a JavaScript file, the path is relative to the document that calls it, not the CDN server where it is hosted:

Bad: <!-- This won't work because jQuery's URL is on another server --> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

Good: <!-- jQuery's URL is relative to this page and can be moved together with it --> <script src="../../libs/jquery/1.3/jquery.min.js"></script>

 

How do you provide relative paths to your CSS files?

Many web pages contain internal or external CSS stylesheets that link to documents on other servers. For example, a custom stylesheet for a content management system (CMS) may be stored on the CMS's server and linked from many web pages:

Bad: <link rel="stylesheet" href="http://cms1.com/mystylesheet.css">

Good: <link rel="stylesheet" href="../../cms1-files/mystylesheet.css">

When linking to a CSS file, the path is relative to the document that calls it, not the server where it is hosted:

Bad: <!-- This won't work because stylesheet's URL is on another server --> <link rel="stylesheet" href="http://cms1.com/mystylesheet.css">

Good: <!-- stylesheet's URL is relative to this page and can be moved together with it --> <link rel="stylesheet" href="../../cms1-files/mystylesheet.css">

 

How do you provide relative paths to your images? Many web pages contain external graphics that link to documents on other servers. For example, an image of a company logo or widget might be stored on their server. It is common practice to create URLs for each graphic using the URL of the document containing the image as the base for the path. This allows the designer/programmer to relocate images without changing all links. The following are some examples of images with different sources: Image 1 was provided by http://wwwimages1/mylogo.gif via <img src="http://wwwimages1/mylogo.gif"> . Image 2 was provided by http://wwwimages2/bigwidget3d-thumbnail.jpg via <img src="http://wwwimages2/bigwidget3d-thumbnail.jpg"> . Image 3 was provided by http://wwwimages3/myfield1.gif via <img src="http://wwwimages3/myfield1.gif"> . When linking to an image, the path is relative to the document that calls it, not the server where the graphic is hosted: Bad: <!-- This won't work because images' URLs are on another server --> <img src="http://wwwimages1/mylogo.jpg" alt="My Logo">

Good: <!-- images' URLs are relative to this page and can be moved together with it --> <img src="../../images/mylogo.jpg" alt="My Logo">

 

How do you provide relative paths to your anchors? Many web pages contain internal or external links that refer to documents on other servers. For example, a navigation bar might link to sections of a website that are stored on another server:

Bad: <a href="http://www.example.com">Home</a>

Good: <a href="../index.html">Home</a> When linking to an anchor, the path is relative to the document that calls it, not the server where it is hosted: Bad: <!-- This won't work because anchors' URLs are on another server --> <p><a href="http://www.example2.com/aboutus">About Us</a></p>

Good: <!-- anchors' URLs are relative to this page and can be moved together with it --> <p><a href="../index.html">About Us</a></p>

 

 

 

How do you provide relative paths to your scripts? Many web pages contain internal or external JavaScript files that link to documents on other servers. For example, a script used for date formatting may be stored on the same server as this page:

Bad: <!-- This won't work because scripts' URLs are on another server --> <script src="http://ajax.googleapis.com/ajax/libs/datepicker/1.5/jquery.ui.datepicker-en_US-20100205.js"></script>

Good: <!-- scripts' URLs are relative to this page and can be moved together with it --> <script src="../../scripts/jquery-ui-datepicker-en_US-20100205.js"></script>

When linking to a script, the path is relative to the document that calls it, not the server where it is hosted: Bad: <!-- This won't work because scripts' URLs are on another server --> <script src="http://ajax.googleapis.com/ajax/libs/datepicker/1.5/jquery.ui.datepicker-en_US-20100205.js"></script>

Good: <!-- scripts' URLs are relative to this page and can be moved together with it --> <script src="../../scripts/jquery-ui-datepicker-en_US-20100205.js"></script>

 

How does the browser determine that a link points to an anchor rather than just text? By using the "name" attribute of an HTML element, you can associate a name with any portion of your document's content. When linking to this named element elsewhere in your document, the browser knows not only where it is but what its content is supposed to be.<br>To give an example, here are 3 links linking to the same article: <a href="../index.html">Link 1</a>, <a href="../index.html#my_anchor">Link 2</a>, and <a href="#my_anchor">Link 3</a>.

If Link 1 is clicked, the browser URL will be: https://developer.mozilla.org/en-US/docs/Learn

If Link 2 is clicked, the browser URL will be: https://developer.mozilla.org/en-US/docs/Learn#my_anchor

If Link 3 is clicked, the browser URL will be: https://developer.mozilla.org/en-US/docs/Learn#my_anchor

When linking to an anchor, the path is relative to the document that calls it, not the server where it is hosted: Bad: <a href="http://wwwexample2com">Link 1</a>

Good: <a="#my_anchor">Link 2</a>

Bad: <a href="../index.html#my_anchor">Link 3</a>

Good: <a href="../index.html">Link 4</a>

If Link 1 is clicked, the browser URL will be: http://wwwexample2com/Learn If Link 2 is clicked, the browser URL will be: https://developer.mozillaorgen-USdocs/Lnk3 If Link 3 is clicked, the browser URL will be: https://developer.mozillaorgen-USdocs/Lnk4 If Link 4 is clicked, the browser URL will be: https://developer.mozillaorgen-USdocs/Learn

 

How do you link to an anchor within the same page?

To link to an anchor within the same page, use the "id" attribute of any HTML element. For example: <a href="#my_anchor">Link with ID</a>

The URL of this link will be: https://developer.mozilla.org/en-US/docs/.#my_anchor

When linking to an anchor, the path is relative to the document that calls it, not the server where it is hosted: Bad: <a id="my_anchor">Link with ID</a>

Good: <a href="#my_anchor">Link with ID</a>

If you click on this link, the URL will be: https://developer.mozilla.org/en-US/.my_anchor

 

When linking to anchors within other documents (such as images that are referred to via hyperlinks), HTML provides a mechanism to reference those documents by using URLs. These URLs must start with either http:// or https://, which is necessary for Internet Explorer and some older browsers. In addition, they may only contain ASCII letters, digits, dots, plus signs (+), hyphens (-), and periods (.), and they must not begin with a space or a tab – otherwise they won't work in Firefox! Any invalid part a relative URL will be returned as a question mark. Here's an example of how to reference an image in another HTML document: <img src="our_image.png" alt="our image" />

This will display our image at 100 pixels wide, which you specify by the width attribute of the <img> element.

To link to this image from other pages on your website, use a relative URL like this: <a href="http://example.com/our_document.html#our_image">Link with ID</a> And here are 2 links that both link to this same image file but one is referencing it via an absolute URL and the other via a relative URL.<br>If you click on Link 1, the browser URL will be: https://example.com/our_document.html

If you click on Link 2, the browser URL will be: http://example.com/our_document.html#our_image

To link to an anchor within the same page, use the "id" attribute of any HTML element : Good: <a href="#my_anchor">Link with ID</a>

Bad: <a id="my_anchor">Link with ID</a>

Good: <img src="../images/our_image.png" alt="our image"/>

If a relative URL is used when linking to this image in another HTML document, it must be relative to the current, linking HTML document.

Good: <a href="http://example.com/our_document.html#our_image">Link with ID</a>

Bad: <a href="#my_anchor">Link with ID</a>

 

When you click on this link, the browser URL will be: https://developer.mozillaorgen-USdocs/Lnk4 Good: <a href="https://developer.mozillaorgen-USdocs/Learn">Home Page</a> Bad: <a href="#h1">Heading 1</a> Note that there is only one way to reference an element if it has an id attribute, regardless of whether or not they are in the same document. For more information about relative URLs, see Relative URLs on MDN.

 

When you click on this link, the browser URL will be: https://developer.mozillaorgen-USdocs/Learn Good: <a href="https://developer.mozillaorgen-USdocs/Lnk3">Link 3</a> Bad: <a href="#Lnk3">Link 3</a>

Note that there are multiple ways to reference links between documents when the links have an id attribute, but all of these ways use at least one absolute path component (i.e., greater than two periods) and they are case sensitive - so only "example" would work, not "Example". For more information about relative URLs, see Relative URLs on MDN.

 

To link to an anchor within the same page, use the "id" attribute of any HTML element.

For example: <a href="#my_anchor">Link with ID</a>

The URL of this link will be: https://developer.mozillaorgen-USdocs/.#my_anchor

When linking to an anchor, the path is relative to the document that calls it, not the server where it is hosted: Bad: <a id="my_anchor">Link with ID</a>

Good: <a href="#my_anchor">Link with ID</a>

 

If the anchor is within the same document, you can reference it with an absolute URL like this: <a href="https://developer.mozillaorgen-USdocs/Learn#my_anchor">Link with ID</a> The browser URL for this link will be: https://developer.mozillaorgen-USdocs/Learn#my_anchor

Good: <img src="../images/our_image.png" alt="our image"/>

Note that there is only one way to reference an element if it has an id attribute, regardless of whether or not they are in the same document. For more information about relative URLs, see Relative URLs on MDN.

 

Good: <a href="https://developer.mozillaorgen-USdocs/Lnk4">Link 4</a>

Bad: <a href="#Lnk4">Link 4</a>

Note that there are multiple ways to reference links between documents when the links have an id attribute, but all of these ways use at least one absolute path component (i.e., greater than two periods) and they are case sensitive - so only "example" would work, not "Example". For more information about relative URLs, see Relative URLs on MDN.

We are social