Tips for adjusting the slider label to move horizontally to match the width of the slider using CSS

Is it possible for the label of the slider to smoothly slide along the X axis to adjust to the current width of the slider? I have demonstrated this effect in a gif. It's a bit challenging to describe in words, so please watch the gif to grasp what I am aiming for.

I've already created the slider using material ui and react, but I'm struggling to replicate the effect shown in the gif.

Below is the current CSS styling for the slider:

:root {
  --slider-width: 60%;
}

.slider-container {
  margin: auto;
  position: absolute;
  left: 200px;
  bottom: 50vh;
  width: var(--slider-width);
  z-index: 999;
  padding: 10px;
}

span [class^="PrivateValueLabel-circle"] {
  width: max-content !important;
  height: fit-content !important;
  transform: rotate(0deg) translateX(-35%) translateY(5px) !important;
  border-radius: 0% 0% 0% 0% !important;
}

span [class^="PrivateValueLabel-label"] {
  margin: 9px;
  transform: rotate(0deg) !important;
}

This is the code used for the slider:

import React, { useEffect, useState } from "react";
import Slider from "@material-ui/core/Slider";
import "./app.css";

const TextSlider = (props) => {
  const [value, setValue] = useState(-1);
  const [marks, setMarks] = useState([]);

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

  useEffect(() => {
    setMarks([
      "2020 trimestre 1",
      "2020 trimestre 2",
      "2020 trimestre 3",
      "2020 trimestre 4",
      "2021 trimestre 1"
    ]);
    setValue(0);
  }, []);

  if (value === -1) {
    return <div />;
  }

  return (
    <div className="slider-container">
      <Slider
        aria-label="asdsds"
        value={value}
        color="primary"
        getAriaValueText={(value, index) => {
          return marks[value];
        }}
        valueLabelFormat={(value, index) => {
          return marks[value];
        }}
        aria-labelledby="discrete-slider-custom"
        step={1}
        min={0}
        max={4}
        valueLabelDisplay="on"
        onChange={handleChange}
        track={false}
        marks={true}
      />
    </div>
  );
};

export default TextSlider;

To see the current slider with its limitations, visit this codesandbox link: https://codesandbox.io/s/material-demo-forked-fd3m5?file=/app.css

Answer №1

You have the ability to choose the style value of the parent element and then reset the translatex() value using CSS.

Here is an example using only CSS:

:root {
  --slider-width: 60%;
}

.slider-container {
  margin: auto;
  position: absolute;
  left: 20%;

  bottom: 50vh;
  width: var(--slider-width);
  z-index: 999;
  padding: 10px;
}

span [class^="PrivateValueLabel-circle"] {
  width: max-content !important;
  height: fit-content !important;
  transform: rotate(0deg) translateX(-35%) translateY(5px);
  border-radius: 0% 0% 0% 0% !important ;
}

span [class^="PrivateValueLabel-label"] {
  margin: 9px;
  transform: rotate(0deg) !important;
}
.MuiSlider-thumb[style="left: 0%;"] span.MuiSlider-valueLabel {
  transform: translate(45%, -35%);
}
.MuiSlider-thumb[style="left: 25%;"] span.MuiSlider-valueLabel {
  transform: translate(20%, -35%);
}
.MuiSlider-thumb[style="left: 75%;"] span.MuiSlider-valueLabel {
  transform: translate(-20%, -35%);
}

.MuiSlider-thumb[style="left: 100%;"] span.MuiSlider-valueLabel {
  transform: translate(-45%, -35%);
}
.MuiSlider-thumb[style] span.MuiSlider-valueLabel {
  transition: 0s;
}

It would be most efficient to handle this from the script itself while setting the left position for the newly created elements.

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

Update TypeScript definitions in version 2.2.2 obtained from NPM @Types

I am currently utilizing the component react-router-bootstrap along with the definitions from DefinitelyTyped. However, the downloaded definitions do not align with the component. While I have submitted a pull request to rectify this issue, it has not yet ...

I am having trouble setting margins for a sticky position in a WordPress website

Can someone help me reposition the left-sidebar with a sticky top to be below the main header when scrolling down? Adding margin-top to sticky-top under col-sm-1 is not working. <style> .col-sm-1 .sticky-top { margin-top: 50px} </style> ...

How can I position a floating label within a form-control in Bootstrap 5, while also setting its width to

I am currently utilizing Bootstrap 5 and attempting to incorporate floating labels with centered form controls that have a width set to auto, as opposed to the default 100% in Bootstrap. My challenge lies in getting the floating labels to remain inside the ...

Unable to change background image using jQuery

Snippet: const imageUrl = 'img/star.png'; $("#venueId-"+nodeId).css('background','url("'+imageUrl+'") no-repeat top right'); This piece of code is the start of a function that triggers upon clicking. The goal is to ...

Automatically adjust and separate list items for more organized spacing

Imagine you have a container that is 780px wide, with 6 items inside ranging from 10px to 130px in width. Your goal is to fill the entire 780px space with these items and evenly space them out using margin. You may have tried using a table layout, but en ...

The export of 'alpha' is not available in the '@mui/system' module

Help! I am encountering an error related to the @mui/material library. I have already looked into the package.json file of mui/system and it seems that 'alpha' is exported in it. ./node_modules/@mui/material/styles/index.js Attempted import erro ...

Activate dark mode automatically in material-ui

According to the official documentation: The documentation mentions that a dark mode theme will be automatically generated and reflected in the UI, but I am encountering issues with it. Dependencies: "@emotion/styled": "^11.0.0", ...

Styling the Navigation in Wordpress

Could someone please help me figure out why the navigation on my homepage is distorted in versions before IE10, but displays correctly on the post page? I'm stumped as to why it functions properly in all other browsers. ...

Creating a CSS grid with multiple rows and columns that expand dynamically

Currently, I am facing challenges with the layout of this CSS design. The number of items ("n") is dynamic and can increase indefinitely. My goal is to create a layout that consists of n rows and many columns (or an unlimited number of columns). For insta ...

Trying out enzyme's componentDidUpdate method in ReactJS testing

I have been attempting to test the componentDidUpdate method, but I am struggling to cover it completely. This is how I am approaching the testing: it('should execute storeCardDesigns if specific conditions are met in componentDidUpdate', () ...

Tips for creating an input field that automatically expands and has a specific width

I am facing an issue with the alignment of my search field. Here is the code snippet: #menu_search{ position: absolute; width: 100%; max-width: 1280px; display: inline; float: right; right: 0px; z-index: 99; } I would like th ...

The dreaded "fatal error: JavaScript heap out of memory" message struck once again while using npx

My attempt at setting up a boilerplate for a React app involved using the command npx create-react-app assessment D:\React>create-react-app assessment Creating a new React app in D:\React\assessment. Installing packages. This might take ...

Is there a way to adjust the spacing between a specific background image in a design pattern? The image must be sourced online

I am currently working on creating a background pattern using only online images, and I need to adjust the spacing between them. Is there a way to specifically control the space between the images in the pattern using CSS? I know about the round and space ...

Saving the Chosen Option from Button Group into react-hook-form State

Struggling to save the chosen value from MUI Button Group into react-hook-form's state, but encountering challenges with the update not happening correctly. view codesandbox example Below is a simplified version of my code: import { ButtonGroup, But ...

Creating custom components in React

At times, I find myself in need of a special UI component such as a multiple range slider. However, I have a preference for not relying on third-party libraries, so I usually opt to create the component myself. As time has passed, I have completely stopped ...

Setting up multiple listeners on a single server in the Caddyfile - a comprehensive guide

I am relatively new to using Caddy for web server configurations. Currently, I have a setup with 2 servers - a backend and a frontend. The frontend is a React application. My goal is to use reverse proxies in Caddy to route incoming requests from the inter ...

How can I implement a hover-over image change effect similar to the one seen in Gmail accounts?

I am attempting to implement a feature that allows users to change their image, similar to what can be done in a GMail account. When the user hovers over the image, an option to "change image" should appear. This may be a new concept for me because I am ...

Designing React JhiPagination component with styles for active page

The JhiPagination component is an abstract component generated with Jhipster. It has an activePage prop that I have defined, similar to the default generated entity page. However, the resulting pagination does not display the current active page class as s ...

Having trouble with pinning the expand column on the far left side of my Material React Table

I am utilizing the Material React Table to display my data in a tree-like structure. My requirement is to pin the expand column (used for collapsing or expanding rows) at the leftmost position, followed by pinning the tasks column. While I have successful ...

Arrange the navigation bar in a straight line

I am facing an issue with my navigation bar... I want it to be centered but it is not working. I want it to fill from left to right. I have tried using the following CSS: text-align: center in .navbar a text-align: center in navbar. .navbar { backgr ...