Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Decimal-Binary Converter</title>
    <!-- Design by groundttorial.com -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <style>
      * {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  background-color: #0075ff;
}
.container {
  background-color: #ffffff;
  width: 80vmin;
  min-width: 480px;
  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;
}
    </style>
  </head>
  <body>
    <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>
        <div class="input-wrapper">
          <label for="bin-inp">Binary:</label>
          <input type="number" id="bin-inp" />
        </div>
      </div>
      <p id="error-msg"></p>
    </div>
    <!-- Script -->
    <script>
      //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;
  }
});
    </script>
  </body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console