HTML - src Attribute



The HTML src attribute is used to represent external resources such as images, videos, audio, and URLs in the page from the external or device resource. In other words, this attribute specifies the location (URL) of the external resource.

The src attribute is applicable to the following elements −

  • <audio>
  • <embed>
  • <img>
  • <iframe>
  • <input>
  • <script>
  • <source>
  • <track>
  • <video>

Syntax

Following is the syntax of the src attribute −

<element src="URL" />

Where, element should be one of the elements listed above.

Example

In the following example, let’s demonstrate the usage of the src attribute in the <input> tag, as follows−

<!DOCTYPE html>
<html>
<head>
   <title>HTML input src Attribute</title>
   <style>
      body {
         text-align: center;
      }
   </style>
</head>
<body>
   <h1 style="color: green;">tutorials <span style="color: black">point</span>
   </h1>
   <h2>THE input src Attribute</h2>
   <form action="https://tutorialspoint.com/" method="post">
      <input id="myImage" type="image" src="https://www.tutorialspoint.com/images/logo.png?v2" alt="Submit" width="200" height="60" />
   </form>
</body>
</html>

When we run the above code, it will generate an output consisting of the text along with a image with hyperlink on the webpage. when the user tries to click on the image it will gets redirect.

Example

Considering the following example, where we are going to use the src attribute with the image tag.

<!DOCTYPE html>
<html>
<head>
   <title>HTML src Attribute</title>
   <style>
      body {
         text-align: center;
      }
   </style>
</head>
<body>
   <h1 style="color: green;">tutorials <span style="color: black">point</span>
   </h1>
   <h2>HTML src Attribute</h2>
   <p>src attribute in img to display the external image on the page</p>
   <img src="https://www.tutorialspoint.com/images/logo.png?v2" alt="logo" />
</body>
</html>

On running the above code, the output window will pop up displaying the text along with a image on the webpage.

Example

Let's look at the following example, where we are going to use the src attribute with the video element.

<!DOCTYPE html>
<html>
<head>
   <title>HTML src Attribute</title>
   <style>
      body {
         text-align: center;
      }
   </style>
</head>
<body>
   <h1 style="color: green;">tutorials <span style="color: black">point</span>
   </h1>
   <h2>HTML src Attribute</h2>
   <p>src attribute in video to display the external video on the page</p>
   <video width="380" height="220" controls>
      <source src="https://samplelib.com/lib/preview/mp4/sample-5s.mp4" type="video/mp4">
   </video>
</body>
</html>

When we run the above code, it will generate an output consisting of the video displayed on the webpage.

Example

Following is the example, where we are going to use the src attribute with the embed tag.

<!DOCTYPE html>
<html>
<head>
   <title>HTML src Attribute</title>
   <style>
      body {
         text-align: center;
      }
   </style>
</head>
<body>
   <h1 style="color: green;">tutorials <span style="color: black">point</span>
   </h1>
   <h2>The embed src attribute</h2>
   <embed src="https://www.tutorialspoint.com/images/logo.png?v2">
</body>
</html>

On running the above code, the output window will pop up displaying the text along with a image on the webpage.

html_attributes_reference.htm
Advertisements