

How we can resize or change the font size on the webpage by clicking? Solution: See this jQuery Font Resize Program With Modify CSS Values, Change Font Size.
Previously I have shared a font preview program using google font API, but this is about resizing font using the JS program. Basically, font-resize programs you will see in some article website, to increase and decrease sizes of fonts. There has dedicated navigation buttons to change font size according to the user’s choice. Sometimes we feel irritated when reading small or too big fonts, that’s why this feature is important.
Today you will learn to create the Change Font Size Program. Basically, there is a text block with a heading, a subheading, and some textsand on the right side of the box, there are 4 buttons with different sizes of “A” character. These buttons are for changing the font-size and the big and small “A” is for indicating the sizes. The buttons help the user to identify and change the font’s size.
So, Today I am sharing jQuery Font Resize Program With Modify CSS Values. There I have used jQuery for creating the program, but we playing with CSS values. As we know jQuery is a JS library, that’s why I am putting this in the JavaScript category. This is a good example for those people who are a beginner in Web Development. Also, You can use this program on your website.
If you are thinking now how this font resizes program actually is, then see the preview given below.
Preview Of Change Font’s Size Program
See this video preview to getting an idea of how this program looks like.
Live DemoNow you can see this visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
jQuery Font Resize Program With Modify CSS Values Source Code
Before sharing source code, get the source code of it. First, I have created a main div named container, put another div section for controls, heading, subheading, and texts inside it. Inside the control div section, I have put 4 hyperlinks for 4 different sizes. Also in the HTML file, I have linked other files like CSS, JS, and jQuery CDN.
Now using CSS I have placed all the elements in the right place, as you can see in the preview. With CSS, I have done basic works like size, position, margin, padding, etc to all the elements. In the controls button, I have put “A” and gave different font-sizes for indicating the works of the buttons. Which button has the small font, that button will reduce the font size after clicking and the bigger one will increase font-size.
jQuery handling here the main feature of the program, which is change font’s size. Basically, jQuery changing here the CSS values on the action, nothing else. I have jQuery’s $("#IDname").click command and gave values separately for each buttons. Left all other things you will understand after getting the codes, I can’t explain all in writing.
For creating the program you have to create 3 files. First file for HTML, second file for CSS, and the third file for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title>Resize Fonts jQuery | Webdevtrick.com</title> <link rel="stylesheet"href="style.css">
</head> <body>
<div id="container"> <div id="controls"> <a href="#"id="small">A</a> <a href="#"id="medium"class="selected">A</a> <a href="#"id="large">A</a> <a href="#"id="max">A</a> </div> <h1>Heading</h1> <h2>Subheading</h2> <p>Lorem ipsum dolor sit amet, pri ne periculis definiebas, habeo gloriatur has id. Ius ad ubique animal, eum recteque electram explicari no, sed in nostrum adipiscing. Lorem ipsum dolor sit amet, pri ne periculis definiebas, habeo gloriatur has id. </p> </div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="function.js"></script>
</body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put the codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | * { margin:0;padding:0; font-family:Arial,Helvetica,sans-serif;text-decoration:none;}
body { background:#e7e7e7;}
#container { width:100%; max-width:400px; margin:0auto; margin-top:15%; padding:40px; background:#fff; box-shadow:0050px0rgba(0,0,0,.25);}
#controls { float:right; padding:2px; width:25px; background:#333; position:fixed; margin:000440px; text-align:center; transition:.25sease-out;}
#controls a { font-size:24px; color:#aaa; display:block; font-weight:bold; padding:5px;}
#controls a:hover { color:#fff; background:#000; transition:.25sease-out;}
a.selected { background:#000; color:#fff!important;}
#small { font-size:10px!important;} #medium { font-size:14px!important;} #large { font-size:18px !important;} #max { font-size:24px !important;}
.small { font-size:75%;}
h1 { font-size:36px; font-weight:bold;}
h2 { font-size:24px; margin:10px0;} p { font-size:14px; line-height:20px;}
@media (max-width: 750px) { #container { width:225px; } #controls { margin:000265px; } } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | $(document).ready(function(){
$("#small").click(function(event){ event.preventDefault(); $("h1").animate({"font-size":"24px"}); $("h2").animate({"font-size":"16px"}); $("p").animate({"font-size":"12px","line-height":"16px"});
});
$("#medium").click(function(event){ event.preventDefault(); $("h1").animate({"font-size":"36px"}); $("h2").animate({"font-size":"24px"}); $("p").animate({"font-size":"14px","line-height":"20px"});
});
$("#large").click(function(event){ event.preventDefault(); $("h1").animate({"font-size":"48px"}); $("h2").animate({"font-size":"30px"}); $("p").animate({"font-size":"16px","line-height":"20px"});
});
$("#max").click(function(event){ event.preventDefault(); $("h1").animate({"font-size":"56px"}); $("h2").animate({"font-size":"42px"}); $("p").animate({"font-size":"24px","line-height":"26px"});
});
$("a").click(function(){ $("a").removeClass("selected"); $(this).addClass("selected");
});
}); |
That’s It. Now you have successfully created jQuery Font Resize Program With Modify CSS Values, Change Font Size. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.
Style fontSize Property
❮ Style Object
Example
Set the font size of an element to "x-large":
document.getElementById("demo").style.fontSize = "x-large";
Try it Yourself »Definition and Usage
The fontSize property sets or returns the font size of the text.
Browser Support
Property | |||||
---|---|---|---|---|---|
fontSize | Yes | Yes | Yes | Yes | Yes |
Syntax
Return the fontSize property:
object.style.fontSize
Set the fontSize property:
object.style.fontSize = "value|initial|inherit"
Property Values
Value | Description |
---|---|
xx-small x-small small medium large x-large xx-large | Sets the size of the font to different fixed sizes, from xx-small to xx-large |
smaller | Decreases the font-size by one relative unit |
larger | Increases the font-size by one relative unit |
length | Defines the font-size in length units |
% | Sets the font-size to a % of the parent element's font size |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Technical Details
Default Value: | medium |
---|---|
Return Value: | A String, representing the font size of the text in the element |
CSS Version | CSS1 |
More Examples
Example
A demonstration of possible values:
var listValue = selectTag.options[selectTag.selectedIndex].text;
document.getElementById("demo").style.fontSize = listValue;
Example
Return the font size of an element:
alert(document.getElementById("demo").style.fontSize);
Try it Yourself »Related Pages
CSS tutorial: CSS Font
CSS reference: font-size property
HTML DOM reference: font property
❮ Style Object
How to Use jQuery to Let Visitors Resize Your Site’s Font
You can use jQuery to give your website visitors the ability to resize your site’s font. This is a useful interface to have, since your visitors can enlarge or reduce the size of the font at their leisure, giving them the best reading experience. If you are not familiar with jQuery, check out our jQuery Basics article for an explanation of how jQuery works.
This is an interactive code display that shows you the HTML, CSS, jQuery, and a demo of the output.
Setting Up HTML for Resizing
The first thing you have to do is set up your HTML so the jQuery can determine what to resize. If you are unfamiliar with equipping your HTML document to reference jQuery, see our Getting Your HTML Ready for jQuery article.
First, you need to add a resizing interface. Don’t worry, this sounds more complicated than it actually is. This is just something your website users can interact with to resize your webpage’s font. As you can see in the example above, we used links that were represented as plus, minus, and the word, “reset.”
<a class="increase">+</a> <a class="decrease">-</a> <a class="reset">reset</a>Each link should have a class that represents the action, like “increase” or “decrease.” When we add the jQuery, we’re going to use these classes to indicate which link should increase, decrease, or reset the font size.
Now, assess what elements or sections you want to be resizable. If it’s a section, you may want to create a <div> with a class so that you can specify a unique area to be resized.
See the Pen jQuery: Resizing Webpage Font Class Example by Pair Networks (@pairdocs) on CodePen.
In the example above, you can see that the <div> with the class=”resizable” is marked to be resized. However, the rest of your paragraphs will be unaffected, unless you specify that all <p> elements should also be resized.
jQuery for Resizing
jQuery handles the heavy lifting of resizing. If you are unfamiliar with jQuery, check out our jQuery Basics article to get a grasp of the common concepts.
The first step is to create a $(document).ready function that will encase all your jQuery statements.
//jQuery statements//jQuery statements});Next, we’re going to add the first jQuery statement to go within this function.
var resize = new Array('element','.class'); resize = resize.join(',');This statement establishes which elements or classes should be included in the resizing. These elements/classes are stored in the variable “resize.” If you look at the resizing font example, you will see that the <p> element and the “resizable” class were chosen for resizing.
After establishing which elements or classes will be affected, you need to set up the code to actually change the font size when a visitor interacts with your resizing interface.
Reset the Font Size
Under our “var resize” statement, we’re going to add a statement that will reset the font to its original size.
var resetFont = $(resize).css('font-size'); $(".reset $(resize).css('font-size', resetFont); });This statement calls the elements or classes you stored in the “resize” variable and, when the link with the “reset” class is clicked, the font-size attribute in CSS will return to its original size.
The $(".reset").click(function() calls the “reset” class from the HTML. This enables the jQuery to reset the font size when the user interacts with the text associated with the “reset” class. In our example, this text is simply the “reset” link.
Increase the Font Size
Next, we need to add a statement that will increase the font size. In our example, we added the “increase” class to the “+” link, so we need to tie the jQuery action of increasing the size to the “+”.
This example does just that.
//specifies that this will affect the “increase” class $(".increase//creates a variable that holds the current font sizevar originalFontSize = $(resize).css('font-size');//creates a variable that parses the numbervar originalFontNumber = parseFloat(originalFontSize, 10);//creates a variable that holds the increased font sizevar newFontSize = originalFontNumber*1.2;//changes the font-size in the CSS document $(resize).css('font-size', newFontSize); return false; });The example starts off by establishing that this statement is dependent on a user clicking the text associated with the “increase” class. This is a “+” in our example. The next three lines add new variables: originalFontSize, originalFontNumber, and newFontSize.
originalFontSize collects the current font size from the CSS and stores it. originaFontNumber parses this number so that jQuery can read it. Then, newFontSize takes the number stored in originalFontNumber and multiplies it by 1.2.
Then, the last line goes into the CSS and changes the font size to match newFontSize.
This sequence will execute every time the “+” is clicked.
Decrease the Font Size
The decrease statement is very similar to the previous statement. However, instead of multiplying the font size by a number that will increase the original font number, the decrease statement multiplies the original font number by a decimal, which reduces it.
//specifies that this will affect the “decrease” class//creates a variable that holds the current font size var originalFontSize = $(resize).css('font-size');//creates a variable that parses the number var originalFontNumber = parseFloat(originalFontSize, 10);//creates a variable that holds the decreased font size var newFontSize = originalFontNumber*0.8;//changes the font-size in the CSS document $(resize).css('font-size', newFontSize); return false; });});This sequence will execute every time the “-” is clicked. This will reduce font size with each click.
jQuery is an open-source software permissive under an MIT license. jQuery is not a product of Pair Networks, Inc., and Pair Networks provides no warranty for jQuery. Please note that there are many levels of javascript libraries available. Please consult with your IT professional for advice and guidance on an appropriate library. This specific product may or may not meet your needs. Pair Networks, Inc. is providing support for this tutorial for your convenience and is not responsible for jQuery's performance. Please read carefully the terms and scope of services for any online service or product you are considering purchasing or using.
Was this article helpful?
Related Articles
Sours: https://www.pair.com/support/kb/resize-sites-font-jquery/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 property font-family and font-size is used. You can try to run the following code to learn how to change font family and font size with jQuery −
Example
Live Demo
<!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%"}); } }); }); </script> </head> <body> <p>Move the mouse pointer on the text to change the font family and size.</p> </body> </html>
Amit D
Published on 20-Dec-2017 10:16:36
Size jquery font
How to change font size using jQuery ?
In this article, we will see how to change the font size of an element using jQuery. To change the font size of an element, we will use css() method.
The css() method is used to change the style property of the selected element.
Syntax:
$(selector).css(property)Return value: It will return the value of the property for the selected element.
In the below example, we have created a div element that contains some text and also created a button element. When the user clicks on the button, the css() method is called and this method sets the font-size property value to 32px.
Example:
HTML
Output:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.
Anyone else in my place would only rejoice: What's wrong with being a male. Yes, class. For some reason, I always wanted something else.
You will also be interested:
- Ion vac cordless vacuum parts
- Roasted peanut calories
- Houzz dining room
- Blue tire lettering
- Mini soda dispenser
- Goodbye letter to narcissistic mother
- Walking dead 150 review
- Local female singers
Well. the next stop in a day. there is time to think. If anything - I will go down myself.