HTML Base Element

HTML Base element <base> defines the base URL or root path for a specific website, which will be used to find the target resources of all the relative paths on the website.

Only one base element must be defined within the head section of an HTML document.

This base path is used by all the relative paths on the website to find resources like files, images, etc.,

<base href="https://randomcodez.com/">
<base href="/web-app/">
<base href="https://randomcodez.com/" target="_blank">

Base Element Attributes

Here is a list of attributes that a base element can have.

  • Attribute target defines target attribute for all the relative URLs on the website

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- <base href="/"> -->
    <!-- <base href="/" target="_blank"> -->
    <base href="https://randomcodez.com/" target="_blank">
</head>
<body>
    <h1>HTML Base Element</h1>
    <p><a href="/assets/logo/logo.png">Logo</a></p>
    <p><a href="/assets/favicon/favicon.ico">Favicon</a></p>
    <p><a href="https://randomcodez.com">Website</a></p>
</body>
</html>

Overall

HTML base element comes in handy to define a default address or location to find all the target resources of a website.