简体   繁体   中英

How to customize the space between an HTML text and verbatimTextOutput in Shiny and Shinydashboard

This is a follow-up question of my previous question ( How to change the width and height of verbatimTextOutput in Shiny and Shinydashboard ). I want to create a verbatimTextOutput looks as the same as a textInput . Below is an example.

Now almost everything is the same except the space between the title and the text box, as the red lines indicate. I am using one line of text with the strong function to mimic the title in the textInput . Is there a way to increase the space between this line and the erbatimTextOutput , making the appearance as the same as the textInput`?

在此处输入图像描述

Code

# Load the packages
library(shiny)
library(shinydashboard)

# User Interface
ui <- dashboardPage(
  header = dashboardHeader(title = ""),
  sidebar = dashboardSidebar(
    sidebarMenu(
      menuItem(
        text = "Example",
        tabName = "tab1"
      )
    )
  ),
  body = dashboardBody(
    tabItems(
      tabItem(
        tabName = "tab1",

        tags$head(
          tags$style(
            HTML(
              "
        .form-control {
            border-radius: 4px 4px 4px 4px;
        }

        #txt1_out {
        font-family:  'Source Sans Pro','Helvetica Neue',Helvetica,Arial,sans-serif;
        font-size: 14px;
        width: 300px; 
        max-width: 100%;
        padding: 6px 12px; 
        white-space: pre-wrap;
        }

        "
            )
          ),
          tags$script("
            Shiny.addCustomMessageHandler('selectText', function(message) {
              $('#txt2_out').prop('readonly', true);
            });
            ")
        ),
        column(
          width = 4,
          textInput(inputId = "txt1", label = "Text input no.1", value = "abcde12345"),
          strong("Text output no.1"),
          verbatimTextOutput(outputId = "txt1_out", placeholder = TRUE)
        )
      )
    )
  )
)

server <- function(input, output, session){
  output$txt1_out <- renderText({
    input$txt1
  })
}

# Run the app
shinyApp(ui, server)

You can add margin-top: 7px; to:

#txt1_out {
font-family:  'Source Sans Pro','Helvetica Neue',Helvetica,Arial,sans-serif;
font-size: 14px;
width: 300px; 
max-width: 100%;
padding: 6px 12px; 
white-space: pre-wrap;
margin-top: 7px;
}

you can play around with this value (6px?). I first inspected Text input no.1 using Chrome DevTools which had:

label {
    display: inline-block;
    max-width: 100%;
    margin-bottom: 5px;
    font-weight: 700;
}

I tried to match the margin-bottom: 5px but the elements aren't identical in all respects which is why I settled on 7px.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM