Home >Web Front-end >CSS Tutorial >src (HTML attribute)
The src
attribute in HTML specifies the location of an embedded resource, most commonly images, but also applicable to audio, video, and iframes. This location can be relative to the current webpage, relative to the server's root directory, or a complete URL.
Relative Paths:
<img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174052993373573.jpg" class="lazy" alt="src (HTML attribute) ">
(image.jpg is in the same folder as the HTML file).<img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174052993391833.jpg" class="lazy" alt="src (HTML attribute) ">
(image.jpg is one directory above the current HTML file).<img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174052993464780.jpg" class="lazy" alt="src (HTML attribute) ">
(image.jpg is in the /images
folder at the website's root).Absolute URLs:
For images hosted on a different server, use a full URL: <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174052993538767.jpg" class="lazy" alt="Image Description">
.
Example:
The following code displays an image named myimage.png
located in the same directory as the HTML file:
<code class="language-html"><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/174052995196248.png" class="lazy" alt="src (HTML attribute) "></code>
Key Considerations:
alt
Attribute: Always include the alt
attribute to provide alternative text for screen readers and when the image fails to load. This is crucial for accessibility.src
attribute should accurately reflect the file type (e.g., .jpg
, .png
, .gif
, .mp4
, .mp3
).Frequently Asked Questions (FAQs):
While the provided text covers many aspects of the src
attribute, here's a concise summary addressing key points:
src
? src
(source) specifies the URL of an embedded resource.src
? <img alt="src (HTML attribute)" >
, <audio></audio>
, <video></video>
, <iframe></iframe>
, and others embedding external content.src
is missing? The image (or other embedded resource) won't display.src
handle different domains? Yes, but subject to CORS restrictions.<source></source>
elements within <picture></picture>
, <audio></audio>
, or <video></video>
tags for multiple sources.src
vs. href
? src
embeds content; href
creates a link to content.This revised answer provides a more streamlined explanation and addresses the FAQs in a clearer, more concise manner.
The above is the detailed content of src (HTML attribute). For more information, please follow other related articles on the PHP Chinese website!