How to Adjust Font Size Dynamically in JavaScript

How to Adjust Font Size Dynamically in JavaScript

Introduction

In this tutorial we will create a How to Adjust Font Size Dynamically in JavaScript. This tutorial purpose is to teach how you can change your font size dynamically. This will tackle all the basic function that that will allow you change your font size. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system if you want to resize any text in your website. I will give my best to provide you the easiest way of creating this program Font Adjust. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display text and slider button. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. input[type=range]{
  7. writing-mode: bt-lr; /* IE */
  8. -webkit-appearance: slider-vertical; /* WebKit */
  9. width: 50px;
  10. height: 200px;
  11. padding: 0 24px;
  12. outline: none;
  13. background:transparent;
  14. }
  15. </style>
  16. </head>
  17. <nav class="navbar navbar-default">
  18. <div class="container-fluid">
  19. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  20. </div>
  21. </nav>
  22. <div class="col-md-3"></div>
  23. <div class="col-md-6 well" style="overflow:hidden;">
  24. <h3 class="text-primary">How to Adjust Font Size Dynamically in JavaScript</h3>
  25. <hr style="border-top:1px dotted #ccc;"/>
  26. <div class="col-md-2">
  27. <input type="range" min="0" max="72" value="12" oninput="adjustFontSize(this.value);"/>
  28. <div class="form-inline">
  29. <label>Value</label>
  30. <input type="text" size="1" id="display" class="form-control" disabled="disabled" />
  31. </div>
  32. </div>
  33. <div class="col-md-8">
  34. <center><label id="result">I Love Sourcecodester</label></center>
  35. </div>
  36. </div>
  37. <script src="script.js"></script>
  38. </body>
  39. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically adjust the font size. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. document.getElementById("display").value="12px";
  2. document.getElementById("result").style.fontSize=val+"px";
  3.  
  4. function adjustFontSize(val){
  5. document.getElementById("display").value=val+"px";
  6. document.getElementById("result").style.fontSize=val+"px";
  7. }

The code above is very simple and straightforward, to resize the font size we will only need one method called adjustFontSize(). This function will adjust the font size dynamically by tweaking the slider. The trick for this code is that we just simply append the font size value for every time the slider been moved.

Output:

The How to Adjust Font Size Dynamically in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Adjust Font Size Dynamically in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment