
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to align text content into center using JavaScript?
We can align text content into the center using JavaScript by manipulating the CSS properties of the element. This can be done by selecting the element and setting the "text-align" property to "center". Additionally, we can also use the "margin" property to adjust the positioning of the element.
Approach
There are multiple ways to center text content using JavaScript −
The most common way is to set the text-align property to “center” in the CSS stylesheet.
Another way is to use the <center> tag. Finally, you can use the margin property to center text content.
Example
Below we will be seeing the code centering the text using only JavaScript.
<html> <head> <title>JavaScript - Center Text</title> </head> <body> <script> const centerText = () => { var centerText = document.createElement('p'); centerText.innerText = 'Center Text'; centerText.style.color = 'black'; centerText.style.textAlign = 'center'; document.body.appendChild(centerText); } centerText(); </script> </body> </html>
Example
Let us see another example to align text content into center −
<!DOCTYPE html> <html> <head> <title> Align Text </title> </head> <body> <h1>Demo Heading</h1> <p id="demo"> This text will be center aligned </p> <button onclick="demoFunc();">Align</button> <script> function demoFunc() { let txt = document.getElementById("demo"); txt.style.textAlign = "center"; } </script> </body> </html>
- Related Articles
- How to align flexbox container into center using JavaScript?
- Center image using text-align center with CSS?
- How to center align text in table cells in HTML?
- Usage of CSS align-content property center value
- How to Align the modal content box to the center of any screen?
- How to center align component using BoxLayout with Java?
- How to Align Navbar Items to Center using Bootstrap 4?
- How to Align Text Strings using Python?
- How to Justify Text using text-align & text-justify CSS Properties?
- How to align block elements to the center?
- How to make a div center align in HTML?
- How to center text in HTML?
- How to center a Text object horizontally on canvas using FabricJS?
- How to center a Text object vertically on canvas using FabricJS?
- CSS align-content property

Advertisements