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

如何在R Shiny悬浮提示框中缩进项目符号文本?

解决R Shiny悬浮提示框项目符号缩进问题

问题原因

你当前通过手动添加空格来缩进项目符号,这种方式依赖空格的渲染宽度,在不同浏览器或字体环境下容易出现对齐混乱,且代码维护性差。

解决方案:使用HTML列表+CSS微调

最可靠的方式是用语义化的HTML无序列表(<ul>/<li>)组织提示内容,再通过CSS调整缩进和间距,确保跨环境的一致性。

修改后完整代码

library(shiny)

ui <- fluidPage(
  tags$head(
    tags$style(HTML("
      .tooltip-inner {
        text-align: left !important;
      }
      /* 调整列表的缩进和边距 */
      .tooltip-inner ul {
        margin: 0;
        padding-left: 20px;
      }
      /* 可选:增加列表项之间的间距 */
      .tooltip-inner li {
        margin-bottom: 4px;
      }
    "))
  ),
  
  div(
    style = "text-align: center; margin-bottom: 10px;",
    h3("Dynamic Curve Generator"),
    div(
      style = "text-align: center; margin-bottom: 10px;",
      span(
        "Instructions",
        id = "instruction_label"
      ),
      tags$script(HTML("
        $(document).ready(function() {
          $('#instruction_label').attr('title', 
            `<ul>
              <li><b>Drag</b> blue control point bubbles to shape a curve</li>
              <li><b>Right-click</b> the curve to add more points for finer shaping</li>
            </ul>`
          ).tooltip({
            html: true,
            placement: 'bottom'
          });
        });
      "))
    )
  )
)

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

shinyApp(ui, server)

关键改动说明

  1. HTML结构优化:将原来的手动项目符号和换行,替换成<ul>包裹的<li>列表项,浏览器会自动处理项目符号的缩进逻辑。
  2. CSS微调:通过.tooltip-inner ul设置padding-left控制缩进幅度,同时清除默认margin避免多余空白;可选的.tooltip-inner li设置margin-bottom优化行间距。

这种方式不仅能实现整齐的缩进,还能让提示内容结构更清晰,后续维护或添加新条目也更方便。

内容的提问来源于stack exchange,提问作者Village.Idyot

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 08:48:23