The Correct Way to Create a Hyperlink in HTML
The Correct HTML for Creating a Hyperlink
The correct HTML for creating a hyperlink is the anchor tag, written as a with an href attribute pointing to the destination URL. Here is the most basic example.
a href="https://www.example.com"Visit Example/a
Let us understand what each part is doing here. The a tag stands for anchor, and it is the official HTML element used to define a link. The href attribute, which is short for hypertext reference, tells the browser where this link should take the user when clicked. Whatever text sits between the opening and closing tag becomes the clickable part that shows up on the page. So in the example above, the words Visit Example are what the visitor will actually see and click on, while the URL in the href quietly does the job of redirecting them.
A common mistake beginners make is forgetting the closing tag or mixing up the quotation marks. HTML expects the value of href to be wrapped in either double or single quotes, and the anchor element must be properly closed, otherwise the rest of your page content might accidentally get pulled inside the link.
What Is the Correct HTML for Creating a Hyperlink With JavaScript
Sometimes you do not want a link sitting in your HTML as plain markup, you want it created or controlled dynamically through JavaScript, maybe based on user interaction or data coming from an API. In that case, JavaScript is used to either generate the anchor element on the fly or to change where an existing link points.
Here is a simple way to create a hyperlink using JavaScript by generating the anchor element dynamically.
const link = document.createElement("a")
link.href = "https://www.example.com"
link.textContent = "Visit Example"
document.body.appendChild(link)
This piece of code creates a brand new anchor element, assigns it a destination through the href property, gives it visible text, and finally attaches it to the page. This approach is extremely useful when you are building something like a dynamic list of search results or product cards where each item needs its own unique link that is not known ahead of time.
You can also simply change the destination of a link that is already sitting in your HTML. For instance, if there is a link with the id myLink, you can update its target like this.
document.getElementById("myLink").href = "https://www.example.com"
Both methods rely on the same underlying idea, which is that the anchor element always needs an href to function as a real hyperlink, whether that href is written directly in the HTML or assigned later through a script.
What Is the Correct HTML for Creating a Hyperlink Example in Real Use
Let us look at a more realistic example, something you might actually use on a website. Suppose you run a small blog and want to link to one of your older posts from a new article.
a href="/blog/how to bake bread at home"Check out our bread baking guide/a
Notice how the link here points to a relative path instead of a full web address. This is completely valid HTML and is actually the recommended approach when linking to pages within your own website, since it keeps things flexible if your domain ever changes. External links, on the other hand, usually need the complete address starting with https so the browser knows exactly which website to fetch.
Another useful pattern is linking directly to an email address or a phone number, which many beginners forget is possible.
a href="mailto:hello@example.com"Send us an email/a
a href="tel:+911234567890"Call us now/a
These small variations of the mailto and tel schemes turn ordinary text into functional shortcuts, letting visitors open their email client or dial a number with a single tap, especially useful on mobile devices.
Which Character Is Used to Indicate an End Tag
This is another question that trips up a lot of new learners. In HTML, the forward slash character is used to indicate an end tag. So while an opening tag looks like p, the closing version of that same tag looks like slash p, with the forward slash placed right after the less than symbol and before the tag name.
The same rule applies to the anchor tag we have been talking about. The opening tag is a href="..." and the closing tag is slash a. Without this forward slash, the browser has no clean way of knowing where an element is supposed to end, and your markup can start behaving unpredictably, with later content accidentally inheriting styles or behavior from an unclosed tag.
It is worth mentioning that a small number of HTML elements, called void elements, do not need a closing tag at all because they cannot contain any content. Elements like img, input and br fall into this category. But the anchor tag is not one of them, so anytime you open an a tag, make sure you are closing it properly with slash a.
How Can You Open a Link in a New Tab or Browser Window
If you want a link to open in a new tab rather than replacing the current page, you need the target attribute set to the value blank.
a href="https://www.example.com" target="blank"Visit Example/a
When a browser sees target set to blank, it opens the linked page in a fresh browsing context, which in most modern browsers means a new tab, though depending on browser settings it could also open as a separate window. This is commonly used for outbound links, so visitors do not lose their place on your website when they click through to an external resource.
There is one more thing worth knowing here for anyone serious about web security. Whenever you use target blank, it is good practice to also add rel with the value noopener, like this.
a href="https://www.example.com" target="blank" rel="noopener"Visit Example/a
This prevents the newly opened page from having partial access back to your original page through the window object, which is a minor but real security consideration, especially if the destination site is not one you fully control.
What Is the Correct HTML for Making a Checkbox
While we are on the topic of common HTML interview style questions, let us cover a few related ones that often come up alongside hyperlink questions, starting with checkboxes.
The correct HTML for making a checkbox is the input element with its type attribute set to checkbox.
input type="checkbox" id="subscribe" name="subscribe"
label for="subscribe"Subscribe to our newsletter/label
The checkbox itself is a void element, so it does not need a closing tag, but pairing it with a proper label element, connected through the for and id attributes, makes the checkbox accessible and clickable through its text as well, not just the tiny box itself.
How Can You Make a Numbered List
To make a numbered list in HTML, you use the ordered list element, written as ol, and each item inside it goes inside an li tag.
ol
li First step/li
li Second step/li
li Third step/li
/ol
The browser automatically numbers each list item starting from one, and it will renumber everything on its own if you add or remove items later, so you never have to manually type out the numbers yourself. This is different from an unordered list, which uses ul and shows bullet points instead of numbers.
What Is the Correct HTML for Making a Drop Down List
A drop down list in HTML is created using the select element, with each individual choice defined by an option element inside it.
select name="country" id="country"
option value="india"India/option
option value="usa"United States/option
option value="uk"United Kingdom/option
/select
When the page loads, the browser collapses all these choices into a single compact dropdown, and the visitor can click it to reveal the full list of options. The value attribute on each option is what actually gets sent to the server when a form is submitted, while the text between the tags is simply what the user sees on screen.
What Is the Correct HTML for Making a Text Input Field
Finally, to create a basic text input field, you use the input element with its type set to text.
input type="text" id="username" name="username" placeholder="Enter your name"
Just like the checkbox, this is a void element and does not require a closing tag. The placeholder attribute is optional but genuinely useful, since it displays a light grey hint inside the empty field, guiding the user on what kind of information is expected without permanently occupying the field with default text.
Bringing It All Together
At its core, HTML is built around a fairly small set of predictable rules once you get past the initial confusion. Hyperlinks always rely on the anchor tag paired with an href attribute, closing tags always use that forward slash character, and form related elements like checkboxes, dropdowns and text fields each have their own dedicated input types that the browser knows how to render correctly. Once these basics feel natural, reading and writing HTML stops feeling like memorizing random symbols and starts feeling like a language you actually understand, which makes building and debugging real web pages a whole lot easier.
