Decimal-Binary Converter using HTML, CSS & JavaScript

Decimal-Binary Converter using HTML, CSS & JavaScript

Decimal-Binary Converter using HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make a Decimal-Binary Converter which will generates a converter that will convert the binary value to decimal value when the user will define some figure. This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Decimal-Binary Converter Project.

Decimal Binary Converter

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make the Decimal-Binary Converter Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Decimal-Binary Converter Project.

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Decimal-Binary Converter Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Decimal-Binary Converter

First we’ll start with creating the structure of the Decimal-Binary Converter project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Decimal-Binary Converter</title>
    <!-- Google Fonts -->
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
    <div class="container">
      <h2>Decimal Binary Converter</h2>
      <div class="wrapper">
        <div class="input-wrapper">
          <label for="dec-inp">Decimal:</label>
          <input type="number" id="dec-inp" />
        <div class="input-wrapper">
          <label for="bin-inp">Binary:</label>
          <input type="number" id="bin-inp" />
      <p id="error-msg"></p>
    <!-- Script -->
    <script src="script.js"></script>

CSS Code for Decimal-Binary Converter

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Decimal-Binary Converter project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now we have created the structure using HTML and styled the webpage using CSS its time to add the functionality using JavaScript in this project.

50+ HTML, CSS & JavaScript Projects With Source Code

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
body {
  background-color: #0075ff;
.container {
  background-color: #ffffff;
  width: 80vmin;
  max-width: 37.5em;
  padding: 3em 2.5em;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  border-radius: 0.62em;
  box-shadow: 0 1.2em 2.5em rgba(0, 21, 44, 0.18);
h2 {
  font-size: 1.8em;
  color: #0075ff;
  text-align: center;
  font-weight: 600;
  letter-spacing: 0.5px;
  margin-bottom: 1.3em;
.wrapper {
  display: flex;
  justify-content: space-between;
  gap: 1.8em;
label {
  display: block;
  margin-bottom: 0.5em;
  font-weight: 500;
  color: #050121;
input {
  position: relative;
  font-size: 1.1em;
  width: 100%;
  padding: 0.5em;
  border-radius: 0.2em;
  border: 1.5px solid #43405c;
  color: #43405c;
  outline: none;
input:focus {
  border-color: #0075ff;
#error-msg {
  text-align: center;
  margin-top: 1.25em;
  color: #ff4362;

JavaScript Code for Decimal-Binary Converter

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. In this code we have defined the decimal and binary function in the get element ID and an error when there is an error from the user side. Let us see the Final Output of the project Decimal-Binary Converter using HTML, CSS & JavaScript (Source Code).

Portfolio Website using HTML and CSS (Source Code)

//Initial References
let decInp = document.getElementById("dec-inp");
let binInp = document.getElementById("bin-inp");
let errorMsg = document.getElementById("error-msg");

//Convert decimal to binary when user inputs in the decimal field
decInp.addEventListener("input", () => {
  let decValue = parseInt(decInp.value);
  //Converts the decimal value to binary
  binInp.value = decValue.toString(2);

//Convert binary to decimal when user inputs in the binary field
binInp.addEventListener("input", () => {
  let binValue = binInp.value;
  //If the binary number is valid convert it to decimal
  if (binValidator(binValue)) {
    decInp.value = parseInt(binValue, 2);
    errorMsg.textContent = "";
  //Else display an error message
  else {
    errorMsg.textContent = "Please Enter An Valid Binary Input";

  //Function to check if the binary number is valid i.e it does not contain any number other than 0 and 1
  function binValidator(num) {
    for (let i = 0; i < num.length; i++) {
      if (num[i] != "0" && num[i] != "1") {
        return false;
    return true;


Live Preview of Decimal-Binary Converter using HTML, CSS & JavaScript

See the Pen
by Harsh Sawant (@harshh9)
on CodePen.

We have Successfully created our Decimal-Binary Converter using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9

Leave a Reply