Pixel to EM Converter using HTML, CSS & JavaScript
Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Pixel – EM Converter which will generates a corresponding px value in that section. 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 Pixel – EM Converter Project.
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 Pixel – EM 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 Pixel – EM Converter Project.
Step 3
At last we will use JS (JavaScript) which will add a logic to make the Pixel – EM Converter Project responsive from the user end.
I hope you have got an idea about the project.
HTML Code for Pixel – EM Converter
First we’ll start with creating the structure of the Pixel – EM 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"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Pixel-EM Calculator</title> <!-- Google Font --> <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet" /> <!-- Stylesheet --> <link rel="stylesheet" href="style.css" /> </head> <body> <div class="container"> <h1>PX-EM Converter</h1> <div class="wrapper"> <label for="inp-base-px">Base Pixel Size:</label> <input type="number" id="inp-base-px" value="16" /> </div> <div class="wrapper"> <div class="inp-wrapper"> <label for="inp-px">PX:</label> <input type="number" id="inp-px" placeholder="px" /> </div> <div class="inp-wrapper"> <label for="inp-em">EM:</label> <input type="number" id="inp-em" placeholder="em" /> </div> </div> </div> <!-- Script --> <script src="script.js"></script> </body> </html>
Portfolio Website Using HTML ,CSS ,Bootstrap and JavaScript
CSS Code for Pixel – EM Converter
Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Pixel – EM Converter project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.
* { padding: 0; margin: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { background-color: #7fca37; } .container { background-color: #ffffff; width: 80vmin; font-size: 16px; position: absolute; transform: translate(-50%, -50%); top: 50%; left: 50%; padding: 4em 1.8em; border-radius: 0.6em; box-shadow: 0 0.7em 3em rgba(44, 71, 18, 0.4); } h1 { width: 100%; text-align: center; font-size: 2.8em; } label { display: block; font-weight: 600; font-size: 1.1em; color: #3c354e; } input { width: 100%; font-size: 1.25em; padding: 0.6em 0.55em; margin-top: 0.5em; border-radius: 0.2em; border: 1px solid #0a0125; } .container .wrapper:nth-child(2) { width: 50%; margin: 3em auto 1.25em auto; } .container .wrapper:nth-child(3) { display: flex; flex-direction: row; justify-content: space-between; gap: 0.6em; } @media screen and (max-width: 575px) { .container { font-size: 14px; } .container .wrapper:nth-child(2) { width: 100%; margin: 3.5em auto 0.6em auto; } .container .wrapper:nth-child(3) { flex-direction: column; } }
JavaScript Code for Pixel – EM Converter
Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Pixel – EM Converter using HTML, CSS & JavaScript (Source Code).
let inpBase = document.getElementById("inp-base-px"); let inpPX = document.getElementById("inp-px"); let inpEM = document.getElementById("inp-em"); //Function to convert pixels to em let pxToEm = () => { let inpBaseValue = inpBase.value; let pxValue = inpPX.value; //Checking if input field is not empty //If not empty then calculate the EM value if (pxValue.length != 0) { inpEM.value = pxValue / inpBaseValue; } //If empty then clear EM field else { inpEM.value = ""; } }; //Function to convert em to pixel let emToPx = () => { let inpBaseValue = inpBase.value; let emValue = inpEM.value; //Checking if input field is not empty //If not empty then calculate the pixel value if (emValue.length != 0) { inpPX.value = emValue * inpBaseValue; } //If empty then clear the PX field else { inpPX.value = ""; } }; //Function to calculate EM and PX when Base Font Size is changed let calcEmPx = () => { //Checking if input field is not empty //If not empty then run emToPx()/pxToEm() if (inpBase.value.length != 0) { emToPx(); } //If empty then clear PX and EM field else { inpPX.value = ""; inpEM.value = ""; } }; //Adding oninput event to each input field inpPX.oninput = pxToEm; inpEM.oninput = emToPx; inpBase.oninput = calcEmPx;
10+ Javascript Projects For Beginners With Source Code
Final Output
We have Successfully created our Pixel – EM Converter using HTML, CSS & JavaScript (Source Code). 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