Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Dash Common HTML Components

Dash Common HTML Components

One of Dash's core components is the HTML component, which allows you to embed standard HTML elements in your application.

This article details the commonly used HTML components in Dash.

1. Import HTML Components

In the latest version of Dash, the way to import HTML components has changed; we can directly import from dash module, import the html component, instead of importing from dash_html_components as before.



from dash import html

Common HTML Components

Component Description Example Code
html.Div Create a container (
), used to wrap other components
html.Div(children=[html.H1("标题"), html.P("这是一个段落。")])
html.H1 Create a level-1 heading (

)

html.H1("一级标题")
html.H2 Create a level-2 heading (

)

html.H2("二级标题")
html.H3 Create a level-3 heading (

)

html.H3("三级标题")
html.P Create a paragraph (

)

html.P("这是一个段落。")
html.Span Create an inline container (), used to wrap inline text or elements html.Span("这是一段行内文本。")
html.Br Create a line break (
)
html.Div(children=[html.P("第一行"), html.Br(), html.P("第二行")])
html.A Create a hyperlink () html.A("点击这里访问 Dash 官网", href="https://dash.plotly.com")
html.Img Insert an image () html.Img(src="https://plotly.com/assets/images/logo.png", height="50px")
html.Ul Create an unordered list (
    )
html.Ul(children=[html.Li("列表项1"), html.Li("列表项2")])
html.Ol Create an ordered list (
    )
html.Ol(children=[html.Li("第一项"), html.Li("第二项")])
html.Li Create a list item (
  • )
  • html.Li("列表项")
    html.Button Create a button ( html.Button("点击我", id='my-button')
    html.Label Create a label (), usually used together with input components html.Label("用户名:"), dcc.Input(id='username', type='text')
    html.Table Create a table ())
    html.Table(children=[html.Tr(children=[html.Th("姓名"), html.Th("年龄")])])
    html.Tr Create a table row (
    html.Tr(children=[html.Th("姓名"), html.Th("年龄")])
    html.Th Create a table header () html.Th("姓名")
    html.Td Create a table cell () html.Td("张三")
    html.Header Create a header (
    )
    html.Header(children=[html.H1("页眉标题")])
    html.Footer Create a footer (
    )
    html.Footer(children=[html.P("版权所有 © 2023")])
    html.Section Create a section (
    )
    html.Section(children=[html.H2("章节标题"), html.P("章节内容")])
    html.Nav Create a navigation bar ( html.Nav(children=[html.A("首页", href="/"), html.A("关于", href="/about")])
    html.Main Create the main content area (
    )
    html.Main(children=[html.H1("主要内容")])
    html.Article Create an article area (
    )
    html.Article(children=[html.H2("文章标题"), html.P("文章内容")])
    html.Aside Create a sidebar ( html.Aside(children=[html.H2("侧边栏"), html.P("侧边栏内容")])
    html.Details Create collapsible content (
    )
    html.Details(children=[html.Summary("点击展开"), html.P("隐藏内容")])
    html.Summary Create the title of collapsible content () html.Summary("点击展开")

    2. Common HTML Components

    2.1 html.Div

    html.Div is one of the most commonly used HTML components in Dash; it is used to create a div .div element is usually used to group content and can be styled via CSS.

    Example

    html.

    Div

    (

    children

    =

    [

        html.

    H1

    (

    'This is a title'

    )

    ,

        html.

    P

    (

    'This is a paragraph.'

    )

    ]

    )

    In this example,html.Div contains a heading (html.H1) and a paragraph (html.P).

    2.2 html.H1 to html.H6

    html.H1 to html.H6 is used to create headings of different levels.H1 is the highest-level heading,H6 is the lowest-level heading.

    Example

    html.

    H1

    (

    'This is an H1 Heading'

    )

    ,

    html.

    H2

    (

    'This is an H2 Heading'

    )

    ,

    html.

    H3

    (

    'This is an H3 Heading'

    )

    ,

    html.

    H4

    (

    'This is an H4 Heading'

    )

    ,

    html.

    H5

    (

    'This is an H5 Heading'

    )

    ,

    html.

    H6

    (

    'This is an H6 Heading'

    )

    2.3 html.P

    html.P is used to create a paragraph (

    element).

    Example

    html.

    P

    (

    'This is a paragraph.'

    )

    2.4 html.A

    html.A is used to create a hyperlink ( element). You can specify href attribute to specify the target URL of the link.

    Example

    html.

    A

    (

    'Click here to visit the Dash official website'

    ,

    href

    =

    'https://dash.plotly.com/'

    )

    2.5 html.Img

    html.Img is used to insert an image in the page ( element). You need to specify the src attribute to specify the image's URL.

    Example

    html.

    Img

    (

    src

    =

    'https://dash.plotly.com/assets/images/logo.png'

    ,

    alt

    =

    'Dash Logo'

    )

    2.6 html.Ul and html.Li

    html.Ul is used to create an unordered list (