Adjust the sizing of all fonts following the switch in font styles

Seeking help for adjusting font-size across a responsive web page design using different font families.

font-family:Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif;

To achieve responsiveness, various media queries were applied to adjust the font size:

#menu li{
    font-size:12px;
}
@media only screen and (min-width: 768px) {
#menu li{
    font-size:14px;
}
}
@media only screen and (min-width: 1000px) {
#menu li{
    font-size:16px;
}
}
@media only screen and (min-width: 1300px) {
#menu li{
    font-size:17px;
}
}

The challenge arises when changing the font family as it requires updating all classes within the media queries. The font family is now changed to:

font-family: 'Merriweather', serif;

with the font source link from Google Fonts provided.

<link href='https://fonts.googleapis.com/css?family=Merriweather:300' rel='stylesheet' type='text/css'>

The new font is lighter than the previous one, resulting in larger font sizes. A jQuery solution is proposed to scale down the font by 0.9:

$(document).ready(function(){
    $('*').each(function({
       $(this).css('fontSize',$(this).css('fontSize').split('px')[0]*0.9);
    })
})

However, this approach disrupts responsiveness on resize events. Is there a pure CSS solution available? Your assistance is greatly appreciated.

Answer №1

To establish a comprehensive solution, consider setting the foundational font size in a style rule for html and/or body. Then, utilize em, rem, or percentage values in all other rules: em remains relative to its parent element, while rem is always tied to the root size (i.e., html).

For instance, create distinct rules within media queries for html; such as html { font-size: 15px; } (adjusting the value accordingly). By utilizing em, rem, or percentages for other font sizes, each one will adjust based on the established rule.

Answer №2

To avoid having to change all elements with jQuery, one solution is to utilize relative font sizes such as em. An even more effective option is the use of rem, although it may not be as widely supported. This allows for easy adjustments to font sizes across the entire document:

#menu li {
    font-size: 0.688em;
}

@media only screen and (min-width: 768px) {
    #menu li {
        font-size: 0.75em;
    }
}
@media only screen and (min-width: 1000px) {
    #menu li {
        font-size: 0.875em;
    }
}
@media only screen and (min-width: 1300px) {
    #menu li {
        font-size: 0.938em;
    }
}
document.body.style.fontSize = "90%";

Check out this MDN article on length units for more information.

Alternatively, you can create a separate stylesheet for larger fonts, which can be easily inserted or removed when changing the font family:

#menu li {
    font-size:11px;
}

@media only screen and (min-width: 768px) {
    #menu li {
        font-size:12px;
    }
}
@media only screen and (min-width: 1000px) {
    #menu li {
        font-size:14px;
    }
}
@media only screen and (min-width: 1300px) {
    #menu li {
        font-size:15px;
    }
}

If this process seems too complex, consider using SASS or LESS to make font sizes more customizable.

And remember, in my opinion, it's never advisable to use extremely small font sizes!

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

Transfer the parameter from ajax function to the aspx.cs file

I've written some code using HTML, JS, and AJAX, but it's not functioning as expected. <script type="text/javascript"> function DeleteSelectedRecord(id) { alert(id); $.ajax({ type: "POST", ...

Restricting the number of lines within a paragraph in Angular 2

Is there a method to limit the number of lines in a <p> tag and add an ellipsis (...) at the end? Using character count for truncation doesn't work well as the width of the element varies according to device screen size. ...

Determining the size of an image prior to uploading it in a React

The solution for this issue can be found using vanilla JavaScript here To clarify, instead of using alert(), my goal is to store the dimensions in the state object. How can I adapt this code into a React component utilizing the OnChange() event handler? ...

The library 'material-ui' does not have a 'withStyles' export available

Here is the content of my package.json file. Everything was working fine until recently when I started encountering this error. "@material-ui/icons": "1.0.0-beta.42", "chartist": "0.10.1", "classnames": "2.2.5", "material-ui": "1.0.0-beta.41", "npm-ru ...

Using slashes in the import statement versus using require statement

Note: I am currently running on node version 14.1 Here is the line of code I'm experimenting with: import "module-alias/register"; This results in the following error: Error [ERR_MODULE_NOT_FOUND]: Cannot find module The error seems to point to t ...

Encountering a Console warning while working with the Material UI menu. Seeking advice on how to resolve this issue as I am integrating HTML within a text

Caution: PropType validation failed. The prop text provided to LinkMenuItem is invalid as it should be a string, not an object. Please review the render method of Menu. Below is the code snippet: var menuItems = [ // text is not valid text { route: &ap ...

Using HTML and CSS to create a flexbox layout with multiple rows

.ent__food__image { display: flex; justify-content: end; } .bev__food__image { display: flex; justify-content: end; } .kids__food__image { display: flex; justify-content: end; } <main class="flex-container"> <!-- Entrees --> & ...

Closing the dropdown menu by directly clicking on the button

I've noticed several inquiries on this platform regarding how to close a drop-down menu by clicking anywhere outside of it. However, my question is a bit different. I want the dropdown-menu to remain open once clicked, only closing when the user clic ...

Implementing multiple filters with jQuery

Make a Selection `<select class="form-control" id="technology"> <option name="sort" value="2g" id="2g"gt;2G</option> <option name="sort" value="3g" id="3g"&g ...

How can I remove a quadratic curved arrow tool in Fabric JS canvas after it has been created?

I'm currently working on developing a tool for creating quadratic curved arrows. For reference, I utilized a demo from the following link to build the tool: I have successfully created the tool as per my requirements. However, I am encountering some ...

Why does Froala Editor not maintain formatting when retrieving data from the database?

I've been using the Froala editor to add content on my website, and it's doing well for inserting data into the database. However, I'm facing an issue when retrieving data from the database as it doesn't maintain the original formatting ...

Delete the event handler if the original selector used to set up the handler is unknown

I need to remove an event handler from the element. The current handler is added by a third-party plugin, and I want to prevent its behavior. As far as I know, the functions off() and unbind() require the original selector to be used. The issue arises b ...

Ensuring the consistency of actions through thorough testing

Currently utilizing xstate for the implementation of a login flow. Within my machine, the initialState triggers a Promise, which redirects to a state with an entry action if rejected. I am seeking to properly test the timing of when this action is called. ...

When CSS is modified by inserting an extra div, it causes the positioning of other elements to shift towards

I'm currently working on adapting a method to make elements "sticky" for older browsers as discussed in this particular article. The basic idea involves implementing some JavaScript code that listens for scroll events. Upon detection of such an event ...

Updating partial data in MVC without having to reload the entire HTML content is a straightforward process that

I am looking to update partial view data without refreshing the HTML. I have utilized Json to fetch the data, but it seems like there might be some issues in the script. The Success part is not being executed. [HttpPost] public ...

Classic leading and CSS line-height traditional styling

The CSS specification dictates that line-height should be implemented by dividing the specified value by two and applying the result both above and below a line of text. This significantly differs from the traditional concept of leading, where spacing is ...

How to creatively position custom arrows in a React JS Nuka Carousel

Seeking assistance on properly positioning custom arrows within the Nuka Carousel component. After combining the decorators, I found that both of my arrows are appearing side by side. How can I address this issue? My goal is to have one arrow positioned in ...

What could be causing the computed property in Vue 2 component to not return the expected state?

I'm encountering an issue with my Vue component where it fails to load due to one of its computed properties being undefined: Error: Cannot read properties of undefined (reading 'map') Here is the snippet of the computed property causing ...

Implementing a Scalable Application with React Redux, Thunk, and Saga

What sets Redux-Saga apart from Redux-Thunk? Can you explain the primary use of redux saga? What exactly is the objective of redux thunk? ...

What could be causing the issue: Unable to locate or read the file: ./styles-variables?

I'm currently following a tutorial on how to create responsive layouts with Bootstrap 4 and Angular 6. You can find the tutorial here. I've reached a point where I need to import styles-variables.scss in my styles file, but I keep encountering t ...