👀 Live Preview
An <img> loaded via src:
The browser fetches the URL in src and displays the image.

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.
Image files.
External JS.
Video/audio.
Relative paths.
Change .src.
Embed vs link.
srcThe 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.
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.
Set src to a valid URL string on supported elements:
<img src="photo.jpg" alt="Description">
<script src="app.js" defer></script>
<iframe src="https://example.com/embed" title="Embedded page"></iframe><img>: required unless using certain SVG patterns; always include alt.<script src>: do not put inline code inside the tag—use a separate file or omit src.<video> / <audio>: prefer <source src> for multiple formats.%20, etc.).The src attribute accepts a URL. Common patterns:
src="images/logo.png" (relative to the current page URL).src="/assets/app.js" (from the site root).src="https://cdn.example.com/lib.js".src="data:image/svg+xml,..." for inline small assets.<source src="clip.mp4" type="video/mp4"> inside <video>.<!-- 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>| Element | Loads | Example |
|---|---|---|
<img> | Image file | src="photo.jpg" |
<script> | JavaScript file | src="app.js" |
<iframe> | HTML document | src="/embed" |
<source> | Media file | src="clip.webm" |
<audio> | Audio file | src="song.mp3" |
| JavaScript | el.src = url | Swap at runtime |
| Element | Uses src? | Notes |
|---|---|---|
<img> | Yes | Primary image URL; pair with alt |
<script> | Yes | External JS; use defer or async when needed |
<iframe> | Yes | Embedded browsing context; set title |
<video> / <audio> | Yes | Direct src or via <source> children |
<source> | Yes | Inside video, audio, or picture |
<a>, <link> | No | Use href instead |
src vs href vs srcset vs srcdoc| Attribute | Purpose | Typical element |
|---|---|---|
src | Embed resource URL | img, script, iframe |
href | Link to resource | a, link |
srcset | Multiple image candidates | img, source |
srcdoc | Inline HTML for iframe | iframe |
img, script, video/source, and dynamic element.src updates in JavaScript.
An <img> loaded via src:
The browser fetches the URL in src and displays the image.
The <img> element uses src to specify the image file and alt for accessibility:
<img src="example.jpg" alt="An example image">The browser requests example.jpg, decodes it, and paints it inline. If the file fails to load, the alt text is shown instead.
Load an external JavaScript file with <script src>:
<script src="script.js"></script>When src is present, the script content comes from the external file. Leave the tag empty—do not mix inline code with src.
Embed video with src on a <source> child inside <video>:
<video width="320" height="240" controls>
<source src="example.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>The browser picks the first supported source. The fallback text inside <video> appears only if no source works.
Change the loaded resource at runtime by assigning to element.src:
<img id="dynamicImage" src="photo-a.jpg" alt="Gallery image">
<script>
document.getElementById("dynamicImage").src = "photo-b.jpg";
</script>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.
<img src> should have a descriptive alt attribute (or alt="" if decorative).title on <iframe src> so screen reader users know what the embedded frame contains.<track src> for prerecorded media when possible.src images should also appear in text.src.Path or absolute link.
HTTP request to URL.
Render, play, or execute.
Images, scripts, media.
The src attribute is supported on all relevant HTML elements in every modern browser. Supported file types depend on the element and browser codecs.
src has been fundamental since the early web for images and scripts.
Bottom line: Use src confidently; validate URLs and file types for your target browsers.
defer or async on external scripts when appropriatealt on images and title on iframes<source src> entries for media fallbacksscript srcsrc (embed) with href (link)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.
srcBookmark these before embedding your next resource.
Where to load
PurposeMost common
ElementsProject files
URLsSwap runtime
DynamicEmbed vs link
Compareimg, 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).images/logo.png or /assets/app.js are resolved relative to the page or site root.element.src to load a different resource at runtime.Practice img src, script src, and dynamic updates in the Try It editor.
5 people found this page helpful