【问题标题】:Layout and Dropdown menu in Dash - PythonDash 中的布局和下拉菜单 - Python
【发布时间】:2019-09-09 07:10:00
【问题描述】:

我似乎无法正确设置下拉菜单框的布局。基本上我希望下拉框位于他们匹配问题的右侧并且在同一行。

有人可以帮忙吗?

我尝试了 style={'display': 'inline-block', 'width:'X%'} 和 className = 'X columns' 的多种组合,但没有成功。

import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_auth

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
html.Div(
            [   
                html.Div(
                    [
                        html.H6("""Select your current industry""", 
                        style={'textAlign': 'right', 'font-weight':'bold', 'margin-left': '0em', 'margin-right': '2em', 'display': 'inline-block', 'width': '40%'})
                    ],      
                    ),

                dcc.Dropdown(
                            id = 'business_area_dropdown',
                            options=[
                                {'label': 'Academia', 'value': 'academia'},
                                {'label': 'Energy', 'value': 'energy'},
                                {'label': 'Research', 'value': 'research'}
                            ],
                            placeholder="Select Business Area",
                            style = dict(
                            width = '40%',
                            display = 'inline-block',
                            verticalAlign = "middle"
                            )
                        )

            ],
            className='row'
        ),

    html.Div(
            [   
                html.Div(
                    [
                        html.H6("""Are you happy where you are?""", 
                        style={'textAlign': 'right', 'font-weight':'bold', 'margin-left': '0em', 'margin-right': '2em', 'display': 'inline-block', 'width': '40%'})
                    ],      
                    ),

                dcc.Dropdown(
                            id = 'search_preference',
                            options=[
                                {'label': 'Yes', 'value': 'yes'},
                                {'label': 'No', 'value': 'no'}
                            ],
                            placeholder="Select Answer",
                            style = dict(
                            width = '40%',
                            display = 'inline-block',
                            verticalAlign = "middle"
                            )
                        )

            ],
            className='row'
        ),],

    style={'display': 'inline-block', 'backgroundColor': '#fff7dd', 'width': '99%'}

    )


if __name__ == '__main__':
    app.run_server()

下拉框显示在完全不同的行中。我希望下拉框与要回答的各自问题水平对齐。

【问题讨论】:

    标签: python html css plotly-dash


    【解决方案1】:

    我最喜欢的造型技巧 Flexbox 是我解决这个问题的方法。

    app.layout = html.Div([
        html.Div(
            [
                html.Div(
                    [
                        html.H6("""Select your current industry""",
                                style={'margin-right': '2em'})
                    ],
                ),
    
                dcc.Dropdown(
                    id='business_area_dropdown',
                    options=[
                        {'label': 'Academia', 'value': 'academia'},
                        {'label': 'Energy', 'value': 'energy'},
                        {'label': 'Research', 'value': 'research'}
                    ],
                    placeholder="Select Business Area",
                    style=dict(
                        width='40%',
                        verticalAlign="middle"
                    )
                )
            ],
            style=dict(display='flex')
        ),
    
        html.Div(
            [
                html.Div(
                    [
                        html.H6("""Are you happy where you are?""",
                                style={'margin-right': '2em'})
                    ],
                ),
    
                dcc.Dropdown(
                    id='search_preference',
                    options=[
                        {'label': 'Yes', 'value': 'yes'},
                        {'label': 'No', 'value': 'no'}
                    ],
                    placeholder="Select Answer",
                    style=dict(
                        width='40%',
                        display='inline-block',
                        verticalAlign="middle"
                    )
                )
    
            ],
            style=dict(display='flex')
        ), ],
    )
    

    这是结果的截图:

    【讨论】:

    • 谢谢,太好了!抱歉回复晚了!
    • 有没有办法实现这一点,同时保持所有下拉框的左边缘垂直对齐?
    • 是的。想到的第一种方法是将包含H6 元素的divs 的宽度设置为相同的固定宽度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多