Search code examples
cssrshinyrangesliderion-range-slider

In Shiny, how can I change the formatting of the labels of a range slider?


Shiny makes use of the ion-rangeslider.

I'm trying to change the formatting of the labels of a range slider. In the code below, .irs-single { color: black; background: transparent } removes the default blue background of the label in the regular slider (top), but it has no effect on the labels of the range slider (bottom).


shiny output

library(shiny)

ui <- fluidPage(
  sliderInput("test1",
              "Select a value:",
              min = 0,
              max = 50,
              value = 20),
  sliderInput("test2",
              "Select a range:",
              min = 0,
              max = 50,
              value = c(30, 40)),
  tags$style(type = "text/css",
             HTML(
               ".irs-single { color: black; background: transparent }")
             )
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

I tried adding each of the following, directly targeting the range slider:

  1. .js-irs-1 .irs-single { color: black; background: transparent }
  2. .js-irs-1 .irs-single.to { color: black; background: transparent }
  3. .js-irs-1 .irs-single.from { color: black; background: transparent }

But the rang slider's labels still come out with their default color, blue:

shiny output


Not that it's possible to, for instance, directly target the formatting of one of the range slider's buttons:

.js-irs-1 .irs-slider.to { background: red }

shiny output


Solution

  • library(shiny)
    
    css <- "
    .irs-from, .irs-to { color: black; background: transparent }
    "
    
    ui <- fluidPage(
      sliderInput("test1",
                  "Select a value:",
                  min = 0,
                  max = 50,
                  value = 20),
      sliderInput("test2",
                  "Select a range:",
                  min = 0,
                  max = 50,
                  value = c(30, 40)),
      tags$style(type = "text/css", HTML(css))
    )
    
    server <- function(input, output) {
    }
    
    shinyApp(ui = ui, server = server)
    

    enter image description here