The src attribute specifies the URL of the page to embed.

If the src attribute is not specified, nothing will be displayed on the page.

The src attribute can be used on the following elements: <frame>, <iframe>, <img>, <input>, <script>, and <video>.

The location of the external file can come in two ways:

  1. The first one is the local file that you saved on the computer, and you can access it by writing the file directory as ./folder/image.png.
  2. The second one is the online file that you get from the internet, and you can access it by writing the URL as https://example.com/image.png.

An example code

Here are some examples of how to use the src attribute in an HTML file.

An image source file:

<img src="./Rick Astley - Never Gonna Give You Up.png" alt="Rick Astley" />

A video source file:

<video controls>
  <source src="./Rick Astley - Never Gonna Give You Up.webm" type="video/webm"/>
</video>

An audio source file:

<audio controls>
  <source src="./Rick Astley - Never Gonna Give You Up.mp3" type="audio/mp3" />
</audio>

Notice about src attribute

While the src attribute is usually used in img, video, and audio, it also supports other elements like embed, iframe, input, script, input, source, and track.

Get my free e-book to prepare for the technical interview or start to Learn Full-Stack JavaScript