• 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 / Using jQuery to Change the Font Size of a Paragraph

Using jQuery to Change the Font Size of a Paragraph

January 29, 2022 Leave a Comment

To change the font size of a paragraph using jQuery, we simply can use the css() method to change the styles of an element.

$("#p1").css("font-size", "12px");

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 change the font size of the paragraph to “12px”, we will use the jQuery css() method in the following JavaScript code.

$("#div1 p").css("font-size", "12px");

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

jQuery("#div1 p").css("font-size", "12px");

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

To change the font size of text using jQuery, we can combine the css() method 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 12px.

<div>
  <p id="p1">This is some text</p>
  <p id="p2">This is some text</p>
  <div id="click-me">Change font size</div>
</div>

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

Here is the JavaScript code:

$("#click-me").click(function(){
    $("#p1").css("font-size", "12px");
});

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

Code Output:

This is some text

This is some text

Change font size

Full Code:

<div>
  <p id="p1">This is some text</p>
  <p id="p2">This is some text</p>
  <div id="click-me">Change font size</div>
</div>

<script>

$("#click-me").click(function(){
    $("#p1").css("font-size", "12px");
});

</script>

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

Other Articles You'll Also Like:

  • 1.  jQuery mouseover – An Interactive Example of the mouseover Method
  • 2.  Using jQuery to Set the Id of a Div
  • 3.  Using jQuery to Get Text Length
  • 4.  Using jQuery to Add a Sibling to an Element
  • 5.  jQuery Random Number Generator Between Two Numbers
  • 6.  Using jQuery to see if URL Contains Specific Text
  • 7.  Using jQuery to Add Class to HTML Element
  • 8.  Using jQuery prepend to Insert Element As First Child
  • 9.  Using jQuery to Append Text to textarea Field
  • 10.  jQuery slideDown – How to Slide Down and Show an Element 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