HTML src Attribute

Beginner
4 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The src attribute specifies the URL of an external resource an element should load. You will see it most often on <img>, <script>, <iframe>, <audio>, and <video>.

The value is a valid URL string — absolute (https://…) or relative (/images/photo.jpg). Not the same as srcset (responsive candidates) or srcdoc (inline iframe HTML).

Baseline · Widely available.

Not deprecated. Core HTML attribute on media and embedding elements across all browsers.

Empty or broken URLs fail silently for users.

Always verify the path. For images, pair src with meaningful alt text.

HTML Syntax

syntax.html
<element src=" url">

For <img>, MDN: at least one of src or srcset is required. When both exist with width descriptors, supporting browsers use srcset/sizes and keep src as a fallback.

Attribute Values

Common URL forms:

ValueDescriptionTry it
/images/photo.jpgRoot-relative path on the same siteTry it
https://example.com/a.jsAbsolute URL (often for CDNs / scripts)Try it
./media/clip.mp4Relative to the current document URLTry it

Live Preview

Change the src on an image. The preview shows which URL is loaded.

Same <img> element — only the src URL changes.

Image
Live src demo image

HTML Examples

Short study snippets. Use Try It Yourself for a live editor.

Example 1 — Image URL

The classic use: load a picture and describe it with alt.

HTML
<img src="/images/javascript.png"
     alt="JavaScript logo"
     width="120"
     height="120">
Try It Yourself

How It Works

  1. src points the browser at the image file.
  2. alt describes the image if it fails or for screen readers.
  3. width/height reserve space and reduce layout shift.
  4. Root-relative paths start with / from the site root.

Example 2 — External Script

Load JavaScript from a file instead of writing it inline.

HTML
<!-- External file -->
<script src="/js/app.js" defer></script>

<!-- Demo: tiny inline data URL so this page runs alone -->
<p id="msg">Loading…</p>
<script src="data:text/javascript,document.getElementById('msg').textContent='Hello from src!'"></script>
Try It Yourself

How It Works

  1. script src fetches an external JavaScript file.
  2. defer (when used) runs after HTML is parsed, in order.
  3. Empty body — when src is set, do not put code inside the same classic script tag.
  4. Study tip — the try-it demo uses a data URL so it works without a separate .js file.

Example 3 — Audio and Video

Point media elements at a file with src (or nest <source> children).

HTML
<audio src="/audio/count.mp3" controls>
  Your browser does not support audio.
</audio>

<video src="/video/count.mp4" controls width="320">
  Your browser does not support video.
</video>
Try It Yourself

How It Works

  1. src is the media file URL.
  2. controls shows play/pause UI for users.
  3. Fallback text inside the tags helps when media is unsupported.
  4. Multiple formats? Prefer nested <source> elements instead of one src.

Edge Cases & Pitfalls

Most src bugs are wrong paths or mixing it up with similar attributes.

path

Relative paths depend on the page URL

photo.jpg resolves from the current document folder. Prefer clear root-relative paths when unsure.

vs

src ≠ srcset ≠ srcdoc

srcset = candidates; srcdoc = iframe HTML string; src = one resource URL.

script

Do not mix src and inline classic code

If a classic <script> has src, put the code in that file — not inside the tag.

img

Always provide alt for images

Broken src still needs accessible alt. Decorative images can use empty alt="".

cors

Cross-origin rules still apply

Scripts, modules, and some media from other origins may need CORS headers to work fully.

Browser Support

src is a core HTML attribute for loading external resources on media and embedding elements.

✓ Baseline · Widely available

src

Supported everywhere for img, script, iframe, audio, video, and related elements.

100% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Full support
src Universal

Bottom line: Universal. Focus on correct URLs, accessibility (alt), and the right sibling attributes (srcset, srcdoc) for each use case.

Key Takeaways

  • Role: URL of the resource to load.
  • Elements: img, script, iframe, audio, video, and more.
  • Paths: absolute or relative — check resolution carefully.
  • Siblings: use srcset / srcdoc when those jobs fit better.

One line: src is “where is the file?” for images, scripts, and media.

Frequently Asked Questions

src sets the URL of the external resource an element should load — for example an image file, a JavaScript file, a media file, or a framed page.
Commonly img, script, iframe, audio, video, embed, and track. source and input type=image can also use src. Always check the element’s MDN page for exact rules.
No. src is a single URL (or fallback). srcset lists multiple image candidates with width or density descriptors for responsive selection.
No. srcdoc embeds HTML content directly inside an iframe. src loads a document from a URL.
Yes. Relative URLs resolve against the document (or base) URL. Absolute URLs include a scheme such as https:.
The resource fails to load. For img, provide meaningful alt text. Empty or broken src URLs are a common cause of missing media.

Next: srcdoc

Embed HTML directly inside an iframe without a separate URL.

srcdoc tutorial →

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.

8 people found this page helpful