Resources | Subject Notes | Information Communication Technology ICT
Understand the reasons why relative file paths must be used for attached stylesheets in website authoring.
When creating a website, it's common to separate the content (HTML) from the presentation (CSS stylesheets). Using relative file paths allows your website to be easily moved and updated without breaking the links between the HTML and CSS files.
A relative path specifies the location of a file relative to the current location of the HTML file. This makes your website more portable.
There are two main types of relative paths:
Consider a simple website structure:
Directory | File |
---|---|
/htdocs/mywebsite/ | index.html |
/htdocs/mywebsite/styles/ | style.css |
In the index.html
file, the correct relative path to link the stylesheet would be "styles/style.css"
.
If the style.css
file were moved to a different directory within the mywebsite
folder (e.g., /htdocs/mywebsite/css/style.css
), the relative path in index.html
would need to be updated to "css/style.css"
.
Absolute paths specify the full URL of a file, including the domain name. For example: http://www.example.com/mywebsite/styles/style.css
. Absolute paths are not portable.
Relative paths specify the location of a file relative to the current location of the HTML file. Relative paths are portable.
Using relative file paths for stylesheets is essential for creating websites that are portable, maintainable, and easy to update. They ensure that your website will continue to function correctly even if the website's directory structure changes.