Modifying the Color of Individual Items within the Pagination Component in MUI

I am attempting to modify the background color of every item in my Pagination component by utilizing makeStyles.

import { Pagination } from "@material-ui/lab";
import { makeStyles } from "@material-ui/core";

const pagination = makeStyles({
  root: {
    "& .Mui-selected": {
      color: "white",
      backgroundColor: "black"
    }
  }
});

export default function App() {
  const paginationClass = pagination();

  return <Pagination count={10} page={10} className={paginationClass.root} />;
}

Here is a link to view what I have accomplished so far.

Is there a way for me to adjust the color of each Pagination Item using makeStyles?

Answer №1

To give a distinct background color to individual items, you must enclose the Pagination component and define the style for the pagination using the makeStyles variable within the wrapper. Subsequently, you can manage the list of pagination elements using the CSS property :nth-of-type().

Note: The styling begins from the second element as the first and last elements represent arrows.
import { Pagination } from "@material-ui/lab";
import { makeStyles } from "@material-ui/core";

const pagination = makeStyles({
  root: {
    "& li .Mui-selected": {
      color: "white",
      backgroundColor: "black"
    },
    "& li:nth-of-type(2) .Mui-selected": {
      backgroundColor: "red"
    },
    "& li:nth-of-type(3) .Mui-selected": {
      backgroundColor: "green"
    },
    "& li:nth-of-type(4) .Mui-selected": {
      backgroundColor: "orange"
    },
    "& li:nth-of-type(5) .MuiPaginationItem-root": {
      backgroundColor: "violet"
    },
    "& li:nth-of-type(6) .MuiPaginationItem-root": {
      backgroundColor: "magenta"
    }
    ....
  }
});

export default function App() {
  const paginationClass = pagination();

  return (
    <div className={paginationClass.root}>
      <Pagination count={10} page={2} />
    </div>
  );
}

https://codesandbox.io/s/mui-how-to-change-the-color-of-each-item-in-the-pagination-component-rrd06s?file=/src/App.js

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

Modify the CSS style of the select label and change the color of the currently selected option in MUI

For the past few days, I've been facing challenges with implementing MUI components while working on a page for a React app. I'm almost finished with my project, but there are 2 key things missing... On my page, I have utilized a Select and an ...

How can Angular2 detect when an entity is clicked within a window?

There are multiple items generated using *ngFor: <my-item *ngFor="let item of myArray" [p]="item"></my-item> I am able to handle a click event like this: <my-item ... (click)="doWork(item)"></my-item> However, I want to avoid a ...

Tips for displaying a background image without obstructing the container class on your website

How can I set a background image for my website? Whenever I use the code background-image:url('path_to_image');, it ends up covering my container class. Is there a way to place the image behind the container class? ...

When I try to increase the date by 1 day using the MUI X Date picker, it does not update as expected

I have encountered an issue where adding 1 day to my date picker on button press results in it jumping to 01/01/1970. The same occurs when attempting to subtract 1 day from it. My goal is to achieve the following: Refer to this image of my Date picker: D ...

Turning a text into a JSON data structure

How can I make JavaScript recognize a string as JSON? I have a function that only works when passed a JSON object. If I pass a string with the same format as JSON, it doesn't work. I want to find a way for the function to treat the string as JSON, ev ...

Issue with express-validator returning undefined value on forms set to enctype='multipart/form-data'

Currently, I am developing a login authentication application using basic node.js+express. While extracting values (such as name, email, etc) from the registration page, I utilize express-validator for validation. However, I encounter an issue where all va ...

Display images that have been uploaded on the page either as a grid view or a list of

In my Reactjs application, I am uploading multiple images to Azure Blob Storage. On a page, there is a button to open the upload form. Once the upload completes, I want to display all these images on the same page. Since the images have different aspect ...

Delete an <li> element upon clicking a span tag

I've been attempting to fadeOut some <li> elements, however I haven't had any success. Here's my code below: <li class="lclass" id="1"> First Li <span class="removeit" id="1" style="color:#C00;">Remove</span> ...

Is it possible to align the second "navbar" div inline with the first "navbar" div?

I am struggling to align the div "nav2" on the same line as "nav1", but it just won't move up. I've tried using float, but it's not working. * { padding: 0; margin: 0; box-sizing: border-box; } body { width: 100%; height: 100vh; } #navb ...

Creating a table with React components to display an object

I'm a React novice struggling to render an object in a table. While I can access the ctx object using console.log, I can't seem to display it in the table on the browser. Any help and advice would be greatly appreciated. The code snippet is provi ...

When the clearInterval function is invoked - either when the timer is modified or when the rendering is detached from the setInterval it is linked to

As a new React developer, I've come across a problem that has me stuck. Does the setInterval associated with a specific render get cleared automatically? import React, { useState, useEffect, useRef } from "react"; import ReactDOM from ...

Using two different colors in text with CSS

Is it possible to have text in two different colors like this: https://i.stack.imgur.com/R40W1.png In terms of HTML, I tried looking it up but found answers related to:- https://i.stack.imgur.com/GRAfI.png ...

Leveraging JSON Data in Highcharts

I am looking to create a column range chart using Highcharts to display a series of high and low temperatures. I found a demo example that showcases the kind of chart I want on the Highcharts website at: The data I have is stored in a file named datatest. ...

Creating a spinning Cube with separate fields for x, y, and z rotation speeds: a step-by-step guide

After dabbling in Java, Visual Basic, HTML, and CSS, I'm now looking to create an interface featuring a central spinning cube with 3 fields to control its x, y, and z rotation speeds. Can you suggest which language would be the best fit for this proje ...

Tips for boosting performance and minimizing unnecessary rerenders (Todo app case study using hooks)

Exploring React hooks deeper, I've been striving to enhance the performance of my components. Despite using useCallback appropriately, my application seems sluggish due to frequent updates. Is there a way to optimize my components so that only one or ...

Obtain an item from an array by utilizing the property with Lodash _.find

I currently have an array of objects called memberToChange.checkboxes: ICheckbox[] structured like this: https://i.stack.imgur.com/LyKVv.png Within this setup, there is also a variable named internalNumber with the value "3419". My objective is to locate ...

How can I transfer information from Node.js to Vue in an Nuxt.js server-side rendering setup?

I need to find a way to pass Firestore data to Vue files from Node.js (Express) because I have to utilize Firestore's getCollections() method, which cannot be executed on the client side. I have set up nuxt-ssr for deploying Google Cloud Functions an ...

Rotation of Weekly Menus

I have a project to enhance our Menu Order Website by adding a weekly rotating menu feature. I have completed the entire website, including the admin panels, but I am struggling to implement the rotating menu functionality. Currently, all menus are include ...

The issue with the Woocommerce quantity increment buttons not functioning properly persists after an AJAX refresh, and the automatic load feature only activates after two

I've hit a brick wall with this particular issue. Many people have suggested solutions, but none seem to be effective for me. My situation probably resonates with quite a few individuals: I decided to customize the WooCommerce quantity input (/global ...

What method does nosotroshq.com use to achieve the navigation effect when hovering over a menu item?

Seeking advice from skilled individuals familiar with JQUERY: Can anyone explain the technique used by nosotroshq.com in their top navigation bar? Specifically, how do they create the slow animation when hovering over "ABOUT US"? Any insights would be ap ...