HTML base tag
- 24-07-2022
- Trung Minh
- 0 Comments
In this article we will learn the HTML base tag, which is a URL declaration tag for a web page through which the browser will automatically append incomplete URLs.
1. What is the base tag in HTML?
The base tag sets the base URL for all related URLs in the HTML document.
In an HTML document there is only one base tag, and it must be inside the head tag. The syntax is as follows:
<base href="http://localhost/test/images/" target="_blank">
Where the href attribute is the URL declaration, and the target will declare how to redirect.
This attribute is useful in cases where the URLs in the HTML document do not specify the domain or the root directory of the domain.
Example : You put all the CSS and JS data in the domain.com/public
directory. Now you just need to declare the base url as follows:
https://domain.com/public/
As for the CSS and JS files, they will be:
<link href="style.css" rel="stylesheet"/>
The browser will understand that the style.css file will be in the public folder.
2. How to use the base tag in HTML
For example, use the base tag to set all URLs to start from the images folder.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base href="http://localhost/test/images/" target="_blank"> </head> <body> <h1>Học lập trình miễn phí tại freetuts.net</h1> <img src="test.jpg" width="24" height="39" alt="Stickman"> </body> </html>
Attributes of the base tag :
- href – the base path, which will be the starting place for all related URLs in the HTML document.
- hreftarget – Specifies the default target for hyperlinks and in-page forms.
Browser support :
- Chrome
- Firefox
- IE
- Safari
- Opera