You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在含嵌入图片的Shiny Dashboard标题中恢复文字白色?

解决Shiny Dashboard标题文字变色问题

当你用tags$a包裹标题内容时,HTML的<a>标签默认样式会覆盖shinydashboard标题的白色文字样式,以下几种方法可以帮你在保留图片的同时恢复白色文字:

方法一:直接给<a>标签设置样式

给tags$a添加style属性,指定文字颜色为白色,还可按需去掉链接下划线:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(

  dashboardHeader(titleWidth = 350,
                  title = tags$a(style = "color: white; text-decoration: none;",
                                 tags$img(src = "RStudioImage.png", height = '50',
                                          width = '50'),
                                 "Example Dashboard")),
  
  dashboardSidebar(),
  
  dashboardBody()
  
  )

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

方法二:用<span>单独包裹标题文字设置样式

如果只想针对文字调整颜色,不对<a>标签整体修改,可将标题文字放入tags$span并指定样式:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(

  dashboardHeader(titleWidth = 350,
                  title = tags$a(tags$img(src = "RStudioImage.png", height = '50',
                                          width = '50'),
                                 tags$span(style = "color: white;", "Example Dashboard"))),
  
  dashboardSidebar(),
  
  dashboardBody()
  
  )

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

方法三:替换<a>为<div>(无跳转需求时)

如果你的标题不需要跳转功能,直接把tags$a换成tags$div,默认就不会改变文字颜色:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(

  dashboardHeader(titleWidth = 350,
                  title = tags$div(tags$img(src = "RStudioImage.png", height = '50',
                                          width = '50'),
                                 "Example Dashboard")),
  
  dashboardSidebar(),
  
  dashboardBody()
  
  )

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

内容的提问来源于stack exchange,提问作者user13973103

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 01:01:13