HTML src Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Media & Embedding

Introduction

The src attribute is one of the most important attributes in HTML. It specifies the source URL of a resource the browser should load and embed—such as an image, JavaScript file, iframe page, or media clip. Without a valid src, the element usually has nothing to display or run.

The value is typically a URL: absolute (https://example.com/logo.png), root-relative (/images/logo.png), or relative to the current page (assets/app.js). The same attribute name appears on several elements, but each element loads a different kind of content.

What You’ll Learn

01

<img>

Image files.

02

<script>

External JS.

03

<source>

Video/audio.

04

URL types

Relative paths.

05

JavaScript

Change .src.

06

vs href

Embed vs link.

Purpose of src

The primary purpose of the src attribute is to tell the browser where to fetch external content that should become part of the page. On an <img>, that is the image file. On a <script>, it is a JavaScript file to download and execute. On an <iframe>, it is another HTML document to display inside the frame.

Depending on the element, src can point to images, scripts, styles (via deprecated patterns), audio, video, subtitle tracks, or nested documents. The browser resolves the URL, requests the resource, and renders or runs it in place.

💡
Embed, don’t navigate

src embeds a resource into the page. Hyperlinks use href on <a> and <link> instead. For responsive images with multiple files, use srcset alongside (not instead of) a fallback src.

📝 Syntax

Set src to a valid URL string on supported elements:

src.html
<img src="photo.jpg" alt="Description">

<script src="app.js" defer></script>

<iframe src="https://example.com/embed" title="Embedded page"></iframe>

Syntax Rules

  • Value is a URL string (absolute, root-relative, or relative path).
  • On <img>: required unless using certain SVG patterns; always include alt.
  • On <script src>: do not put inline code inside the tag—use a separate file or omit src.
  • On <video> / <audio>: prefer <source src> for multiple formats.
  • URLs must be encoded correctly (spaces as %20, etc.).
  • Cross-origin resources may be subject to CORS and security policies.

💎 Values

The src attribute accepts a URL. Common patterns:

  • Relative filesrc="images/logo.png" (relative to the current page URL).
  • Root-relativesrc="/assets/app.js" (from the site root).
  • Absolute URLsrc="https://cdn.example.com/lib.js".
  • Data URLsrc="data:image/svg+xml,..." for inline small assets.
  • Media on source<source src="clip.mp4" type="video/mp4"> inside <video>.
src-values.html
<!-- Image -->
<img src="/images/hero.webp" alt="Hero">

<!-- External script -->
<script src="https://example.com/analytics.js" async></script>

<!-- Video with source -->
<video controls>
  <source src="example.mp4" type="video/mp4">
</video>

⚡ Quick Reference

ElementLoadsExample
<img>Image filesrc="photo.jpg"
<script>JavaScript filesrc="app.js"
<iframe>HTML documentsrc="/embed"
<source>Media filesrc="clip.webm"
<audio>Audio filesrc="song.mp3"
JavaScriptel.src = urlSwap at runtime

Applicable Elements

ElementUses src?Notes
<img>YesPrimary image URL; pair with alt
<script>YesExternal JS; use defer or async when needed
<iframe>YesEmbedded browsing context; set title
<video> / <audio>YesDirect src or via <source> children
<source>YesInside video, audio, or picture
<a>, <link>NoUse href instead

src vs href vs srcset vs srcdoc

AttributePurposeTypical element
srcEmbed resource URLimg, script, iframe
hrefLink to resourcea, link
srcsetMultiple image candidatesimg, source
srcdocInline HTML for iframeiframe

Examples Gallery

img, script, video/source, and dynamic element.src updates in JavaScript.

👀 Live Preview

An <img> loaded via src:

Demo image loaded with src attribute

The browser fetches the URL in src and displays the image.

Example 1 — Image Element

The <img> element uses src to specify the image file and alt for accessibility:

src.html
<img src="example.jpg" alt="An example image">
Try It Yourself

How It Works

The browser requests example.jpg, decodes it, and paints it inline. If the file fails to load, the alt text is shown instead.

Example 2 — Script Element

Load an external JavaScript file with <script src>:

script-src.html
<script src="script.js"></script>
Try It Yourself

How It Works

When src is present, the script content comes from the external file. Leave the tag empty—do not mix inline code with src.

Example 3 — Video Element

Embed video with src on a <source> child inside <video>:

video-src.html
<video width="320" height="240" controls>
  <source src="example.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>
Try It Yourself

How It Works

The browser picks the first supported source. The fallback text inside <video> appears only if no source works.

Dynamic Values with JavaScript

Change the loaded resource at runtime by assigning to element.src:

dynamic-src.html
<img id="dynamicImage" src="photo-a.jpg" alt="Gallery image">

<script>
  document.getElementById("dynamicImage").src = "photo-b.jpg";
</script>
Try It Yourself

How It Works

In this script, src is set to photo-b.jpg (or new-example.jpg) on the image with id dynamicImage. The same pattern works for iframe.src and other elements that expose a src IDL attribute.

♿ Accessibility

  • Images need alt text — Every <img src> should have a descriptive alt attribute (or alt="" if decorative).
  • Iframes need titles — Set title on <iframe src> so screen reader users know what the embedded frame contains.
  • Video and audio — Provide captions with <track src> for prerecorded media when possible.
  • Do not rely on color in images alone — Important information in src images should also appear in text.
  • Motion and autoplay — Respect user preferences; avoid unexpected autoplaying media loaded via src.

🧠 How src Works

1

Author sets src URL

Path or absolute link.

Markup
2

Browser fetches resource

HTTP request to URL.

Network
3

Browser embeds content

Render, play, or execute.

Display
=

External content in page

Images, scripts, media.

Browser Support

The src attribute is supported on all relevant HTML elements in every modern browser. Supported file types depend on the element and browser codecs.

HTML · Universal

Core embedding attribute

src has been fundamental since the early web for images and scripts.

100% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
src attribute 100% supported

Bottom line: Use src confidently; validate URLs and file types for your target browsers.

💡 Best Practices

✅ Do

  • Use valid, accessible URLs and test that files exist
  • Prefer relative or root-relative paths within your project
  • Add defer or async on external scripts when appropriate
  • Provide alt on images and title on iframes
  • Use multiple <source src> entries for media fallbacks

❌ Don’t

  • Load scripts from untrusted third-party URLs without review
  • Mix inline script code with script src
  • Confuse src (embed) with href (link)
  • Forget URL encoding for spaces and special characters
  • Rely on broken paths—always handle missing images gracefully

Conclusion

Understanding how to use the src attribute is fundamental for embedding external content in HTML. Whether it is images, scripts, iframes, or multimedia files, src provides a reliable way to connect your markup to files on the server or CDN.

Use correct URL paths, pair resources with accessibility attributes, and consider performance options like defer, async, and responsive srcset for images. With these habits, external resources integrate seamlessly into your pages.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about src

Bookmark these before embedding your next resource.

5
Core concepts
📸 02

img & script

Most common

Elements
📁 03

Relative paths

Project files

URLs
⚙️ 04

.src JS

Swap runtime

Dynamic
🔗 05

Not href

Embed vs link

Compare

❓ Frequently Asked Questions

It specifies the URL of an external resource to load and embed, such as an image, script, iframe document, or media file.
Common elements include img, script, iframe, audio, video, source, and track.
src embeds content into the page. href links to a resource without embedding it (used on a and link).
Yes. Paths like images/logo.png or /assets/app.js are resolved relative to the page or site root.
Yes. Assign a new URL to element.src to load a different resource at runtime.
Yes on all relevant HTML elements in every modern browser. Media format support varies by codec.

Embed resources the right way

Practice img src, script src, and dynamic updates in the Try It editor.

Try img src example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful