HTML script tag | Explained
An HTML file consists of three main components header, body, and footer. Inside these elements, you can add other elements as nested elements, such as paragraphs, images, headings, labels, and more. Moreover, HTML also permits us to utilize executable code in it using script elements.
In this manual, we will explain what HTML “<script>” tag is and how to use it. So, let’s go!
What is HTML <script> Tag?
How to Use HTML <script> Tag?
There are two ways to define an HTML <script> tag:
- To embed script code in HTML
Let’s first check out the syntax of the script element.
In the above syntax, “<script>” is the starting tag, and “</script>” is the ending tag of the HTML script element. Between the starting and ending tags, add the code you want to execute.
How to Embed script in HTML?
Step 1: Create HTML File
In the below-given code block:
- We have placed a div class named “main” inside the body element.
- In this div, a heading is placed using the <h1> tag.
- Then, a function named “show()” is defined that will display an alert box on the screen.
Step 2: Style main div
Let’s style the main div:
- First, “.main” is utilized to access the main class.
- Aligned the text in the center by specifying its value as “center”.
- “height” is set as “200px”.
- “background-color” is set as “#e5989b”.
- “margin” is set as “auto” to center the element within its container horizontally.
It can be observed that pressing the added button has executed the code added in the script tag:
How to Add Link to the External Script Code?
The output of the above-described code can be seen below:
We have compiled the essential information related to the HTML script tag.