HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML src Attribute
Photo Credit to CodeToFun
🙋 Introduction
The src
attribute is a crucial attribute in HTML that specifies the source URL of embedded content like images, scripts, iframes, audio, and video elements.
It plays a key role in linking external files to your HTML documents.
🎯 Purpose of src
The primary purpose of the src
attribute is to define the source location of a resource that needs to be embedded or linked in an HTML document.
Depending on the HTML element it is used with, the src
attribute points to different types of external content, such as images, scripts, multimedia files, or other documents.
💎 Values
The src
attribute accepts various values depending on the element it is used with. Here are some common values:
- URLs: For elements like images, audio, and video, the
src
attribute typically points to the URL of the external file. - Script Files: When used in script tags (<script>), the
src
attribute points to the URL of an external JavaScript file. - Iframes: The
src
attribute in the <iframe> tag points to the URL of the embedded content or web page.
📄 Example 1: Image Element
The HTML <img> element with src="example.jpg" specifies an image file named "example.jpg" as the source, displaying it on the webpage with alternative text An example image in case the image cannot be loaded.
<img src="example.jpg" alt="An example image">
📄 Example 2: Script Element
This HTML code includes an external JavaScript file, "script.js," using the src
attribute within a <script> tag, allowing the browser to load and execute the specified script in the document.
<script src="script.js"></script>
📄 Example 3: Video Element
The HTML program embeds a video player with specified dimensions, allowing users to control playback, and sources the video content from the file "example.mp4" using the src
attribute within the <source> element.
<video width="320" height="240" controls>
<source src="example.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
🔄 Dynamic Values with JavaScript
You can dynamically set the src
attribute using JavaScript.
This is useful when you want to change the source of an element based on certain conditions or user interactions. Here's a brief example:
<script>
// Dynamically set the src attribute for an image element
document.getElementById("dynamicImage").src = "new-example.jpg";
</script>
🧠 How it Works
In this script, the src
attribute is dynamically set to new-example.jpg for an image element with the id "dynamicImage."
🏆 Best Practices
- Ensure that the value provided to the
src
attribute is a valid and accessible URL or file path. - Use relative paths when linking to files within your project structure.
- When linking external scripts or resources, consider using the async or defer attribute for better page loading performance.
🎉 Conclusion
Understanding how to use the src
attribute is fundamental for embedding external content in your HTML documents.
Whether it's images, scripts, or multimedia files, the src
attribute provides a reliable way to integrate external resources seamlessly.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML src Attribute), please comment here. I will help you immediately.