Customize a theme component only when it is a child of another component

I am trying to customize the CSS of MuiButton only when it is nested inside a MuiDialog. https://i.stack.imgur.com/13doLm.png

In regular CSS, you could achieve this with:

.MuiDialog-root .MuiButton-root {
    border: 5px solid blue;
}

However, I am struggling to implement this in theme overrides:

theme.overrides = {
  MuiDialog: {
    root: {
      border: "7px solid red",
      "& .MuiButton": { border: "5px solid blue" }
    }
  }
};

Unfortunately, this code did not successfully apply the style to the button.

Can someone help me figure out if it is possible to override the style of a MUI button only when it's a descendant of a specific Mui component?

CodeSandbox

Answer №1

The primary style to use in the theme overrides is & .MuiButton-root

Answer №2

I successfully adjusted the background color of a button inside a dialog by using the correct className. Perhaps you may have missed specifying the right className:codesandbox.

 MuiDialog: {
    root: {
      border: "7px solid red",
      "& button": { backgroundColor: "blue" } // <- how to target MUIButton when descendent of MUIDialog?
    }
  }

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

Encountered an issue when utilizing props in the makeStyle function, as it raised an error stating that the property 'height' does not exist within the type '{}

Incorporating material styles and Typescript into my React component, the code snippet below showcases how I have implemented it: import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; const useStyles = make ...

The current page I'm working on is scrolling sideways, but I prefer a stationary layout without any scrolling

I am currently facing an issue with my webpage scrolling to the right. This behavior is not acceptable as web pages are not supposed to scroll to the right, correct? Could someone assist me in eliminating this unwanted scroll from my page? I have only u ...

Utilizing em units within CSS media queries is producing erratic outcomes on various browsers

My CSS media queries are set up in a progressive manner like the following: * { font-size: 14pt; } @media screen and (min-width:85em) { : : } @media screen and (min-width:110em) { : : } When I have two browsers open on the same screen, with identical siz ...

Replicate the ctrl+/- function through coding

While browsing with your browser, you have the ability to adjust the font size by using CTRL + + or CTRL + -. Is there a way to replicate this functionality through code, such as with JavaScript? I am looking to add buttons on my website that allow users ...

Issue with React's ScrollToTop component: Despite expectations, it fails to function as intended

I'm currently working on a React application and I've integrated a ScrollToTop component to help with scrolling functionality. However, I've encountered an issue where the component doesn't seem to be working correctly. I would greatly ...

There seems to be an issue with .ENV functionality in Razzle JS

Attempting to deploy a Razzle project on an Ubuntu server has been challenging. I have created a .env file with two variables: port=80 and host=192.168.1.5. However, when running the project, it defaults to localhost:3000. I've tried exporting PORT=80 ...

Changing the PublicPath of Create React App in a development environment: A step-by-step guide

I'm currently utilizing Microsoft.DotNet.Web.Spa.ProjectTemplates::2.0.0-rc1-final, which leverages Create-react-app and AspnetCore.ReactDevelopmentServer internally. Within my development environment, I am using IIS to host multiple APIs - api1 at h ...

The front end button stubbornly refuses to comply and redirect to another page

I am having trouble getting my button element to redirect my page to another page. I have tried using an onclick function inside the button tag with window.location.href, as well as creating a separate function for the redirect, but nothing seems to be wor ...

Starting up the express server with a React client

Up until this point, my projects have relied on the create-react-app tool, with the express-server and react client kept in separate directories. However, I am now exploring alternatives to create-react-app to gain a deeper understanding of how things wor ...

The drawbacks of using toJS() in React Redux: Is it really necessary in mapStateToProps()?

After reading through the Redux documentation, I came across a recommendation to not use Immutable with Redux. This advice has left me feeling confused. Why should I avoid using toJS() in the mapStateToProps function? It seems that React already uses Dee ...

Issues arise when trying to render a component in React after importing the react-bootstrap library

After running npm init and installing react, react-dom, bootstrap, and react-bootstrap packages, I encountered an issue while trying to import components from the react-bootstrap library. The code import Button from 'react-bootstrap/Button'; resu ...

Prevent row selection in MUI DataGrid when clicking on a cell

When interacting with my DataGrid, I've encountered an issue where clicking on a cell to enter text also selects/deselects a row. Is there a way to set the selection behavior to only occur when clicking on a checkbox? I attempted to resolve this by us ...

Enhance your Vue app by dynamically modifying classes with Tailwind

I am working on a Vue3 application that utilizes Tailwinds configured in the tailwind.config.js file. My question is, can I dynamically modify the value of a preconfigured class from the tailwind.config.js file? For instance: tailwind.config.js: const d ...

Develop a React npm package with essential dependencies included

I have been working on developing a UI library using React ts. As part of this project, I integrated an external library called draft-js into the package. However, when I attempt to implement my library in another project, I keep encountering errors. Despi ...

Switching from Functional State management to Class State management within React Native allows for more flexibility and control

Need help transforming this functional state to a class state. I can usually convert it easily, but what's throwing me off is that the third const state here relies on another state within itself; the state const scrollAnim is used inside the third st ...

Monitor the elements displayed as you scroll

Thank you for taking the time to read this. I am currently working on a webpage that features a top Calendar component displaying a week, and below that is a Scroll component showing detailed information for each day of the week. Here is what my page loo ...

A distinctive web page featuring an engaging image backdrop: captivating content effortlessly scrolling beneath

On my website, I have implemented a background image with a fixed transparent header. When scrolling down, I want the main content to be hidden beneath the header. To achieve this effect, I used the -webkit-mask-image property. However, this approach affec ...

I'm struggling to find the correct media query to fix the problem with resizing my window

Looking to create a layout where two large icons are displayed side by side horizontally, but then switch to a vertical layout when the window size is reduced (such as on a mobile phone). I know that media queries are necessary for this, but I am unsure of ...

What could be causing transition to not be recognized as an element in HTML?

<template> <header> <nav class="container"> <div class="branding"> <router-link class="header" :to="{name : 'Home'}">>FireBlogs</router-link> </div& ...

Horizontal expanding and collapsing navigation menu

Is there a way to modify the expand menu bar so it expands from left to right or right to left, instead of top down? Any assistance would be greatly appreciated. Existing Expand Menu Bar <HTML> <HEAD> <META http-equiv="Content-Type" c ...