I am trying to add my company’s logo to an R shiny app, made using {bslib}. I tried different ways of adding this image to the ‘title’ argument of ‘page_navbar’. While the image gets added, it looks wonky and changes the position of the other items in the header ribbon. An example image and the logo attached.
Here is a demo code that illustrates the problem:
library(shiny)
library(bslib)
ui <- page_navbar(
title = div("My app",
img(src = "WCTMainLogoWhite_edited.png", height = "57.5px", width = "auto",
style = "position: absolute;
top: 1px;
right: 2%;")),
theme = bs_theme(version = 5, bootswatch = "zephyr")|> ##setting the primary color of "zephyr" bootswatch theme manually
bslib::bs_add_rules(
rules = "
.navbar.navbar-default {
background-color: $primary !important;
}
"
),
nav_panel(title = "Trends",
layout_columns(
card(
full_screen = TRUE,
card_header(
"Card 1")
)),
layout_columns(
card(
full_screen = TRUE,
card_header("Card 2")),
card(
full_screen = TRUE,
card_header("Card 3")),
col_widths = c(12, 12)
)
),
nav_panel(title = "Instructions on use", p("Content to be added"))
)
server <- function(input, output, session) {}
shinyApp(ui, server)
Is there a better way to add the image, that will align with the other items in the header?
2
Answers
The best way to align items to center is by using flex.
Add
display: flex
&align-items: center
to parent div. That’s itIf you wish to fix your existing code, then add this properties to your logo.
position: absolute;
.top: 0
&bottom: 0;
.margin: auto 0;
.My personal favorite is using
flex
, ask further for any doubts. Cheers.I would use an approach which is described in this answer, the image is just appended to the
navbar
. This looks like this and is implemented below.However, if one is below the lg breakpoint this does not lead to a satisfactory result because then the
navbar
gets collapsed and the image position changes. Therefore I implementedcss @media (max-width:992px)
below which sets the image position tofixed
and also setsright: 10%
such that one still has access to thenavbar-toggle button
.You could also do this the other way around and switch the position of the image and the toggle button if you want to have the image on the right-hand side.
Unrelated here, but concerning the
setting the primary color of "zephyr" bootswatch theme manually
please note that I recently updated my answer here and provided an in my opinion better solution than the old one. I did not implement here in order to stay close to your provided code.