Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to Create a Link in JavaScript ?
Next article icon

How to Make a Breakline in JavaScript for innerHTML?

Last Updated : 28 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In JavaScript, while working with HTML you will frequently need to add line breaks whether building a complex or simple web application. In this article, you will learn to insert line breaks in JavaScript's innerHTML.

These are the following approaches:

Table of Content

  • Using < br > Tag
  • Using Template Literals
  • By Creating Elements
  • Using CSS

Using <br> Tag

For basic line breaks, you can directly add <br> to your text easily.But for larger projects or when you need to add multiple line breaks, it is convenient to use more efficient methods.

Example: This illustrates a div element with two lines of text, separated by a line break, using JavaScript's innerHTML property.

HTML
<!DOCTYPE html> <html>  <head>     <title>Page Title</title> </head>  <body>     <div id="myElement"></div>     <script>         let element = document.getElementById('myElement');         element.innerHTML = 'lineone<br>linetwo';     </script> </body>  </html> 

Output:

Screenshot-2024-06-06-101217
Output

Using Template Literals

Template literals make adding line breaks cleaner and easier than string concatenation. They are used for multiline strings and also improves code readability. But remember, any indentation within template literals will show up in the output, which is not always favourable.

Example: This illustrates how to insert two lines of text into a div element using JavaScript's innerHTML property.

HTML
<!DOCTYPE html> <html>  <head>     <title>Page Title</title> </head>  <body>     <div id="myElement"></div>      <script>         let element = document.getElementById('myElement');         element.innerHTML = `lineone                              linetwo`;     </script> </body>  </html> 

Output:

Screenshot-2024-06-06-101331
using template literals

By Creating Elements

To insert line breaks, you can create a <br> element and append it to the target element of the webpage. This method is particularly useful while dealing with more complex DOM manipulation scenarios or when you need to insert line breaks dynamically based on specific conditions.

Example: This illustrates how to add a line break and text to a div element using JavaScript.

HTML
<!DOCTYPE html> <html>  <head>     <title>Page Title</title> </head>  <body>     <div id="myElement">lineone</div>      <script>         let element = document.getElementById('myElement');         let br = document.createElement('br');         element.appendChild(br);         element.innerHTML += 'linetwo';     </script> </body>  </html> 

Output:

Screenshot-2024-06-06-101527
By creating element

Using CSS

You can simply use CSS to control the spacing between lines instead of adding <br> tags directly. This method is more accessible and provides consistent line spacing. Simply use white-space: pre-line; CSS property to the element or its parent to automatically create line breaks according to the content provided.

Example: This illustrates a webpage with a div element styled to preserve line breaks within its content.

HTML
<!DOCTYPE html> <html>  <head>     <title>Page Title</title>     <style>         .line-break {             white-space: pre-line;         }     </style> </head>  <body>     <div id="myElement" class="line-break">         line one         line two     </div> </body>  </html> 

Output:

Screenshot-2024-06-06-101708

Next Article
How to Create a Link in JavaScript ?

S

shambhavim1gnc
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • HTML

Similar Reads

  • How to use innerHTML in JavaScript ?
    The innerHTML property in JavaScript allows you to get or set the HTML content of an element as a string. Syntax ForGetting HTML content of an element:let element = document.getElementById("myElementId"); let htmlContent = element.innerHTML;Setting HTML content of an element:let element = document.g
    2 min read
  • How to create HTML List from JavaScript Array?
    Creating an HTML list from a JavaScript array allows you to dynamically generate <ul> (unordered) or <ol> (ordered) lists and fill them with data directly from your array. This is useful when you want to display array data in a structured way on your webpage. In this guide, we’ll go thro
    3 min read
  • How to Create a Link in JavaScript ?
    In JavaScript, a link typically refers to creating HTML hyperlinks (<a> tags) dynamically using JavaScript. You can link using a document.createElement('a'), setting attributes like href and textContent, and then appending it to the document with appendChild(). Here are some common approaches
    4 min read
  • How to Create a New Line in JavaScript ?
    A new line can be made in JavaScript using the below-discussed methods. Using Escape Sequence `\n`The \n escape sequence represents a newline character in JavaScript strings and it can used to render a new line in JavaScript. [GFGTABS] JavaScript console.log("GfG"); console.log("\n
    3 min read
  • How to break JavaScript Code into several lines ?
    In this article, we will discuss how to break javascript code into several lines. We generally break the code into several lines so that it can be read easily by a third person or the programmer itself. There are a few ways to break JavaScript code into several lines: We can use the backslash \ char
    1 min read
  • How to insert a line break in PHP string ?
    In this article, we will discuss how to insert a line break in a PHP string. We will get it by using the nl2br() function. This function is used to give a new line break wherever '\n' is placed. Syntax: nl2br("string \n");where the string is the input string. Example 1: PHP Program to insert a line
    2 min read
  • How to create an element from a string in JavaScript ?
    In this article, we will learn how to create an element from a string using JavaScript. This can be used in situations where dynamically generated elements are required by the user. This can be achieved using many approaches as given below: Table of Content Using the createElement() methodUsing the
    3 min read
  • JavaScript - Insert Character in JS String
    In JavaScript, characters can be inserted at the beginning, end, or any specific position in a string. JavaScript provides several methods to perform these operations efficiently. At the BeginningTo insert a character at the beginning of a string, you can use string concatenation or template literal
    2 min read
  • How to include inline JavaScript in HAML ?
    There are mainly two ways to include JavaScript in HTML: Internal/Inline: By using "script" element in the "head" or "body" section. External: By using an external JavaScript file. We will be extending the below example for HTML and see how it could be implemented similarly for HAML. To include inli
    2 min read
  • How to break nested for loop using JavaScript?
    The break statement, which is used to exit a loop early. A label can be used with a break to control the flow more precisely. A label is simply an identifier followed by a colon(:) that is applied to a statement or a block of code. Note: there should not be any other statement in between a label nam
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences