Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style individual bsTooltip (shinyBS) elements

I am trying to add some tooltips to different action buttons in my shiny app through the bsTooltip() function of the shinyBS package, and I would like to modify the width just of a specific tooltip box. To do that, I can specify the HTML tags at the beginning of my UI and modify directly the CSS, but if I use the simple element .tooltip {...} I modify the width of every tooltip in my code:

Below you can find a minimal reproducible example with two different action buttons:

library(shiny)
library(shinyBS)

library(shiny)

ui <- fluidPage(

  tags$head(tags$style(HTML(".tooltip {width: 300px;}"))),

  br(),

  actionButton(inputId = "button1",
               label = "First"),
  bsTooltip(id = "button1",
            title = "Bonjour!",
            placement = "right",
            trigger = "hover"),

  br(),
  br(),

  actionButton(inputId = "button2",
               label = "Second"),
  bsTooltip(id = "button2",
            title = "Hello!",
            placement = "right",
            trigger = "hover")

)

server <- function(input, output, session) {

}

shinyApp(ui, server)

I've been in this situation before, for example when I had to modify the color of the placeholder of a specific textInput() widget. To do that, in my HTML() function I specified:

tags$head(tags$style(HTML("#textinput_ID::placeholder {color: #EEE1525;}")))

but this doesn't seem to work in this case.

Thanks a lot for your help!

like image 315
nd091680 Avatar asked Dec 05 '25 12:12

nd091680


1 Answers

You can wrap your actionButton and bsTooltip in a div with an id. Now you can select this div by it's id and only style the tooltip inside.

library(shiny)
library(shinyBS)

ui <- fluidPage(

  tags$head(tags$style(HTML("#button1_div .tooltip {width: 300px;}"))),

  br(),
  div(id='button1_div',
      actionButton(inputId = "button1",
                   label = "First"),
      bsTooltip(title = "Bonjour!",
                placement = "right",
                trigger = "hover")),

  br(),
  br(),

  actionButton(inputId = "button2",
               label = "Second"),
  bsTooltip(id = "button2",
            title = "Hello!",
            placement = "right",
            trigger = "hover")

)

server <- function(input, output, session) {

}

shinyApp(ui, server)
like image 64
Wilmar van Ommeren Avatar answered Dec 07 '25 02:12

Wilmar van Ommeren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!