Home > Article > Web Front-end > In HTML, what is the difference between src attribute and href attribute?
The src attribute and href attribute are commonly used attributes in HTML and are used to specify references to external resources in web pages. Although they can both be used to reference external resources, they have different uses and functions.
First of all, the src attribute is used to specify how external resources are embedded in web pages. It is often used to introduce media files such as images, audio, and video, and to reference JavaScript files. External resources can be embedded into HTML documents by using the src attribute.
The following is an example of using the src attribute to reference an image:
<img src="image.jpg" alt="图片">
In the above code snippet, the src attribute specifies the path to the image file. The browser will load and display the corresponding image based on the value of the src attribute. picture of.
In addition, examples of using the src attribute to introduce JavaScript code are as follows:
<script src="script.js"></script>
In the above code, the src attribute defines the path of the imported JavaScript file, and the browser will load it according to the value of the src attribute. and execute the JavaScript code in that file.
In contrast, the href attribute is used to specify how external resources are linked in a web page. It is often used to introduce CSS style sheet files and create links between other web pages.
The following is an example of using the href attribute to introduce a CSS style sheet:
<link href="style.css" rel="stylesheet">
In the above code, the href attribute specifies the path of the imported CSS file, and the browser will use the href attribute according to the value of the Loads and applies the style rules in this stylesheet file.
In addition, examples of using the href attribute to create links are as follows:
<a href="page2.html">跳转到第二页</a>
In the above code, the href attribute defines the path of the link target. When the user clicks on the text content, the browser will use the href attribute to The value jumps to the corresponding web page.
To summarize, the src attribute is mostly used to introduce media files or script files, while the href attribute is mostly used to introduce style sheet files and create links. The src attribute is used to embed external resources, while the href attribute is used to create connections to other web pages. Correct use of these two attributes can effectively manage and reference external resources, and improve the functionality and aesthetics of web pages.
The above is the detailed content of In HTML, what is the difference between src attribute and href attribute?. For more information, please follow other related articles on the PHP Chinese website!