Tips for customizing your MUI slider design

import * as React from "react";
import Box from "@mui/material/Box";
import Slider from "@mui/material/Slider";

function valuetext(value) {
  return `${value}°C`;
}

export default function RangeSlider() {
  const [value, setValue] = React.useState([20, 37]);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <Box sx={{ width: 300 }}>
      <Slider
        getAriaLabel={() => "Temperature range"}
        value={value}
        onChange={handleChange}
        valueLabelDisplay="auto"
        getAriaValueText={valuetext}
      />
    </Box>
  );
}

Is it possible to style the range within the thumb green and the range outside of both thumbs a lighter green as shown in the image below?

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

Answer №1

You have the ability to personalize your slider using the styled utility:

const green500 = "#228b22";
const green900 = "#7FFF00";

const CustomSlider = styled(Slider)(({ theme }) => ({
  color: green500, //set the color of the slider between thumbs
  "& .MuiSlider-thumb": {
    backgroundColor: green500 //customize the color of the thumbs
  },
  "& .MuiSlider-rail": {
    color: green900 //adjust the color of the slider outside the area between thumbs
  }
}));

See Demo

Answer №2

Make sure to add the following classes to your CSS stylesheet. The "Track" class represents the area between the slider thumbs, while the "Rail" class refers to the space outside both thumbs.

.CustomSlider-thumb{
  color: blue;
}
.CustomSlider-rail{
  color: cyan;
}
.CustomSlider-track{
  color: navy;
}

Answer №3

To achieve this in a different way, you can create your own custom styles.

const CustomizedSliderStyles = {
    '& .CustomThumb': {
        color: "#8f4c59"
    },
    '& .CustomTrack': {
        color: "#8f4c59"
    },
    '& .CustomRail': {
        color: "#dbe39f"
    },
    '& .CustomActive': {
        color: "#72b7be"
    }
};

Then simply apply them like so:

<Slider customStyles={CustomizedSliderStyles} />

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

Ways to address the issue of using the keyword 'await' outside of an async function in React

I am currently facing an issue where I need to dynamically retrieve the device's IP address (as it changes over time) in order to fetch JSON data from the database using a specific link like this: https://{ipaddress}:5000/users from a node.js server. ...

Issue with Azure App Service on Windows: The file is not being unzipped in the wwwroot folder by Azure DevOps Release

I have successfully developed an application using NextJS My goal is to deploy this app on an Azure App service The deployment was a success on the Vercel server without any need for customization Currently, I am utilizing DevOps tools such as Pipelines ...

CSS: Specify child element to have a full width of 100%, but appear only 50% wide

Looking at this page, it seems that the header photo is not displaying full width. #wrap { width: 990px; } #content-wrap { display: flex; } .image-header { display: block; width: 100%; } .image-header img { width: 100%; height: auto; } .cont ...

Dividing React Router into individual files

I am facing an issue while trying to implement routes in my react project with three separate components: Window.js import React, { Component } from 'react'; import SideBar from "../SideBar/SideBar"; import MainBody from "../MainBody/MainBody"; ...

Exporting all components using require: A complete guide

Currently, I am working on a Vue js application where I am tasked with exporting all the files and components from a specific directory. These files/components need to be imported into a separate file named paths.js. If I use the require function to impor ...

Is it necessary for NPM to execute scripts labeled as dependencies during the npm i command execution?

When npm i is run, should it execute the scripts named dependencies? I've observed this behavior in the latest version of Node (v19.8.1) and I'm curious if it's a bug. To replicate this, follow these steps: mkdir test cd test npm init -y T ...

Looking for a way to focus on an element similar to `event.target.focus()`

I am in the process of developing a dynamic list component that generates an empty text field every time an item is added to the list. The structure of the component is shown below: const DynamicInputList = ({ inputs, onChangeArray, label, name, errors }) ...

Asynchronous function nested within a loop

Hello there! I am currently working on converting a SQLite database to NeDb using the following code snippet: const sqliteJSON = require('sqlite-json'); const Datastore = require('nedb') const exporter = sqliteJSON('etecsa.db&apo ...

Error: Unable to access data from an unidentified source (reading '0') in open ai

` this is the embedding.ts file import { OpenAIApi, Configuration } from "openai-edge" const config = new Configuration({ apiKey: process.env.OPENAI_API_KEY, }); const openai = new OpenAIApi(config); export async function getEmbeddings(t ...

Accessing a single element from a nested object in Handlebars.js

Recently, I have been working on a nodejs application that utilizes handlebars js as its view engine. My main challenge at the moment is accessing one specific element from a nested object that I am passing to the hbs view. router.get('/view_users/:i ...

Creating a new array set by utilizing the map method

How can I filter and return a new array using the map function based on 2 conditions: The array must not be empty The role should be "moderator" This is an example of my initial response: Below is a React function that utilizes the map method to proces ...

The Vue computed property is failing to retrieve the data it needs

I'm having trouble with the computed() property not retrieving data. Data was initialized in the created() property. Am I missing something here? Any guidance on how to resolve this issue would be greatly appreciated. const randomPlayers = { temp ...

Tips for utilizing jQuery Ajax data action

I've been trying to understand how to effectively utilize the data parameter in a $.Ajax call. However, I am facing confusion regarding the 'action' part within the data call. Is it meant to trigger an action in a controller? If so, how can ...

Improving HTML coding with a more effective alternative to document.write()

Looking at the setup of my website, I have a menu button and comments section that need to appear on every page. Instead of manually adding this code to each HTML file, I decided to streamline the process by using a JavaScript file that generates all of th ...

I attempted to include multiple images in my HTML using Vue.js, but encountered an error message stating "illegal invocation"

Here is my HTML code: <form method="POST" v-on:submit.prevent="handleSubmit($event);"> <div class="row"> <div class="col-md-4"> <div class="form-group label-floating"> <label class="control-label">Name</label> <input ...

What is the best way to make a Dojo TitlePane overlap using CSS?

My dilemma involves a jsfiddle featuring two TitlePane widgets in the central pane's top right corner. Currently, clicking on the right TitlePane ("Switch Basemap") moves the left TitlePane ("Map Overlays") to the left. What I want is for the right Ti ...

Having trouble installing firebase with npm in a react project - encountering various errors during the process

Struggling to set up firebase but facing roadblocks... Tried various solutions: npm cache clean --force npm install --save react-firebase Please assist! PS C:\Users\SharmaShashtra\amzclone> npm install firebase npm WARN config global -- ...

Is there a problem with the toolbar navigation icon color not updating?

I am experiencing an issue with my Material toolbar where the navigation icon color is not changing properly. Although I have made changes in the Layout validation, the updated color is not reflected when I install the app on my phone. For reference, here ...

Is there a way to link table A's ID to table B's userID in a postgreSQL database?

Is it possible in PostgreSQL to establish a relational index between table A ID and table B userId for the purpose of joining two tables based on their ids? To provide further clarification, here is an example using MongoDB and Mongoose: const Billing = ...

When anchor is set programmatically, the focus outline is not displayed

I am currently facing an issue with my application where I am programmatically setting focus on elements in certain scenarios. While it generally works well, I have noticed that when I set the focus on an anchor element using $("#link1").focus(), the focus ...