Overview
Styles and scripts can be placed directly in the <head> of the document or even inline with the element they are modifying, but neither are good practice. It would be better to link to external scripts and/or style sheets.
However, if you must place them in the document itself, the following shows where we want to place our script(s) and style(s). Typically this is done directly after the <title> tag.
The <style> tag is used to enclose all the styles we want to place in the <head> of our document. Style chunks like this can only be placed in the <head> of the document.
Similarly, the <script> tag is used to enclose all our scripts. Typically they are placed in the <head> of the document, but can be placed anywhere in the <head> or even <body> of the document if necessary.
<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 -->
<style>
#primary {
color: red;
font-size: 18px;
}
#secondary {
margin-top: 35px;
}
</style>
<!-- scripts -->
<script>
function myFunction() {
var element = document.getElementById('myDIV');
element.classList.add('mystyle');
}
</script>
</head>
Placing a Script in the Body
Scripts can also 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>
function myFunction() {
var element = document.getElementById('myDIV');
element.classList.add('mystyle');
}
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>
</body>
</html>
Note
Technically, a <script> can be placed anywhere in the body of the page. If doing so, it is good practice to place a <noscript> tag just after the <script> tag for browsers that cannot run JavaScript for some reason.
Inline Styles
Styles can also be placed directly within an element using the <style> tag.
It would be rare that you would want to do something like this since it would override any other rules you have in the <head> or in an external style sheet. This would also be difficult to locate and change styles if you did this throughout a page, or worse yet, a complete site.
<body>
<h1 style="color:green;">This heading is green</h1>
<p style="color:blue;">This paragraph is blue.</p>
</body>
Note
The order you link to a style sheet is important. That’s why they are 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 specificityor 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.