• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / JavaScript / Using JavaScript to Change the Font Size of a Paragraph

Using JavaScript to Change the Font Size of a Paragraph

January 29, 2022 Leave a Comment

To change the font size of a paragraph using JavaScript, we simply use the style fontSize property.

document.getElementById("p1").style.fontSize = "12px";

Let’s say we have the following HTML:

<div id="div1">
  <p id="p1">We will change the font size of this text.</p>
</div>

If we want to change the font size of the paragraph to “12px”, we will use the fontSize property in the following JavaScript code.

document.getElementById("p1").style.fontSize = "12px";

If we wanted to underline the paragraph, we could do that easily by targeting the textDecoration property.

We can change many styles using JavaScript by targeting the style properties of an element.

Using JavaScript to Change the Font Size of Text with a Click

To change the font size of text using JavaScript, we can target the style fontSize property with a click event.

Let’s say we have the following HTML and we want to change the font size of the first paragraph to make it smaller.

<div>
  <p id="p1">The Programming Expert</p>
  <p id="p2">Code Snippets to Help You Solve Your Programming Headaches Fast</p>
  <p id="p3">Find Solutions for Your Programs in Python, PHP, HTML, SAS, JavaScript and more.</p>
  <div id="click-me" onclick="clickFunction()">Change font size</div>
</div>

We can utilize both the Style fontSize property of #p1 along with an onclick event to change the font size of the first line of text.

Here is the JavaScript code:

function clickFunction() { 
  document.getElementById("p1").style.fontSize = "32px";
}

The final code and output for this example of how to change the font size of a paragraph using JavaScript is below:

Code Output:

The Programming Expert

Code Snippets to Help You Solve Your Programming Headaches Fast

Find Solutions for Your Programs in Python, PHP, HTML, SAS, JavaScript and more.

Change font size

Full Code:

<div>
  <p id="p1">The Programming Expert</p>
  <p id="p2">Code Snippets to Help You Solve Your Programming Headaches Fast</p>
  <p id="p3">Find Solutions for Your Programs in Python, PHP, HTML, SAS, JavaScript and more.</p>
  <div id="click-me" onclick="clickFunction()">Change font size</div>
</div>

<script>

function clickFunction() {
  document.getElementById("p1").style.fontSize = "32px";
}

</script>

Hopefully this article has been useful for you to understand how to use JavaScript to change the font size of some text.

Other Articles You'll Also Like:

  • 1.  Finding the Length of a String in JavaScript
  • 2.  JavaScript onkeyup – How to Use onkeyup Event with JavaScript
  • 3.  Using the appendChild Method with JavaScript to Add Items to a List
  • 4.  Using JavaScript to Add Item to Array if it Does Not Exist
  • 5.  Using JavaScript to Hide Element by Class
  • 6.  How to Create a JavaScript Count Up Timer
  • 7.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 8.  How to Check if a Number is Prime in JavaScript
  • 9.  Remove Empty Strings from an Array in JavaScript
  • 10.  Using JavaScript to Remove Leading Zeros

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy