HTML
CSS
JavaScript
jQuery
PHP
SQL
WordPress
HTML Tutorials

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>
				
			

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>
				
			

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.

HTML
CSS
JavaScript
jQuery
PHP
SQL
WordPress

Why 1SMARTchicken?

This site was built and is maintained to benefit my autistic son.
See More →

My Son's Name is Johnny

He was diagnosed as autistic quite late, at age four...
His story

Buy Me a Coffee

Thanks for your support!

Feedback

If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.