HTML:HTML Links and Images: <a> href and <img> src Attributes
HTML•Hyperlinks & Media

HTML Links and Images: <a> href and <img> src Attributes

Learn attribute syntax for navigation links, opening tabs safely with rel="noopener", and image accessibility.

Syntax

<a href="https://compilra.app" target="_blank" rel="noopener">Link</a> <img src="img.jpg" alt="Description" />

Parameters & Values

Key components and acceptable values for this structure:

ParameterTypeDescription
hrefAttributeHypertext reference URL destination.
altAttributeAccessible alternative text describing image.

Ready-to-Run Code Example

Executable in Compilra

Run this complete, working example directly without installing HTML locally:

<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; padding: 20px; text-align: center; }
    .card { border: 1px solid #cbd5e1; border-radius: 12px; padding: 20px; max-width: 320px; margin: auto; }
    a.btn { display: inline-block; background: #2563eb; color: white; padding: 8px 16px; border-radius: 6px; text-decoration: none; font-weight: bold; }
  </style>
</head>
<body>
  <div class="card">
    <h2>Compilra IDE</h2>
    <p>Run 10+ programming languages online with real-time cloud compilation.</p>
    <a href="/compiler" class="btn">Launch Compiler &rarr;</a>
  </div>
</body>
</html>
Expected Output:
[Rendered Interactive Card with Title, Description, and Styled Call-to-Action Link Button]

How It Works: Step-by-Step

  • Lines 10-15: Constructs interactive card with styled anchor <a> tag.

Key Concepts

  • The <a> tag creates hyperlinks to external URLs or internal routes.
  • Always supply the alt attribute on <img> tags for screen readers and SEO.

Common Mistake: Using target="_blank" without rel="noopener noreferrer"

Opening external links in new tabs without rel="noopener" creates a security vulnerability (tabnabbing).

❌ Incorrect
<a href="https://example.com" target="_blank">Link</a>
✅ Correct
<a href="https://example.com" target="_blank" rel="noopener noreferrer">Link</a>

Related HTML Tutorials & Compiler

Run and test HTML on Compilra

Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.

HTML Online Compiler