Customize Typography style variations in Material-UI version 5

I have encountered a similar issue, but with a twist from Can't override root styles of Typography from Materil-UI

Back in v4, I had a theme set up like this:

import { createTheme } from '@material-ui/core/styles';

export const theme = createTheme({
  palette: {
    type: 'light'
  },
  typography: {
    fontFamily: ['Work Sans', 'sans-serif'].join(','),
    fontSize: 14,
    h1: {
      fontFamily: ['Rubik', 'sans-serif'].join(','),
      fontSize: 20
    },
    h2: {
      fontFamily: ['Rubik', 'sans-serif'].join(','),
      fontSize: 18
    },
    h3: {
      fontFamily: ['Rubik', 'sans-serif'].join(','),
      fontSize: 16
    },
    h4: {
      fontFamily: ['Rubik', 'sans-serif'].join(','),
      fontSize: 16
    }
  }
});

Previously, this setup successfully overrode the font size for the

<Typography variant='h2'>h2 heading</Typography>

However, after upgrading to v5, this override no longer functions as intended.

I attempted to address this by adding a components section to my theme:

  components: {
    MuiTypography: {
      styleOverrides: {
        h2: {
          color: 'red',
          fontSize: '18px'
        }
      }
    }
  }

Unfortunately, this solution also did not produce the desired result.

I have observed that a new CSS class is now being applied to the HTML, which overrides previous styling rules.

What steps are needed to ensure that all Typography variants are overridden as they were in v4? Newly added class This additional class was not present in v4's HTML structure

Answer №1

To customize a variant using style overrides, you can follow the example below:

export const customTheme = createCustomizedTheme({
  components: {
    CustomTypography: {
      variants: [
        {
          props: { variant: 'customVariant' }, /* component properties */
          style: {
            /* insert your customized styles here: */
            backgroundColor: 'blue',
            fontWeight: 'bold',
          },
        },
      ],
    }

I found success with this approach. Hopefully it works for you too :)

Answer №2

The problem has been identified and resolved. Within the file app.ts, I originally wrote:

import { ThemeProvider } from '@mui/styles';

However, it should have actually been written as:

import { ThemeProvider } from '@mui/material/styles';

Answer №3

After following the steps mentioned above and seeing no progress, I decided to remove all MUI V4 references from my package.json file. Surprisingly, that finally resolved the issue for me. Hopefully this solution can assist someone else as well :)

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

Divs gracefully appear one after the other in a sequential manner

I am attempting to showcase a sequence of 4 divs in a row using the ease-in CSS transition feature. I came across this example that demonstrates what I am aiming for: http://jsfiddle.net/57uGQ/enter code here. However, despite my best efforts, I am unable ...

The scrolling function of the jQuery UI select menu is not working properly on the Android browser

I am currently developing a mobile application that utilizes the jQuery UI select menu. The functionality works well, but I have encountered an issue with certain drop downs being too long to display properly. While my PC shows scrollable floating divs as ...

What is causing the element to disappear in this basic Angular Material Sidenav component when using css border-radius? Check out the demo to see the issue in action

I have a question regarding the Angular Material Sidenav component. I noticed that in the code below, when I increase the border-radius property to a certain value, the element seems to disappear. <mat-drawer-container class="example-container" ...

Mastering the integration of Sass with NextJS

After successfully styling my NextJS app with SCSS modules, I encountered an unexpected error when I returned to work on it later: Syntax error: Invalid CSS after "...ound: variables": expected expression (e.g. 1px, bold), was ".$main-gradie ...

The CSS grid-template-area feature is not functioning properly in web browsers

Struggling with creating a responsive CSS grid layout for different screen sizes. I want to have five divs on desktop screens and adjust the layout for smaller screens. As a newbie in web development, I'm finding it challenging to get it right. .fir ...

Building a Dynamic Grid Design using Material-UI in a React App

I am currently diving into the MUI grid system for the first time and feeling a bit lost. My goal is to create a responsive vertical grid layout, and I have attached an example image for reference. Below is my attempt using the default MUI component. I w ...

The art of styling <li> elements compared to <a> elements

While these inquiries may lean towards being subjective, I am interested in learning about the industry's generally accepted best practices: 1) With responsive design and collapsible menus becoming more common, should a nav or nav menu element be sty ...

The transparency of my navbar renders it elegant, yet I desire to imbue the toggle background with a vibrant hue

I'm facing an issue with the transparent Navbar I built in Bootstrap 5. On mobile toggle view, the navigation links are getting lost within the text of the hero image. To resolve this, I want to keep the main navigation bar transparent and change the ...

Sequence of background colors not altering as intended

After clicking a button, I included the following jQuery code in a code snippet on my WordPress site: jQuery("#cf_course_planner tr").css("background-color", "#f66"); jQuery("#cf_course_planner tr:eq(0)").css(& ...

modify the inherent CSS property

I am working with a component that I have inherited, including its CSS style, and I need to modify one of its properties. Current CSS: .captcha-main-image { width: 100%; height: auto; } <img class ="captcha-main-image" id="captchaImage" src= ...

Images stored locally are not appearing in React JS applications

I'm currently exploring React JS and Material UI to develop a dynamic web application. I'm attempting to link a local image using 'url(${process.env.PUBLIC_URL})' but unfortunately, the image is not showing up for some unknown reason. ...

The alignment of the image background is malfunctioning on the iPad 3 device

Hey there, I need some assistance resolving an issue with a header background image on iPad3. The problem is that the image is not displaying correctly on iPad3 browsers, even though it works fine on desktop browsers. Below you can find my CSS and HTML cod ...

Pictures Unavailable to Show in Table

I've been working on a project where I need to insert some images into a table. Usually, this isn't an issue for me. However, today when I added the images to the table, all I see is a square border with the alt text inside it. I'm confident ...

"Strategically placing elements on an HTML grid

My current project involves creating a grid layout in HTML using CSS. The goal is to use this layout for various elements such as images, text, and links. What I envision is a visually appealing grid where each object fits together seamlessly with no gaps ...

What is the best way to stack a canvas box on top of another canvas box?

My goal is to have two canvas squares stacked on top of each other. While I am familiar with drawing on canvas, I need assistance in placing one canvas on top of another. Despite my attempts at setting the position, I have not been successful. <canvas ...

Adding custom styles to the calendar in the DatePicker component of Material-UI: A step-by-step guide

I'm currently using a DatePicker component from Mui and I am attempting to customize the Calendar component by including the properties position:absolute, left, and top. My objective is to align the right side of the TextField component with the cale ...

Align the text in the center of the image, which is depicted as a bullet point

I am currently using images as bullet points for my Github Pages (markdown files), following the instructions outlined here. My goal now is to vertically center the text next to the image. This is demonstrated here (cf. lower third of page), however, I am ...

Aligning a div within another div and organizing its elements

Hey everyone, I'm struggling with centering a div that contains two Tumblr posts columns, as shown in the screenshot linked below. I really want it to be centered on the page where there is no sidebar present. Additionally, I'm hoping to get the ...

Issues with PHP Form Email DeliveryIt seems that there is a

I currently have a ready-made form that is constructed using html, css, jquery, and ajax. It's basically copied and pasted onto my contact page for now. This is the code I include before the html tag on the contact.php page. <?php session_name(" ...

Dependency resolution error: Unable to find a suitable version of react with the specified peer requirements in @material-ui/[email protected] package

I started a new project with React by running npm init -y npm install react react-dom and then attempted to add Material UI: npm install @material-ui/core However, an error occurred: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependenc ...