How can I change the font family and font size with jQuery?

To change the font family and font size with jQuery, use the jQuery css() method. The CSS properties font-family and font-size are used to modify the typography of elements dynamically.

Basic Syntax

The css()

$(selector).css({"font-family": "fontName", "font-size": "size"});

Example

You can try to run the following code to learn how to change font family and font size with jQuery ?

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("p").on({
                mouseenter: function() {
                    $(this).css({
                        "font-family": "Arial, Helvetica, sans-serif", 
                        "font-size": "200%"
                    });
                },
                mouseleave: function() {
                    $(this).css({
                        "font-family": "Times, serif", 
                        "font-size": "100%"
                    });
                }
            });
        });
    </script>
</head>
<body>
    <p>Move the mouse pointer on the text to change the font family and size.</p>
    <p>This is another paragraph to test the font changes.</p>
</body>
</html>

Alternative Method

You can also change font properties individually using separate css() calls ?

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#changeFont").click(function() {
                $("h2").css("font-family", "Georgia, serif");
                $("h2").css("font-size", "24px");
            });
        });
    </script>
</head>
<body>
    <h2>Sample Heading Text</h2>
    <button id="changeFont">Change Font</button>
</body>
</html>

Conclusion

Using jQuery's css() method makes it simple to dynamically change font family and font size properties, providing interactive typography effects for web elements.

Updated on: 2026-03-13T19:08:26+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements