Boosting the specificity of Material UI override styles

I came across a question about Material UI Overriding styles with increased specificity that seemed related to my issue, but I couldn't apply the solution to my problem.

My problem involves a vertical Slider with marks, and I am trying to remove the text-transformation for the marks.

This is how I am setting the styles:

const useStyles = makeStyles((theme) => ({
    vertical: {
        marginRight: theme.spacing(3),
        transform: 'none',
        '&$markLabel': {
            transform: 'none'
        }
    },
    markLabel: {
        transform: 'none'
    },
}));

I am trying to use these styles with the Slider like this:

  <Slider
      orientation="vertical"
      marks={marks}
      classes={{
        vertical: classes.vertical,
        markLabel: classes.markLabel
      }}
  />

When inspecting in Chrome DevTools, I noticed that the vertical styles are being applied, but they are getting overridden by a more specific class

.MuiSlider-marked.MuiSlider-vertical
:

https://i.sstatic.net/Uv0T9.png

The same issue occurs with the text-transformation, where it is applied but then overridden by a more specific combination of two classes

.MuiSlider-vertical .MuiSlider-markLabel
:

https://i.sstatic.net/u4UTS.png

I also attempted using a Theme, but unfortunately, that approach did not work as expected:

createTheme({
    overrides: {
        MuiSlider: {
            markLabel: {
                transform: 'none'
            },
            vertical: {
                '&$markLabel': {
                    transform: 'none'
                },
            },
        }
    },
});

I would greatly appreciate any assistance with this problem!

Answer №1

The solution that has been effective for me in resolving the issue is as follows:

makeStyles:

const useStyles = makeStyles((theme) => ({
    root: {
        '& .MuiSlider-markLabel': {
            transform: 'none',
            left: '-14px',
            marginBottom: '17px',
        }
    },
}));

Slider:

<Slider
    orientation="vertical"
    marks={marks}
    classes={{
      vertical: classes.vertical,
    }}
    className={classes.root}
/>

By setting the styles for root using className, it is essentially achieving the same result as:

<Slider
    orientation="vertical"
    marks={marks}
    classes={{
      vertical: classes.vertical,
      root: classes.root
    }}
/>

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 React-Router-Config to dynamically set the page title

I am seeking advice on how to dynamically set page titles using the configuration file in conjunction with react-router-config. Should I use props or Helmet for this purpose? routes.js const routes = [ { title: 'Home', path: ...

Tips for creating a horizontally scrolling div

Here is the CSS for the div that needs to be able to scroll horizontally: .form-holder{ padding-left: 20px; width: auto; background: ; overflow-x: auto; max-height: 300px; padding-bottom: 30px; } Every ...

No response observed upon clicking on the li element within the personalized context menu

I created a custom context menu that pops up when you click on an li element within an unordered list. I'm trying to trigger an alert when clicking on an li item inside the context menu, but it's not working as expected. To handle this dynamic c ...

Encountering a "No exported member" error while attempting to include & { session: Express.Session } in the MyContext type while following a tutorial on React, Express, and Typescript

Currently exploring a React, Express, and Typescript tutorial, which is still quite new to me. I am trying to grasp the concept of setting up custom types, and this is what I have so far. In my types.ts file: import { Request, Response } from "expres ...

CSS: Fixed item at the bottom of a container that scrolls horizontally

I am working on creating a sticky footer within a horizontally scrolling element. I want the sticky footer to be positioned at the bottom, adjust its height based on the content inside it, and match the width of the scrollable parent (in this case 200px). ...

Can secret URL paths be concealed while utilizing React Router?

Just diving into React and I have a newbie question that Google isn't helping with. Maybe someone here has the answer: When using React Router or other declarative routing methods, how can you hide private URLs from users who shouldn't have acce ...

Download the PDF instead of just viewing or downloading it through a link

I need help configuring a link so that when the user clicks "download here," a PDF file is automatically downloaded to their desktop. Can someone provide guidance on how to achieve this? ...

Strategies for creating smooth transitions for elements using CSS transformations

Is there a way to smoothly fade in edits to elements in CSS when hovered over? I am looking to change the border from solid to dotted, but want the transition to be gradual. How can this be achieved? UPDATE: To provide more context, here is the current c ...

What is the best way to decrease the font size of every element in the DOM?

Is there a way to decrease the size of all DOM elements by a specific amount? Currently, I am using Bootstrap styles where h5 is set at 14px and I need it to be 12px, and h1 is at 36px when I want it to be 34px, and so forth. I have considered two option ...

Show a pop-up form when a user focuses on it using React

Hello, I have been looking for a way to create an overlay with a form that appears when a specific input field is clicked. I am trying to achieve this using React. Can someone please advise me on how to accomplish this? Here is my code import React, { Co ...

Nextjs Clerk: The <SignIn/> element lacks proper configuration

I attempted to resolve the issues indicated in the error message below, but unfortunately, I was unsuccessful. How can I troubleshoot this error and perform an “auth-callback with clerk”? - The technologies used include Nextjs, Nextjs Actions, Clerk, a ...

"Utilize binding in ReactJS to maintain the correct context

I'm currently learning how to update states in ReactJS by following a tutorial. In the tutorial, I came across this line of code: this.updateLanguage = this.updateLanguage.bind(this). Although I grasp the basics of 'this' and 'bind&apos ...

Introduction to Grid Layout Using Bootstrap - Rows

Encountering an issue with the vertical alignment of items in the code snippet below when using Bootstrap 4. Initially, the items are stacked vertically, but they align horizontally when either the "row" or "row align-items-start" classes are applied. &l ...

The error "navigator.permissions.query is not a defined object" is encountered in the evaluation

Whenever I try to access my website on an iPhone 7, I encounter a frustrating error. The main screen loads without any issues, but as soon as I click on something, a white bank screen appears. I believe this piece of code might be the cause: useEffect( ...

Why does i18next only function on http://localhost:3000/de (landing page) in NextJS and not on http://localhost:3000/de/about?

I'm new to this and feeling lost on how to tackle this issue. I tried following the instructions on https://github.com/i18next/next-i18next, but I'm struggling with index.js. When I toggle /de on my landing page, it translates correctly in the UR ...

CSS descendant selector add style excluding child elements

My issue involves two divs each containing a table. When I apply the CSS class "table" to the first div, the second table in the div is impacted as well. .tbl-generic th:nth-child(-n+2),td:nth-child(-n+2) { background-color:red; width:25px; } ...

Clearing Arrays in React Native Using useState

I'm struggling with the following code which aims to create an animated polyline for a map. I came across some examples online, but they were using outdated methods and didn't include useEffect or useState. I can't seem to clear the polylin ...

What's the best way to position menu items vertically in a navbar?

I've been struggling to vertically center the menu items "TEST 1","TEST 2" and "BRAND" within the navigation bar without any luck. I've experimented with vertical-align, margin-top, and bottom properties. What I'm aiming for is to have them ...

Calculate the sum of hours worked in a day using pure JavaScript, no external libraries required

Hello, I'm new to this website and please excuse me if my English is not perfect, I am trying my best to communicate :) I have been working on a page that calculates the total hours worked in a day and this is what I have achieved so far until 15/06 ...

Issue with Bootstrap columns being misaligned on mobile devices

On my website, I've implemented a Bootstrap Grid design with three columns under the container-fluid BS attribute, along with .row .no-gutters. While the boxes are perfectly aligned in the desktop view, they stack vertically on mobile and tablet devi ...