【问题标题】:How to center a CSS element under another in Shiny如何在闪亮的另一个 CSS 元素下居中
【发布时间】:2020-07-27 01:00:42
【问题描述】:

我正在设计一个闪亮的应用程序,并且有一个绝对面板,我希望它具有仪表板式的外观。

基本上,我希望在仪表板中显示一些按钮的文本描述,这些按钮显示在文本描述下方的中心。我目前使用 CSS 中的绝对位置控件来确定面板内的位置。虽然这目前有效,但我认为这不是一种特别可扩展的方法。

我附上了一些可重现的代码,以便您了解我在说什么。

非常感谢您的帮助。

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  absolutePanel(
    id = "quick-glance", style="z-index:500; opacity: 0.90",
    class = "panel panel-default",
    draggable=TRUE,
    fixed=TRUE,
    width = 180,
    height = 75,
    top = 20, left = 50,

    textOutput("label1"), tags$head(tags$style("#label1{
                                               color: steelblue;
                                               font-weight: bold; 
                                               border: none;
                                               position: absolute; top: 5px; left: 30px;
                                               ")),
    actionBttn("bttn1",label = "$##",style = "bordered",color = "primary"),
    tags$head(tags$style("#bttn1{ position: absolute; bottom: 5px; left: 20px;}")),

    textOutput("label2"),tags$head(tags$style("#label2{
                                              color: steelblue;
                                              font-weight: bold; 
                                              border: none;
                                              position: absolute; top: 5px; left: 100px;
                                              ")),
    actionBttn("bttn2",label = "#",style = "bordered",color = "danger"),
    tags$head(tags$style("#bttn2{ position: absolute; bottom: 5px; left: 110px;}"))


    )
    )

server <- function(input, output, session) {
  output$bttn1 = renderPrint(input$bttn1)
  output$label1 = renderText("Text")

  output$label2 = renderText("Text Text")
  output$bttn2 = renderPrint(input$bttn2)

}

shinyApp(ui, server)

【问题讨论】:

    标签: html css r shiny dashboard


    【解决方案1】:

    最好将您想要分组的内容包装在 column() 中,这会创建一个 div,然后您可以居中。然后,在每个这样的div 中,您可以将文本和按钮居中。

    library(shiny)
    library(shinyWidgets)
    
    ui <- fluidPage(
      absolutePanel(
        id = "quick-glance", style="z-index:500; opacity: 0.90",
        class = "panel panel-default",
        draggable=TRUE,
        fixed=TRUE,
        width = 180,
        height = 75,
        top = 20, left = 50,
        fluidRow(
          column(
            textOutput("label1"), 
            actionBttn("bttn1",label = "$##",style = "bordered",color = "primary"),
            width = 1
          ),
          column(
            textOutput("label2"), 
            actionBttn("bttn2",label = "#",style = "bordered",color = "danger"),
            width = 1
          )
        ),
        tags$head(tags$style(".col-sm-1 {width: 50%; margin: 0px;}
                              #label1, #bttn1, #label2, #bttn2 {margin: 0 auto; width: 100%; text-align: center}"))
      )
    )
    
    server <- function(input, output, session) {
      output$bttn1 = renderPrint(input$bttn1)
      output$label1 = renderText("Text")
    
      output$label2 = renderText("Text Text")
      output$bttn2 = renderPrint(input$bttn2)
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 感谢您的帮助!一个问题,我想我没有预见到:所以操作按钮中的一个值比其他按钮中的任何其他值长得多。使用上面的解决方案,这意味着所有操作按钮都有一个大边框,有很多空白(因为按钮都是相同的大小)。有没有办法根据内容的长度自动调整按钮边框的大小?谢谢!
    • 我认为如果您删除 CSS 中的 width: 100%;,按钮宽度将是其内容的大小。但是,如果您这样做,它可能不再居中。我不知道如何解决这个问题,你可以搜索一些 CSS 文档来解决这个问题。
    • 你是对的,它不再保持居中。我会做一些阅读!不管怎样,谢谢你的帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 2014-09-19
    • 2023-03-03
    • 2021-12-06
    • 2017-05-13
    相关资源
    最近更新 更多