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

如何在R Shiny的column组件中部放置actionButton按钮

Shiny column组件内按钮居中实现方案

问题背景

需要将actionButton放置在指定宽度的column组件水平中部,先后尝试两种方案均未生效。

已尝试的失效方案

方案1:内联样式嵌套div

column(
  width = 6,
  div(
    style="display:inline-block",
    actionButton(
      "nextpg",
      span("Next Page", style="size:100px"),
      width = 250,
      height = 50
      )
    )
  )

失效原因:仅给嵌套div设置inline-block属性,没有添加水平居中的相关规则,无法实现内部按钮居中。

方案2:自定义CSS类实现

ui.R代码

column(
  width = 6,
  div(
    class="nxtBtn",
    actionButton(
      "nextpg",
      span("Next Page", style="size:100px"),
      width = 250,
      height = 50
      )
    )
  )

custom.CSS代码

.nxtBtn {
  position: center;
  display: inline-block;
  align-items: center;
}

失效原因:CSS中position属性没有center取值,align-items属于Flex布局属性,仅在display: flex时生效,当前规则组合无法实现居中效果。

可行解决方案

直接使用column组件原生的align参数即可实现内部元素水平居中,代码如下:

column(
  width = 6,
  align = "center",
  actionButton(
    "nextpg",
    span("Next Page", style="size:100px"),
    width = 250,
    height = 50
  )
)

该方案不需要额外嵌套dom元素或自定义CSS,语法简洁,适配Shiny原生组件规则。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 02:15:04