Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow users to change font size in a webpage

Tags:

html

jquery

css

I would like to be able to change the size of text in a web page, I guess jQuery can be used, but I have no experience in javascript.

Another problem is that the webpage is a phtml file, and multiple php echo commands are used. And the page is made up of multiple phtml files.

EDIT: I would like to give users 3 choices for different font sizes.

like image 977
Tian Bo Avatar asked May 21 '09 08:05

Tian Bo


People also ask

How do I allow visitors to change the font-size in WordPress?

Adding font-resizer to a WordPress SidebarClick on Appearance and select the Widgets option. Locate the Font Resizer widget and drag it onto your Sidebar widget area. You are now done, there are no settings to change on the widget. You can now see it on your website.

How do you control font-size in HTML?

To change font size in HTML, use the CSS font-size property. Set it to the value you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a paragraph, heading, button, or span tag.

Which property allows you to control the size of the font?

The font-size-adjust property gives you better control of the font size when the first selected font is not available. When a font is not available, the browser uses the second specified font. This could result in a big change for the font size. To prevent this, use the font-size-adjust property.


1 Answers

The approach I would take is to apply a font-size percentage to the body tag of the html

body
{
    font-size: 62.5%;
}

Then for all other elements specify font-size in ems, which generates the size as a scaled up percentage of the inherited font-size

h1
{
    font-size: 1.8em;
}

p
{
    font-size: 1.2em;
}

I use 62.5% on the body because it is easy to translate this to pixel sizes when specifying other font sizes e.g. 1.2em = 12px, 1.8em = 18px and so on

To then change the font size in the page programatically you just need to change the value of the font-size on the body and the rest of the page will scale up or down accordingly

You can test this by having three divs

<div id="sizeUp">bigger text</div>
<div id="normal">normal text</div>
<div id="sizeDown">smaller text</div>

In JQuery I believe you can do

$(document).ready(function() {

    $("#sizeUp").click(function() {

        $("body").css("font-size","70%");

    });

    $("#normal").click(function() {

        $("body").css("font-size","62.5%");

    })

    $("#sizeDown").click(function() {

        $("body").css("font-size","55%");

    })
});
like image 50
Nick Allen Avatar answered Sep 19 '22 19:09

Nick Allen