css - Shiny 的页脚位置

标签 css r shiny css-position footer

我想在 Shiny 的应用程序中调整页脚位置。当页面内容比视口(viewport)短时,页脚应该在视口(viewport)的底部。当页面内容比视口(viewport)长时,页脚应该在内容下方。 This post 建议如何在 CSS 中实现它。在手动编写页面的 HTML 代码时,这种解决方案和类似的解决方案通常很容易使用。
有关于 Shiny 的页脚位置的讨论,其中一些设法将页脚移动到底部。但是,它们无法防止页脚与页面主要内容的底部重叠,这需要缩短主要内容的容器。
考虑以下最小工作示例:

library(shiny)

ui <- navbarPage(title = "Some Example", id = "example",
    tabPanel(title = "Something", value = "something",
        textOutput("some_text")
    ),
    footer = "The footer."
)

server <- function(input, output, session) {
    output$some_text <- renderText(stringi::stri_rand_lipsum(5))
}

shinyApp(ui = ui, server = server)

最佳答案

可以像您描述的那样拥有页脚,但实现起来并不简单。似乎没有内置函数来定位页脚,更不用说以您想要的方式了。
所以,我们需要编写一些自定义的 CSS。为此,我们需要能够定位页脚。当我们查看示例生成的 HTML 时,可以看到参数 footer 指定的内容。简单地包裹在 <div> 中标签类 row .

      <div class="container-fluid">
        <div class="tab-content" data-tabsetid="6611">
          <div class="tab-pane active" data-value="something" id="tab-6611-1">
            <div id="some_text" class="shiny-text-output"></div>
          </div>
        </div>
        <div class="row">The footer.</div>
      </div>
    </body>
    </html>
在任何大小合理的 Shiny 应用程序中,都会有多个 <div> s 与此类,这使得编写可靠地仅针对页脚的 CSS 变得困难。解决方法如下:
    ui <- tagList(navbarPage(
      title = "Some Example",
      id = "example",
      tabPanel(
        title = "Something",
        value = "something",
        textOutput("some_text")
      )),
      tags$footer("The footer.", class = "footer")
    )
现在剩下要做的就是添加定位页脚的 CSS。我使用在 Bootstrap website 上找到的示例.将它集成到 Shiny 的一种方法是这样的:
    ui <- tagList(
      tags$head(
        tags$style(HTML(
          "html {
             position: relative;
             min-height: 100%;
           }
           body {
             margin-bottom: 60px; /* Margin bottom by footer height */
           }
           .footer {
             position: absolute;
             bottom: 0;
             width: 100%;
             height: 60px; /* Set the fixed height of the footer here */
             background-color: #f5f5f5;
           }"))),
      navbarPage(
      title = "Some Example",
      id = "example",
      tabPanel(
        title = "Something",
        value = "something",
        textOutput("some_text")
      )),
      tags$footer("The footer.", class = "footer")
    )
将示例中的 UI 替换为上面的 UI 将生成所需的页脚,当内容很短时,该页脚会粘在视口(viewport)的底部,但当内容长于视口(viewport)时,它会位于内容下方。

关于css - Shiny 的页脚位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67763901/

相关文章:

r - 大小为 K 的整数分区

r - 通过划分两列来创建新的数据列,确定第三列中的除数/除数

html - CSS::after 选择器在我的菜单中换行并移动文本

javascript - 隐藏内容时如何保留页面高度?

css - JSF 中的 RES_NOT_FOUND

r - 这是什么类型的图表?它可以使用ggplot2创建吗?

r - if语句去掉: Error in if: argument is of length zero

R 传单 Shiny : shape_click$id is NULL

r - Shinyfiles 和 renderUI 不能正常工作

css - 在不超出父 div tailwindcss 的情况下 float 右键