Overview
An external style sheet and external JavaScript file are used to define the various styles and actions respectively while keeping everything centralized. This is a best practice because if you were to place your scripts and styles directly in the <head> of each document, there would be no centralized location to make your changes and you’d have to search for all the various rules across multiple locations. See Placing Styles and Scripts Directly in a Document for more information.
The following shows where to place our links to our external script file(s) and CSS file(s). Typically this is done directly after the <title> tag, but could be placed anywhere in the <head> of the document.
<head>
<meta charset="UTF-8">
<meta name="description" content="Free Web Tutorials">
<meta name="keywords" content="HTML, CSS, JavaScript, PHP, SQL">
<meta name="author" content="1SMARTchicken">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Advanced HTML Template</title>
<!-- styles -->
<link rel="stylesheet" type="text/css" href="/docs/style.css">
<link rel="stylesheet" type="text/css" href="/docs/sidebar-styles.css">
<!-- scripts -->
<script src="/scripts/main.js"></script>
</head>
The <link> tag is used to link to external style sheets. The type attribute tells the browser what kind of document to expect and the href attribute is the target URL for where the document lives.
Similarly, the <script> tag is used to link to an external JavaScript file, and requires the type attribute to tell the browser to expect JavaScript and the src attribute which tells the browser where the document lives in the file structure.
Non-Crucial Scripts
Non-crucial scripts can be placed just before the closing </body> tag which can speed up the page load time because the page can first render before applying the scripts.
<!-- scripts -->
<script type="text/javascript" src="/scripts/main.js"></script>
</body>
</html>
Note
The order you link to a style sheet is important. That’s why they’re called Cascading Style Sheets. The rules cascade down the list of documents and if there is a conflict, the last rule loaded is what the browser applies unless one of the rules has more specificity or includes the !important rule.
HTML Notes:
- In our HTML section the term “tag” and “element” are often used interchangeably to refer to both the tag used to create a page element and the element created by the tag (<p> tag = <p> element = paragraph on the page)
- HTML5 is not case sensitive; so <P> is the same as <p>, <H1> is the same as <h1>
- Global attributes can be used with all HTML tags and are therefore not mentioned on every tag page
- To write clean, readable HTML code, it is best to use indentation whereas elements within elements are indented (tabbed or spaces) to create something that looks like a project outline
- The browser will automatically remove any extra spaces and lines in your HTML code when the page is displayed
- Double quotes or single quotes can be used around HTML attribute values, but when the attribute value itself contains one form of quote, it will be necessary to use the other around the attribute
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.