Click to enlarge font size across the entire project

I'm working on a project for elderly users and my client wants a feature where they can easily adjust the font size throughout the application with just one click of a button.

What is the best approach for implementing this functionality? My initial idea was to provide a toggle button on each page and use jQuery to adjust the font size, like so:

...
$('body').css('font-size', '20px');
...

However, I am concerned that this method may not be the most efficient, especially since different elements on the pages have varying font sizes. This would also require users to click the button on every page instead of having a universal setting.

Answer №1

To make font sizing easier, you can use relative font sizes for all elements that are descendants of the body - essentially all contextual elements.

body{
    font-size: 20px; /*This is our default*/
}

h1{
   font-size: 1em; /*Relative to the body, this will also be 20px*/
}

h2{
   font-size:0.8em; /*This will be 20 * 0.8 = 16px;*/
}

h3{
   font-size:80%; /*This translates to 16px calculated from 20 * 80% */
}

h1.huge{
    font-size:1.2em; /*Results in 24px based on 20 * 1.2 */
}

Now every element has a font size relative to the body. To adjust all font sizes with a click, simply modify the body font size:

var button = document.getElementById('.myButton');
button.addEventListener('click', function(){
    var body = document.querySelector('body');
    body.style.fontSize = '25px';
});

(You could also save this setting using localStorage or cookies to remember across pages)

After adjusting, the sizes will be as follows:

h1{
   font-size: 1em; /*25px*/
}

h2{
   font-size:0.8em; /*This results in 20px by calculating 25 * 0.8 */
}

h3{
   font-size:80%; /*Derived from 24 * 80% = 20px; */
}

h1.huge{
    font-size:1.2em; /*Calculates to 30px based on 25 * 1.2 */
}

Note: Relative font sizes are determined based on the parent element, not necessarily the body. So, if a parent element has a relative font size, additional calculations may be needed:

body{
    font-size:25px;
}

div.container{
    font-size: 0.8em; /* 20px when nested under body */
}

div.container > p{
    font-size:0.8em; /* Results in 16px when calculated from 25px * 0.8 * 0.8 */
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using jQuery to Interpret JSON Data from Google Finance

I am struggling to properly parse this JSON data and it's returning blank. I'm not sure if I'm doing it correctly. Could you take a look at it for me, please? $.getJSON('http://www.google.com/finance/info?infotype=infoquoteall&q=S ...

What is the best way to make the child Div float to the bottom of the parent Div?

Screenshot I attempted various CSS techniques in an effort to make the .booknetic_appointment_container_footer float to the bottom of #booknetic_theme_5, but unfortunately, I was unsuccessful. The closest solution I found involved hardcoding padding, but ...

Can a webpage be redirected to another page after an animation using only HTML and CSS?

My goal is to design a landing page that has a button allowing users to trigger an animation before being redirected to another page. This concept involves adding a captivating element before transitioning between pages, similar to a link with a dynamic ...

Double clicking on a row value in a Bootstrap table

Struggling to retrieve the row value after a double click in a bootstrap table. Unfortunately, every attempt returns 'undefined'. Snippet of my code: $('#table').on('dbl-click-row.bs.table', function(field, value, row, $el) ...

iPhone users may encounter a freezing issue with the footer when scrolling

Can someone assist in keeping the footer at the bottom right of the screen on my website? When I view it on my iPhone, the footer remains fixed when scrolling down. Here is how it looks: . Your help would be greatly appreciated. Thank you and have a wonder ...

Looking to show an image when a checkbox is ticked and hide it when it is unticked in HTML?

How do I make a specific image appear when a user clicks on a checkbox? I'm unsure if I should use jQuery, Ajax, or another method. What is the best approach for this task? Your assistance would be greatly appreciated. I have successfully created the ...

I'm puzzled as to why $document.referrer in AngularJS is returning blank, whereas using document.referrer directly gives me a value

let source = $document.referrer; Looking for the value of $document.referrer and storing it in a variable. ...

The fixed left div and scrollable right div are experiencing issues with their alignment

I am having an issue with a section where the left div is fixed and the right div is scrollable. However, the content of the right div scrolls even before the scroll top reaches the section. My goal is for the right div to scroll only when it reaches the t ...

How can I properly choose distinct values for an individual attribute from a JavaScript array containing objects?

Let's imagine a scenario with an array of JavaScript objects like this: var data = [ {category : "root", type: "qqqqq", value1: "aaaaa", value2: "zzzzz"}, {category : "root", type: "qqqqq", value1: "aaaaa", value2: "xxxxx"}, {category : " ...

How can I ensure that the image fits perfectly within the box using HTML and CSS

I'm having trouble displaying an image perfectly within my div. The image doesn't cover the entire box. Here is my code: .card-background{ background-image: url("../../../assets/imgs/back-blank.png"); border-radius: 10px; margin-top: 2%; ...

Incorporating outside content into HTML

Currently, I am working on a project at my job which requires me to create a comprehensive help file with multiple chapters and extensive text all within one large HTML file. However, as the text will need to be translated into different languages, I am lo ...

JavaScript array remains unchanged

I have a sample of JSON data saved in the "data" variable. Here is the FORMAT : { "0" : {"names":"Pooja, Trivedi"}, "1" : {"names":"Pooja, Rooster"} } My objective is to create a map that can calculate the frequency of each name: Pooja = 2 Triv ...

Exploring the functionality of the onblur HTML attribute in conjunction with JavaScript's ability to trigger a

How do the HTML attribute onblur and jQuery event .trigger("blur") interact? Will both events be executed, with JavaScript this.trigger("blur") triggering first before the HTML attribute onblur, or will only one event fire? I am using ...

Fluid percentage-based layout

I have four separate Divs that I would like to position on my page in a specific layout. Here is an image showing how I want them positioned: https://i.sstatic.net/KBZ1U.png In the future, I plan to add multiple svg files to Div 4 and I want it to be scro ...

How to automatically refresh a page in AngularJS after a POST request

After making a POST request, I attempted to use $state.reload in my controller to refresh the page. However, it is not updating the data on my page after registering the form. I have to manually refresh the page to see the correct data. .controller(' ...

What steps can be taken to address the build problem with Angular version 13?

Encountering a problem while working with Angular 13: https://i.sstatic.net/CbAUhh6r.png Attempting to build using ng build --configuration=test, however facing errors as mentioned above. Interestingly, if I remove the reference to bootstrap.min.css in t ...

Swap out the content in a text input box with the text chosen from a suggested autocomplete option

I am working on a text input box with auto-complete options displayed below it. I want to enable users to navigate through the options using keyboard arrows and "select" one, causing it to change color. How can I update the text in the input box with the s ...

The phenomenon of an invisible Absolute or relative position leading to grid components overlapping in Next.js

I've been struggling with this issue for over 48 hours now. I've attempted to troubleshoot by commenting out everything except the affected components and even swapping entire components around, but the problem persists. Oddly enough, rearranging ...

Looking to dynamically add content to a webpage without needing to refresh the page? Utilizing AJAX can help achieve that

On my website, users have the ability to create posts which are then saved in a database. I would like these posts to be retrieved with a query and displayed on the webpage without having to refresh the page. I understand that I need to implement Ajax for ...

JQuery AJAX click event not triggering

I'm currently working on a project to create a dynamic website where users can update text fields directly from the site. However, I've encountered an issue on the 'admin' page where nothing happens when the button is pressed to set the ...