4. Links and Images

Links use the a (anchor) tag with an href attribute that holds the address. Images use the img tag with a src attribute (the file location) and an alt attribute (a text description for screen readers and for when the image cannot load). Attributes always go inside the opening tag as name="value". In an MVC project, images and files normally go in wwwroot and are referenced like ~/images/photo.jpg.

Key points

  • Links to other websites need the full address starting with https://.
  • Links inside your own site can be short, like /Students/Index.
  • Always write a meaningful alt text on images.
  • img has no closing tag.
  • Use target="_blank" to open a link in a new tab.

Example: A link and an image

HTML and CSS Open in editor
<h2>Useful places</h2>
<p><a href="https://developer.mozilla.org">Read the MDN web docs</a></p>

<!-- Normally src is a file path like images/logo.png -->
<img alt="A black rectangle" width="160" height="80"
     src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='80'><rect width='160' height='80' fill='black'/></svg>">
Show explanation

The a tag makes clickable text. The img tag shows a picture. This example draws a small image from inline data so it works without any file.

Try it yourself

Edit the code below and press Run. The result appears in the preview underneath.

Editor
Reset Preview runs in a safe sandbox. Scripts are disabled.
PREVIEW