External files with <link> and <script>

In this section of our HTML tutorial, we will delve into the importance of linking external resources to HTML documents using <link> and <script> tags. These tags are fundamental for including stylesheets and JavaScript files in your web pages, enhancing their functionality and design.

Linking External Stylesheets with <link> Tags

The <link> tag is used to link external CSS (Cascading Style Sheets) files to an HTML document. This allows you to separate your page’s structure from its presentation, making it easier to maintain and style your website consistently.

Basic Syntax

Here is the basic syntax for using a <link> tag to link an external CSS file:

  • rel="stylesheet": Specifies that the linked resource is a stylesheet.
  • type="text/css": Defines the type of the linked resource (CSS in this case).
  • href="styles.css": Provides the path to the external CSS file.

Linking External JavaScript with <script> Tags

The <script> tag is used to include external JavaScript files in an HTML document. This allows you to add interactivity and functionality to your web pages.

Basic Syntax

Here is the basic syntax for using a <script> tag to link an external JavaScript file:

  • src="script.js": Specifies the path to the external JavaScript file.

Browser Compatibility

Both <link> and <script> tags are widely supported across modern web browsers. However, it’s important to specify the type attribute for <script> tags with values like “text/javascript” for optimal compatibility.

Code Examples

Here are examples of HTML documents that link external resources:

Linking an External CSS File

Linking an External JavaScript File

Conclusion

Using <link> and <script> tags to link external resources is essential for creating well-structured and functional web pages. External stylesheets allow you to define the presentation of your content, while external JavaScript files provide interactivity and functionality. As you progress in your web development journey, mastering these techniques will help you build dynamic and appealing websites that meet the needs of your audience.