Creating a Random Number Generator App using HTML, CSS, and Vanilla JS

In this tutorial, you can learn to create a simple Random Number Generator web application. The tutorial aims to provide the students and beginners with a reference for learning some CSS tricks and Vanilla JS techniques. Here, I will be providing some scripts that demonstrate the creation of a Random Number Generator. The complete source code zip file is also provided.

How does the Random Number Generator work?

The Random Number Generator App is a simple web application that generates random numbers. It allows the users to set the minimum and maximum number of the number to be generated. If the set minimum number is greater than the maximum number, the will show a dialog that says that "Minimum Number must be greater than Maximum Number" and when the set numbers are equal, it will show "Minimum Number must not be equal to Maximum Number". The Generated Code will be shown above the form.

Creating the Random Number Generator App

Page Interface

Here is the HTML file script known as index.html. The file contains the page layout and other page elements.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Vanilla JS Random Number Generator</title>
  6. <link rel="stylesheet" href="style.css">
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. </head>
  10. <div class="container">
  11. <h1 id="page-title">Vanilla JS Random Number Generator</h1>
  12. <hr id="title_hr">
  13. <!-- Generator Wrapper -->
  14. <div id="generator-wrapper">
  15. <form action="" id="generator-form">
  16. <div id="generated-code">0</div>
  17. <!-- Input Field -->
  18. <div class="inline-form-field">
  19. <!-- Minimum Number Field -->
  20. <div class="form-field">
  21. <label for="">Minimum Number</label>
  22. <input type="number" name="minimum" min="0" required>
  23. </div>
  24. <!-- Minimum Number Field -->
  25. <!-- Maximum Number Field -->
  26. <div class="form-field">
  27. <label for="">Maximum Number</label>
  28. <input type="number" name="maximum" min="0" required>
  29. </div>
  30. <!-- Maximum Number Field -->
  31. </div>
  32. <!-- Input Field -->
  33. <!-- Generate Code Button -->
  34. <div class="btn-field">
  35. <button id="generate-btn">Generate</button>
  36. </div>
  37. <!-- Generate Code Button -->
  38. </form>
  39. </div>
  40. <!-- Generator Wrapper -->
  41. </div>
  42. <script src="script.js"></script>
  43. </body>
  44. </html>

Stylesheet

Next, here's a CSS file script known as style.css. The file contains the stylesheet code of the page layout and form elements design.

  1. @import url('https://fonts.googleapis.com/css2?family=Dongle:wght@300;400;700&family=Roboto+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;1,100;1,200;1,300;1,400;1,500;1,600&display=swap" rel="stylesheet');
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. font-family: 'Dongle', sans-serif;
  7. font-family: 'Roboto Mono', monospace;
  8. }
  9. ::selection{
  10. color: #fff;
  11. background: #4db2ec;
  12. }
  13. body{
  14. display: flex;
  15. align-items: center;
  16. justify-content: center;
  17. min-height: 100vh;
  18. background: #4facfe;
  19. background-image: linear-gradient(to right, #4facfe 0%, #00f2fe 100%);
  20. padding: 2em 0;
  21. }
  22. #page-title{
  23. color: #fff;
  24. text-align: center;
  25. font-weight: 500;
  26. text-shadow: 0px 0px 15px #0000003a;
  27. }
  28. #title_hr{
  29. width:60px;
  30. border: 2px solid #ffffff;
  31. margin: .35em auto;
  32. }
  33. @media (min-width: 780px){
  34. #page-title{
  35. width: 780px;
  36. }
  37. }
  38.  
  39. div#generator-wrapper {
  40. width: 480px;
  41. margin: 1em auto;
  42. padding: 2em 1em;
  43. background: #fff;
  44. border: 1px solid #d1d1d1;
  45. box-shadow: 0px 0px 10px #00000038;
  46. }
  47.  
  48. .inline-form-field {
  49. position: relative;
  50. display: flex;
  51. width: 100%;
  52. flex-wrap: wrap;
  53. }
  54.  
  55. .form-field{
  56. position: relative;
  57. width: 50%;
  58. padding: 15px 20px;
  59. }
  60.  
  61. .form-field>label,
  62. .form-field>input{
  63. display: block;
  64. width: 100%;
  65. }
  66.  
  67. .form-field>label {
  68. color: #4e4e4e;
  69. margin-bottom:5px;
  70. }
  71.  
  72. .form-field>input {
  73. padding: 0.35em 0.5em;
  74. border: 1px solid #c9bdbd;
  75. outline: none;
  76. }
  77.  
  78. .form-field>input:focus {
  79. border-color: #94def596;
  80. box-shadow: 0px 0px 7px #94def547;
  81. }
  82.  
  83. .btn-field {
  84. display: flex;
  85. align-items: center;
  86. justify-content: center;
  87. }
  88. button#generate-btn {
  89. padding: 0.35em 0.75em;
  90. background: #00adff;
  91. color: #fff;
  92. font-size: 1.3rem;
  93. font-weight: 500;
  94. border: none;
  95. cursor: pointer;
  96. width: 100%;
  97. border-radius: 50px;
  98. }
  99.  
  100. button#generate-btn:hover {
  101. background: #0474a8;
  102. box-shadow: 0px 0px 10px #0474a844;
  103. }
  104.  
  105. div#generated-code {
  106. text-align: center;
  107. font-size: 4rem;
  108. letter-spacing: 2px;
  109. }

JavaScript

Lastly, here's the JavaScript file script known as script.js. The file contains the code that generates the random number between the given minimum and maximum numbers upon submission.

  1. const generateEl = document.getElementById('generated-code')
  2. const form = document.getElementById('generator-form')
  3.  
  4. form.addEventListener('submit', function(e){
  5. e.preventDefault()
  6. var min = form.querySelector('input[name="minimum"]').value
  7. var max = form.querySelector('input[name="maximum"]').value
  8. min = parseInt(min)
  9. max = parseInt(max)
  10. if(min > max){
  11. alert("Maximum Number must be greater than Minimum Number");
  12. return false;
  13. }
  14. if(min == max){
  15. alert("Minimum Number must not be equal to Maximum Number");
  16. return false;
  17. }
  18. var generatedCode = Math.floor(Math.random() * (max - min + 1)) + min;
  19. generateEl.innerText = generatedCode
  20. })

Snapshots

Here are some snapshots of the web application script results.

Page Interface

Random Number Generator App using Vanilla JS

Generator Wrapper

Random Number Generator App using Vanilla JS

Generator Form

Random Number Generator App using Vanilla JS

Generated Code

Random Number Generator App using Vanilla JS

There you go! I have provided also the complete source code zip file on this website and it is free to download. The download button is located below this tutorial's content. Feel free to download and modify the source code the way you wanted.

That's it! I hope this Creating a Random Number Generator App using HTML, CSS, and Vanilla JS Tutorial will help you with what you are looking for and that you'll find something useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment