Dash Css
When developing interactive Web applications, Dash not only allows you to use Python to build complex applications, but also provides rich styling options to make your applications look more professional and beautiful.
In Dash, styling is implemented through CSS. Dash provides multiple ways to set component styles, including:
* **Inline Styles**: Set component styles directly through the `style` attribute.
* **External CSS Files**: Set global styles by loading external CSS files.
* **Dash Bootstrap Components**: Use Bootstrap-style components and styles.
* **Dash DAQ**: Style components for creating data acquisition and control applications.
* * *
## 1. Inline Styles
You can directly set CSS styles for components through the style attribute. style is a dictionary where keys are CSS properties and values are CSS values.
## Example
from dash import Dash, html
# Create Dash app
app = Dash( __name__ )
# Define layout
app.layout= html.Div(
style={
'backgroundColor': 'lightblue',# Background color
'padding': '20px',# Padding
'borderRadius': '10px',# Border radius
'textAlign': 'center'# Text alignment
},
children=[
html.H1("Welcome to Dash", style={'color': 'darkblue'}),
html.P("This is a styled Div.")
]
)
# Run app
if __name__ =='__main__':
app.run_server(debug=True)
**Running Effect:**
* Background color is light blue, padding is 20px, border radius is 10px, text is centered.
* Title text color is dark blue.
* * *
## 2. External CSS Files
By loading external CSS files, you can set global styles for the entire application.
1. Create a CSS file (e.g., `assets/styles.css`).
2. Load the CSS file in the Dash application.
CSS File Example (assets/styles.css):
## Example: assets/styles.css file
/* Set global font */
body {
font-family: Arial,sans-serif;
}
/* Set heading style */
h1 {
color: darkblue;
text-align:center;
}
/* Set Div style */
.custom-div{
background-color: lightblue;
padding:20px;
border-radius:10px;
text-align:center;
}
Dash using css file:
## Example
from dash import Dash, html
# Create Dash app
app = Dash( __name__ )
# Define layout
app.layout= html.Div(
className='custom-div',# Use CSS class name
children=[
html.H1("Welcome to Dash"),
html.P("This is a styled Div.")
]
)
# Run app
if __name__ =='__main__':
app.run_server(debug=True)
**Running Effect:**
* The application loads styles from `assets/styles.css`.
* `h1` heading color is dark blue, centered.
* `Div` background color is light blue, with padding and border radius.
* * *
## 3. Dash Bootstrap Components
Dash Bootstrap Components (dash-bootstrap-components) is a third-party library that provides Bootstrap-style components and styles. It helps quickly build beautiful Dash applications.
pip install dash-bootstrap-components
## Example
from dash import Dash, html
import dash_bootstrap_components as dbc
# Create Dash app
app = Dash( __name__ , external_stylesheets=[dbc.themes.BOOTSTRAP])
# Define layout
app.layout= dbc.Container(
children=[
dbc.Row(
dbc.Col(
html.H1("Welcome to Dash Bootstrap", className="text-center text-primary")
)
),
dbc.Row(
dbc.Col(
dbc.Card(
children=[
dbc.CardHeader("Card Title"),
dbc.CardBody(
children=[
html.P("This is a Bootstrap-style card.", className="card-text")
]
)
],
className="mt-4"# Set margin
)
)
)
]
)
# Run app
if __name__ =='__main__':
app.run_server(debug=True)
**Running Effect:**
* Uses Bootstrap theme styles.
* Title is centered, color is theme blue.
* Card component has title and content, with beautiful styling.
* * *
## 4. Dash DAQ
Dash DAQ is a component library for creating data acquisition and control applications, providing rich style components.
pip install dash-daq
## Example
from dash import Dash, html
import dash_daq as daq
# Create Dash app
app = Dash( __name__ )
# Define layout
app.layout= html.Div(
style={'textAlign': 'center'},
children=[
daq.Thermometer(
id='thermometer',
value=25,
min=0,
max=100,
label="Thermometer",
style={'margin': '20px'}
),
daq.Gauge(
id='gauge',
value=50,
min=0,
max=100,
label="Pressure Gauge",
style={'margin': '20px'}
)
]
)
# Run app
if __name__ =='
YouTip