How to Create Text Editor using HTML CSS and JavaScript?
Last Updated :
08 Nov, 2024
The style property in JavaScript is used to create a text editor with HTML and CSS. This editor features buttons for text formatting, including options for bold, italic, left, center, and right alignment, as well as uppercase, lowercase, and capitalized transformations.
Each button triggers a function that updates the textarea content based on the selected style, providing an interactive way to format text directly within the editor.
Creating a Text Editor using HTML, CSS, and JavaScript
- Create a structure using <textarea> for text input and buttons for formatting actions (like bold, italic, etc.).
- Style the text editor and buttons to make it visually appealing with colors, borders, and spacing.
- Add buttons that, when clicked, trigger specific JavaScript functions to apply formatting like bold, italics, text alignment, or case transformation.
- Each function manipulates the text inside the <textarea> by changing its style (e.g. fontWeight for bold, textAlign for alignment).
- The user can clear or change the text's appearance instantly by interacting with these buttons.
Example: Building a basic text editor with HTML, CSS, and JavaScript.
HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Text Editor</title>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.0/css/all.min.css">
</head>
<body>
<section>
<h1>TEXT EDITOR</h1>
<div class="button-container">
<button onclick="editoroption1()" title="Bold Text">
Bold
</button>
<button onclick="editoroption2()" title="Italic Text">
Italic
</button>
<button onclick="editoroption3()" title="Left Align">
<i class="fas fa-align-left"></i>
</button>
<button onclick="editoroption4()" title="Center Align">
<i class="fas fa-align-center"></i>
</button>
<button onclick="editoroption5()" title="Right Align">
<i class="fas fa-align-right"></i>
</button>
<button onclick="editoroption6()" title="Uppercase Text">
Upper Case
</button>
<button onclick="editoroption7()" title="Lowercase Text">
Lower Case
</button>
<button onclick="editoroption8()" title="Capitalize Text">
Capitalize
</button>
<button onclick="editoroption9()" title="Clear Text">
Clear Text
</button>
</div>
<textarea id="textarea1" rows="15" placeholder="Your text here...">
</textarea>
</section>
<script src="script.js"></script>
</body>
</html>
CSS
/* styles.css */
body {
font-family: 'Arial', sans-serif;
background-color: #f3f4f6;
color: #333;
margin: 0;
padding: 20px;
}
h1 {
text-align: center;
color: #4A4A4A;
}
.section {
padding: 20px;
border-radius: 8px;
background-color: #fff;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.button-container {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
button {
margin: 5px;
padding: 10px 15px;
border: none;
border-radius: 5px;
background-color: #4CAF50;
color: white;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
textarea {
width: 100%;
border: 1px solid #4CAF50;
border-radius: 5px;
padding: 10px;
resize: none;
font-size: 16px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
JavaScript
//script.js
function editoroption1() {
document.getElementById("textarea1")
.style.fontWeight = "bold";
}
function editoroption2() {
document.getElementById("textarea1")
.style.fontStyle = "italic";
}
function editoroptionr3() {
document.getElementById("textarea1")
.style.textAlign = "left";
}
function editoroption4() {
document.getElementById("textarea1")
.style.textAlign = "center";
}
function editoroption5() {
document.getElementById("textarea1")
.style.textAlign = "right";
}
function editoroption6() {
document.getElementById("textarea1")
.style.textTransform = "uppercase";
}
function editoroption7() {
document.getElementById("textarea1")
.style.textTransform = "lowercase";
}
function editoroption8() {
document.getElementById("textarea1")
.style.textTransform = "capitalize";
}
function editoroption9() {
document.getElementById("textarea1")
.style.fontWeight = "normal";
document.getElementById("textarea1")
.style.textAlign = "left";
document.getElementById("textarea1")
.style.fontStyle = "normal";
document.getElementById("textarea1")
.style.textTransform = "capitalize";
document.getElementById("textarea1")
.value = "";
}
Output
Similar Reads
How to create a Color Generator using HTML CSS and JavaScript ? In this article, we will develop a Color Generator application using HTML, CSS, and JavaScript.In this application, we have created the Multi Format color generator. Users can select Colour Types like RGB, HEX, and CMYK. Use the sliders to customize the color and Inout field in case of HEX and color
6 min read
How to make Live Coding Editor using HTML CSS and JavaScript ? In this article, we will implement a live coding editor using HTML, CSS, and JavaScript. This project will allow you to write and execute HTML, CSS, and JavaScript code in real-time, making it an excellent tool for learning and testing your code snippets.Final OutputPrerequisiteHTMLCSSJavaScriptAppr
3 min read
How to Create Tab Headers using CSS & JavaScript? Tab headers are a commonly used user interface element in web development. They provide a way to organize content into distinct sections, allowing users to switch between them easily. Each tab represents a different category or topic, and clicking on a tab displays the corresponding content while hi
2 min read
How To Build Notes App Using Html CSS JavaScript ? In this article, we are going to learn how to make a Notes App using HTML, CSS, and JavaScript. This project will help you improve your practical knowledge in HTML, CSS, and JavaScript. In this notes app, we can save the notes as titles and descriptions in the local storage, so the notes will stay t
4 min read
How to Create a Style Tag using JavaScript? To create a <style> tag using JavaScript, use the document.createElement() method to create the tag, which allows you to dynamically add elements to the DOM.Syntaxdocument.createElement('style')Example 1: This HTML code dynamically styles a <div> element with a green background and white
1 min read