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

如何修改Shiny中HTML()内文本字号?有无更佳列表生成方案?

解决Shiny中HTML列表字号修改与更优实现方案

一、修改HTML元素字号的方法

1. 内联样式(快速调整单个元素)

直接给目标HTML标签添加style属性指定字号,适合针对性调整:

text <- HTML(
    "
    <h1 style='font-size:30px;'>Title</h1>
    <p style='font-size:16px;'>Some text goes here</p>
    <br/>
    <ul style='font-size:14px;'>
    <li>
    <h3 style='font-size:20px;'>Another title</h3>
    <p>Some text goes here</p>
    </li>
    <li>
    Some other text goes here
    </li>
    </ul>
    "
)

注:ul标签设置字号后,内部li文本会自动继承,无需重复设置。

2. 内部样式表(批量统一设置)

如果需要统一调整一类元素的字号,在HTML中嵌入<style>块,更便于后续维护:

text <- HTML(
    "
    <style>
        h1 {font-size:30px;}
        p {font-size:16px;}
        ul, li {font-size:14px;}
        h3 {font-size:20px;}
    </style>
    <h1>Title</h1>
    <p>Some text goes here</p>
    <br/>
    <ul>
    <li>
    <h3>Another title</h3>
    <p>Some text goes here</p>
    </li>
    <li>
    Some other text goes here
    </li>
    </ul>
    "
)

二、更优的项目符号列表生成方式

1. 使用Shiny原生tags函数

用shiny::tags系列函数构建DOM元素,完全用R语法编写,可读性和可维护性更强,也方便动态修改:

text <- tags$div(
    tags$h1("Title", style = "font-size:30px;"),
    tags$p("Some text goes here", style = "font-size:16px;"),
    tags$br(),
    tags$ul(
        style = "font-size:14px;",
        tags$li(
            tags$h3("Another title", style = "font-size:20px;"),
            tags$p("Some text goes here")
        ),
        tags$li("Some other text goes here")
    )
)

如果需要动态生成列表项(比如从向量批量创建),可以结合lapply实现:

item_list <- c("First item", "Second item", "Third item")
dynamic_ul <- tags$ul(lapply(item_list, tags$li))

2. 使用Markdown语法

如果内容是静态的,直接编写Markdown文本,通过markdownToHTML转换后传入UI,写法更简洁:

md_content <- "
# Title
Some text goes here

- **Another title**  
  Some text goes here
- Some other text goes here
"
text <- HTML(markdown::markdownToHTML(text = md_content, fragment.only = TRUE))

若内容较多,还可以用shiny::includeMarkdown加载外部.md文件,更适合管理长文本内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 12:27:13