Words counter with stylish dark UI using HTML CSS and JS

Words counter is the tool that helps the user count the words he/she writes on it. It is beneficial for blog writers/content writers. We have already created a post sharing the source code of a Rich Text Editor with a words counter. You can check it out. 

In this post, you will learn how to create a word counter tool with a stylish dark UI. Each CSS code and JavaScript code used in this project is going to be deeply explained. This tool is mainly created using JavaScript. Also, it is a simple and awesome-looking one. 

Words counter with stylish dark UI using HTML CSS and JS

Files and folder

Create a folder and create important files such as index.html, style.css and script.js on it. 

index.html

Here the main HTML layout is created. I have added style.css and script.js externally. And this is the best way to add javascript and HTML in a live website project. 


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>word counter</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="mainContainer">
        <h1>Word counter</h1>
        <div onkeyup="countChar1(this);"  class="inputBox" id="text-input" contenteditable="true"></div>
        <div class="count">
            words: <span class="words"></span> 
        </div>
    </div>
</body>
<script src="script.js"></script>
</html>

style.css

full source code


* {
    margin: 0;
    padding: o;
    box-sizing: border-box;
    outline: none;
}

body {
    background-color: rgb(11, 20, 49);
}

.mainContainer {
    max-width: 700px;
    margin: auto;

}

.mainContainer h1 {
    text-align: center;
    font-size: 50px;
    color: rgb(184, 184, 184);
    padding-bottom: 30px;
   text-decoration: underline;
   
}

.mainContainer .inputBox {
    height: 400px;
    color: rgb(216, 216, 216);
    padding: 20px;
    font-size: 18px;
    font-weight: 550;
    border:3px solid rgb(109, 69, 172);
    border-radius: 30px;
    overflow: auto;
}

.mainContainer .inputBox::-webkit-scrollbar {
    width: 0px;
}
.mainContainer .count{
    float: right;
    padding: 20px;
    margin: 10px;
    background-color: blueviolet;
    color: rgb(255, 255, 255);
    font-weight: 700;
    position: relative;
    top: -100px;
    font-size: 18px;
    border-radius: 30px;
}

script.js

The javascript code for count the words written by users. 


document.getElementById('text-input').addEventListener('input', function () {
    var text = this.textContent,
        count = text.trim().replace(/\s+/g, ' ').split(' ').length;

    document.querySelector('.words').textContent = count;
    
});


Discussions

Post a Comment