Search
 
SCRIPT & CODE EXAMPLE
 

HTML

relative vs absolute path html

Relative path: 
<img src="dog.png">
<!-- 
a local file relative to where the html is on a computer or server
-->

Absolute path: 
<img src="http://yourdomain.com/images/example.png">
<img src="//yourdomain.com/images/example.png">
<!-- 
full URL path
-->

Great stack overflow answer:

<!-- 
URLs including scheme (e.g. http / https) and the hostname (e.g. yourdomain.com) shouldn't be used (for local resources) because it will be terrible to maintain and debug.

Let's say you have used absolute URL everywhere in your code like <img src="http://yourdomain.com/images/example.png">. Now what will happen when you are going to:

switch to another scheme (e.g. http -> https)
switch domain names (test.yourdomain.com -> yourdomain.com)
In the first example what will happen is that you will get warnings about unsafe content being requested on the page. Because all your URLs are hardcoded to use http(://yourdomain.com/images/example.png). And when running your pages over https the browser expects all resources to be loaded over https to prevent leaking of information.

In the second example when putting your site live from the test environment it would mean all resources are still pointing to your test domain instead of your live domain.

So to answer your question about whether to use absolute or relative URLs: always use relative URLs (for local resources).

This is how to structure absolute URLs with scheme:
//yourdomain.com/images/example.png

This URL is relative based on the current scheme used and should almost always be used when including external resources (images, javascripts etc).

What this type of URL does is use the current scheme of the page it is on. This means that you are on the page http://yourdomain.com and on that page is an image tag <img src="//yourdomain.com/images/example.png"> the URL of the image would resolve in http://yourdomain.com/images/example.png.
When you would have been on the page http**s**://yourdomain.com and on that page is an image tag <img src="//yourdomain.com/images/example.png"> the URL of the image would resolve in https://yourdomain.com/images/example.png.

This prevent loading resources over https when it is not needed and automatically makes sure the resource is requested over https when it is needed.
-->
Comment

PREVIOUS NEXT
Code Example
Html :: html form detached submit button 
Html :: use variable in input value vuejs 
Html :: how to change the color of text in marquee in html 
Html :: how to put javascript in html 
Html :: date of birth select box in html 
Html :: multiline comment html 
Html :: on page load animation 
Html :: html form label 
Html :: strikethrough code 
Html :: html how to play gifs 
Html :: how to jump line in html 
Html :: how to make a div responsive in css 
Html :: mvc Html.DropDownList get value 
Html :: bootstrap css 
Html :: input type tel 
Html :: ngbdatepicker disable input 
Html :: font awesome react color 
Html :: convert element to html string 
Html :: remove reset all css using style property attribute in react component 
Html :: full height div inside td 
Html :: number format in html input 
Html :: bootstrap wysiwyg 
Html :: how to make auto focus textarea vuejs 
Html :: was blocked because of a disallowed MIME type (“text/html”). 
Html :: how to make a footer in html extend to the bottom 
Html :: html contenteditable attribute 
Html :: reduce the width of google recaptcha 
Html :: class disabled 
Html :: how to put the heading in th ecenter of th html page 
Html :: style html 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =