You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Plotly中让下拉菜单标题与下拉列表横向并排而非上下排列?

如何在Plotly Dash中实现下拉菜单标题与下拉列表横向并排展示?

默认情况下标题与下拉菜单会以上下排列的方式展示,要实现横向并排的效果,可通过以下方式修改代码:

当前使用代码

app.layout = dbc.Container([
dbc.Row([
    dbc.Col(html.H1("Testing",
                    className = 'text-center text-primary, mb-4 '
                    ,style={"textDecoration":"underline",'font-weight': 'bold'}),
            width=12
            ),
  
    ]),
html.Br(),
html.Br(),

dbc.Row([
   dbc.Col([
       html.H3('Product'
                ,style={'font-size': '25px'}
               ),
        
        
       dcc.Dropdown(id='product_dd', value= None,
                      options = [{'label':x, 'value':x} 
                                for x in product_cat],
                      
                      searchable = True, search_value='',
                      placeholder= 'Please select ...',
                      clearable=True
                      ),
       html.Br(),
       
       ], width=3, md=4),
])

当前展示效果

当前上下排列效果

预期展示效果

预期横向并排效果

解决方案

方法:嵌套Bootstrap Row与Col组件

利用Dash Bootstrap Components的dbc.Row和dbc.Col,将标题与下拉菜单放在同一行的不同列中,并设置垂直对齐,确保视觉上的协调:

修改后完整代码

app.layout = dbc.Container([
dbc.Row([
    dbc.Col(html.H1("Testing",
                    className = 'text-center text-primary, mb-4 '
                    ,style={"textDecoration":"underline",'font-weight': 'bold'}),
            width=12
            ),
  
    ]),
html.Br(),
html.Br(),

dbc.Row([
   dbc.Col([
       # 用Row包裹标题和下拉,实现横向排列
       dbc.Row([
           # 标题列:自适应宽度,避免占用过多空间
           dbc.Col(html.H3('Product', style={'font-size': '25px'}), width='auto'),
           # 下拉菜单列:占据剩余空间
           dbc.Col(dcc.Dropdown(id='product_dd', value= None,
                          options = [{'label':x, 'value':x} 
                                    for x in product_cat],
                          searchable = True, search_value='',
                          placeholder= 'Please select ...',
                          clearable=True
                          ), width=True)
       ], align="center"),  # 让行内元素垂直居中
       html.Br(),
       
       ], width=3, md=4),
])

内容的提问来源于stack exchange,提问作者beginofwork

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 10:50:36