Fix the position of the shiny dashboard header toggle button

477 Views Asked by At

In the shiny app below I want the toggle button that shows and hides the sidebar to remain fixed in its position whether the sidebar is hidden or not. Now when the sidebar is hidden there is a blue square empty gap between the button and the beginning of the page.

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
ui <- dashboardPage(
  
  dashboardHeader(
    titleWidth = 0
    
  ),
  dashboardSidebar(
  ),
  dashboardBody(
    tags$head(tags$style(HTML('
    /* navbar (rest of the header) */
        .skin-blue .main-header .navbar {
                                background-image:url("https://www.r-project.org/logo/Rlogo.png");
                                background-position-x: 3%;
                                background-size: 170px 40px;
                                background-repeat: no-repeat;
                                background-color:#000;
                              }
        
       
                              '))),
    tags$style(type="text/css",".sidebar-toggle{ position: absolute;
    left: 0;
}")
    
  ),
  controlbar = dashboardControlbar(id = "dashboardControlbarID", collapsed = TRUE,skin = "light"

)
)

server <- function(input, output){
  
  
  
}

shinyApp(ui, server)

enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

You just have to reduce the left margin - please see margin-left: 0px !important;:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
ui <- dashboardPage(
  dashboardHeader(titleWidth = 0),
  dashboardSidebar(),
  dashboardBody(
    tags$head(tags$style(HTML('
    /* navbar (rest of the header) */
        .skin-blue .main-header .navbar {
                                background-image:url("https://www.r-project.org/logo/Rlogo.png");
                                background-position-x: 3%;
                                background-size: 170px 40px;
                                background-repeat: no-repeat;
                                background-color:#000;
                                margin-left: 0px !important;
                              }
        
       
                              '))),
    tags$style(type="text/css",".sidebar-toggle{position: absolute; left: 0;}")
  ),
  controlbar = dashboardControlbar(id = "dashboardControlbarID", collapsed = TRUE,skin = "light")
)

server <- function(input, output){}

shinyApp(ui, server)