• 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
  • Ruby
  • 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.  Using JavaScript to Create a Unique Array
  • 2.  Get the Sum of Array in JavaScript
  • 3.  How to Convert Radians to Degrees Using JavaScript
  • 4.  Using JavaScript to Remove Character From String
  • 5.  Using JavaScript to Resize an Image
  • 6.  JavaScript Array includes Method
  • 7.  How to Use JavaScript to Change Button Text
  • 8.  JavaScript Map Size – Find Length of JavaScript Map
  • 9.  Set Hidden Field Value In JavaScript
  • 10.  Using JavaScript to Check if a Number is Divisible by 3

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy