• 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 / jQuery / How to Use jQuery to Animate the Font Size of a Paragraph

How to Use jQuery to Animate the Font Size of a Paragraph

March 15, 2022 Leave a Comment

To use jQuery to animate the font size of a paragraph, we can use the jQuery animate() method to change the font-size property of that element.

$("#p1").animate({fontSize: "30px"});

Let’s say we have the following HTML:

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

If we want to animate the font size of the paragraph from its default size of 18px to 30px, we will use the jQuery animate() method in the following JavaScript code.

$("#div1 p").animate({fontSize: "30px"});

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1 p").animate({fontSize: "30px"});

Using jQuery to Animate the Font Size of Text with a Click

To animate the font size of text using jQuery, we can combine the animate() method with a click event.

Let’s say we have the following HTML and we want to animate the font size of the first paragraph to 30px. We will provide the user a button to animate the text to 30px and a button to animate it back to 18px

<div>
  <p id="p1">This is some text</p>
  <p id="p2">This is some text</p>
  <div id="click-me1" class="click-me">Animate font size to 30px</div>
  <div id="click-me2" class="click-me">Animate font size back</div>
</div>

We can utilize both the jQuery click() and animate() methods to animate the font size of the first line of text and make it larger and smaller.

Here is the JavaScript code:

$("#click-me1").click(function(){
  $("#p1").animate({fontSize: "30px"});
});
$("#click-me2").click(function(){
  $("#p1").animate({fontSize: "18px"});
});

The final code and output for this example of how to animate the font size of text using jQuery is below:

Code Output:

This is some text

This is some text

Animate font size to 30px
Animate font size back

Full Code:

<div>
  <p id="p1">This is some text</p>
  <p id="p2">This is some text</p>
  <div id="click-me1" class="click-me">Animate font size to 30px</div>
  <div id="click-me2" class="click-me">Animate font size back</div>
</div>

<script>

$("#click-me1").click(function(){
  $("#p1").animate({fontSize: "30px"});
});
$("#click-me2").click(function(){
  $("#p1").animate({fontSize: "18px"});
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery to animate the font size of a paragraph.

Other Articles You'll Also Like:

  • 1.  How to Use jQuery to Change Span Text
  • 2.  How to Uncheck All Checkboxes in jQuery
  • 3.  Using jQuery to Remove Background Color
  • 4.  jQuery focusin – Changing the Background Color with the focusin() Method
  • 5.  jQuery fadeOut – Fading Out Element in HTML
  • 6.  jQuery delay – How to Delay the Start of a Method
  • 7.  jQuery focusout – How to Use the focusout() Method on an HTML Form
  • 8.  Using jQuery to Set the Width of a Div
  • 9.  jQuery blur – How to Use the blur() Method on an HTML Form
  • 10.  Examples of Text Animations Using jQuery

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