Examples
Create a text node and append it to the document:
let textNode = document.createTextNode["Hello World"]; document.body.appendChild[textNode];
Try it Yourself »
Create a
element with a text node:
const h2 = document.createElement["h2"];
const textNode = document.createTextNode["Hello World"];
h2.appendChild[textNode];
Try it Yourself »
Create a
element with a text node:
const para = document.createElement["p"];
const textNode = document.createTextNode["Hello World"];
para.appendChild[textNode];
Try it Yourself »
HTML Elements are Nodes
All HTML elements are nodes.
Elements are nodes. Attributes are nodes. Texts are nodes.
Some elements contain other nodes.
Some elements contain text nodes.
Some elements consain attribute nodes.
Definition and Usage
The createTextNode[]
method creates a text node.
Syntax
document.createTextNode[text]
Parameters
Parameter | Description |
text | Required. The text for the node. |
Return Value
Type | Description |
Node | The created text node. |
Browser Support
document.createTextNode[]
is a DOM Level 1 [1998] feature.
It is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
Example
The six different HTML headings:
This is heading 1
This is heading 2
This is heading 3
This is heading 4
This is heading 5
This is heading 6
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The to
tags are used to define HTML headings.
defines the most important heading.
defines the least important heading.
Note: Only use one per page - this should represent the main heading/subject for the whole page. Also, do not skip heading levels - start with
, then use
, and so on.
Browser Support
- | Yes | Yes | Yes | Yes | Yes |
Global Attributes
The to
tags also supports the Global Attributes in HTML.
Event Attributes
The to
tags also supports the Event Attributes in HTML.
More Examples
Example
Set the background color and text color of headings [with CSS]:
Hello World
Hello World
Try it Yourself »
Example
Set the alignment of headings [with CSS]:
This is heading 1
This is heading 2
This is heading 3
This is heading 4
Try it Yourself »
Related Pages
HTML tutorial: HTML Headings
HTML DOM reference: Heading Object
Default CSS Settings
Most browsers will display the element with the following default values:
Example
h2 {
display: block;
font-size: 2em;
margin-top: 0.67em;
margin-bottom: 0.67em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
Try it Yourself »
Most browsers will display the element with the following default values:
Example
h2 {
display: block;
font-size: 1.5em;
margin-top: 0.83em;
margin-bottom: 0.83em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
Try it Yourself »
Most browsers will display the element with the following default values:
Example
h3 {
display: block;
font-size: 1.17em;
margin-top: 1em;
margin-bottom: 1em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
Try it Yourself »
Most browsers will display the element with the following default values:
Example
h4 {
display: block;
font-size: 1em;
margin-top: 1.33em;
margin-bottom: 1.33em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
Try it Yourself »
Most browsers will display the element with the following default values:
Example
h5 {
display: block;
font-size: .83em;
margin-top: 1.67em;
margin-bottom: 1.67em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
Try it Yourself »
Most browsers will display the element with the following default values:
Example
h6 {
display: block;
font-size: .67em;
margin-top: 2.33em;
margin-bottom: 2.33em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
Try it Yourself »
How to create a new h1 element with JavaScript?
Is there a way to change the text of a H1?
How to create a variable with H1 title?
How to get the value of H1 to JavaScript variable?
What is h1 tag in JavaScript?
to tags are used to define HTML headings. defines the most important heading. defines the least important heading. Note: Only use one per page - this should represent the main heading/subject for the whole page.
per page - this should represent the main heading/subject for the whole page.
Where do you put the h1 element?
How do you create a new tag in JavaScript?
tags..
using document. createElement["p"]..