Search code examples
rshinyr-leaflet

How to change background of Leaflet map?


I'm creating a Shiny App using a leaflet widget. My map has a simple Shapefile with no basemap engaged. Leaflet renders a default grey background. I would like to change the background to white.

I see some answers using javascript code, but I am unfamiliar how to implement this in the R language.


Solution

  • You can change the Leaflet default background color with CSS, see this related question.

    You can use custom CSS in your Shiny app by adding it into the HTML head of the Shiny output:

    ui <- fluidPage(
      tags$head(
        tags$style(HTML(".leaflet-container { background: #f00; }"))
      ),
      # etc.
    )
    # etc.
    

    Other possibilites for adding styles, for example with external CSS files, are explained in the Shiny documentation.