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.
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 (
element), while html.Li is used to create a list item ( element).
Example
html.
Ul
(
children
=
[
html.
Li
(
'List Item 1'
)
,
html.
Li
(
'List Item 2'
)
,
html.
Li
(
'List Item 3'
)
]
)
2.7 html.Ol
html.Ol is used to create an ordered list ( element).
Example
html.
Ol
(
children
=
[
html.
Li
(
'First Item'
)
,
html.
Li
(
'Second Item'
)
,
html.
Li
(
'Third Item'
)
]
)
2.8 html.Br
html.Br is used to insert a line break in the page ( element).
Example
html.
P
(
'This is the first line.'
)
,
html.
Br
(
)
,
html.
P
(
'This is the second line.'
)
2.9 html.Hr
html.Hr is used to insert a horizontal rule in the page ( element).
Example
html.
P
(
'This is the content above.'
)
,
html.
Hr
(
)
,
html.
P
(
'This is the content below.'
)
2.10 html.Span
html.Span is used to create an inline container ( element), usually used to style part of a text.
Example
html.
P
(
'This is a paragraph containing '
,
html.
Span
(
'highlighted'
,
style
=
{
'color'
:
'red'
}
)
,
' text.'
)
3. Using Styles
Dash's HTML components support setting CSS styles via the style attribute.style The attribute accepts a dictionary where the keys are CSS properties and the values are the corresponding CSS values.
Example
html.
Div
(
children
=
"This is a styled Div"
,
style
=
{
'color'
:
'white'
,
'backgroundColor'
:
'blue'
,
'padding'
:
'10px'
,
'borderRadius'
:
'5px'
}
)
This code uses Dash's html.Div component to create a styled div element, resulting in a div with a blue background, white text, rounded corners, and padding.
html.Div: create a
container.
children: set the div The content is "这是一个带样式的 Div".
style: set via CSS styles div 's appearance:
color: white: the text color is white.
backgroundColor: blue: the background color is blue.
padding: 10px: the padding is 10 pixels.
borderRadius: 5px: the border radius is 5 pixels.
-
color: white: the text color is white.
-
backgroundColor: blue: the background color is blue.
-
padding: 10px: the padding is 10 pixels.
-
borderRadius: 5px: the border radius is 5 pixels.
Example
The following is a complete Dash application example showing how to use html.Div and other HTML components, and set styles via the style attribute.
Example
from
dash
import
Dash
,
html
Create the Dash App
app
=
Dash
(
name
)
Define the application's layout
app.
layout
=
html.
Div
(
style
=
{
'fontFamily'
:
'Arial, sans-serif'
,
Set global font
'padding'
:
'20px'
,
Set global padding
'maxWidth'
:
'800px'
,
Set maximum width
'margin'
:
'0 auto'
Center the display
}
,
children
=
[
Title
html.
H1
(
"Dash Example Application"
,
style
=
{
'color'
:
'white'
,
'backgroundColor'
:
'darkblue'
,
'padding'
:
'10px'
,
'borderRadius'
:
'5px'
,
'textAlign'
:
'center'
Text Centering
}
)
,
Paragraph
html.
P
(
"This is a simple Dash application example showing how to use HTML components and styles."