【问题标题】:How to vertically center action buttons in a shiny app?如何在闪亮的应用程序中垂直居中操作按钮?
【发布时间】:2020-05-21 19:06:59
【问题描述】:

我有两个按钮在我的列中水平居中,但还无法弄清楚如何垂直居中。我尝试使用“vertical-align-middle”。下面是我的 UI 代码:

ui <- shinyUI(fluidPage(
  tags$style(
    HTML('
         #buttons {
         background-color:yellow; position:fixed; margin-bottom:50px; vertical-align: middle; opacity:1; height:50px; z-index:5;
         }

         #fluidrow1 {
        height:50px;
         }

         ')
    ),
    fluidRow(id="fluidrow1",
      column(12, align="center", id="buttons",
             actionButton("test1", "Test1"),
             actionButton("rmd2", "RMD2")
      )
    )
  ,uiOutput("uioutput")

))

当我尝试单独执行第一个按钮时,使用以下代码:

 #test1 {
        height:50px; vertical-align: middle;
         }

我最终得到了一个看起来非常奇怪的 UI,两个按钮都居中,但一个很大并且是整个列 div 的高度。

【问题讨论】:

    标签: html css r shiny


    【解决方案1】:

    您可以这样做:

     display: flex;
     align-items: center;
     justify-content: center;
    

    而不是vertical-align: middle;

    所以它会是这样的:

    library(shiny)
    
    ui <- shinyUI(fluidPage(
      tags$style(
        HTML('
             #buttons {
             background-color:yellow; position:fixed; margin-bottom:50px; opacity:1; height:50px; z-index:5;
             display: flex;
             align-items: center;
             justify-content: center;
             }
    
             #fluidrow1 {
            height:50px;
             }
    
             ')
      ),
      fluidRow(id="fluidrow1",
               column(12, align="center", id="buttons",
                      actionButton("test1", "Test1"),
                      actionButton("rmd2", "RMD2")
               )
      )
      ,uiOutput("uioutput")
    
    ))
    
    
    
    server <- function(input, output, session) {
    
    }
    
    
    shinyApp(ui, server)
    

    【讨论】:

    • Mka,您能否快速解释一下为什么需要 display 和 justify-content 属性?感谢您的工作回答!
    • @Nova 很高兴它有效! display: flex 创建一个弹性容器。 justify-content 控制项目的水平对齐,align-item 定义垂直对齐。这个网站有一些有助于理解的视觉效果! css-tricks.com/snippets/css/a-guide-to-flexbox
    猜你喜欢
    • 2019-05-02
    • 2016-01-27
    • 2021-10-06
    • 2023-04-09
    • 1970-01-01
    • 2017-12-12
    • 2020-09-05
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多