HTML Links
π Introduction to HTML Links
HTML links allow users to navigate from one page to another β either within the same website or to external sites. They are created using the <a> tag (short for βanchorβ) and are essential for building web navigation π.
>>βThe web is built on links β they connect everything.β π
π€ Basic Syntax
HTML Link Syntax
<a href="https://example.com">Visit Example</a>- href attribute defines the destination URL. - The anchor text inside the tag is what users will click.
π Linking to Different Destinations
- π External URL β href="https://example.com"
- π Internal page β href="/about.html"
- π Telephone β href="tel:+1234567890"
- π§ Email β href="mailto:someone@example.com"
- π― Page section β href="#section-id"
π§ͺ Example: Internal vs External
Internal and External Links
<a href="/contact.html">Contact Us</a>
<a href="https://google.com">Google</a>πͺ Opening Links in New Tab
Use the target="_blank" attribute to open the link in a new browser tab.
Open Link in New Tab
<a href="https://openai.com" target="_blank">Visit OpenAI</a>Note
β οΈ It's good practice to also add rel="noopener noreferrer" for security when using target="_blank".
π¨ Styling Links with CSS
Links have default styles, but you can customize them using CSS.
CSS for Links
a {
color: blue;
text-decoration: none;
}
a:hover {
text-decoration: underline;
color: red;
}π Link to an Element on the Same Page
Use an anchor with href="#id" to jump to a specific element by its ID.
Jump to Section
<a href="#section1">Go to Section 1</a>
...
<h2 id="section1">Section 1</h2>β οΈ Things to Remember
- Always use a valid and reachable URL
- Use descriptive link text β avoid βclick hereβ
- Don't nest <a> inside another <a>
π Resources
>>βNavigation is not just about clicking β itβs about guiding.β π§