【问题标题】:Why isn't this html div element getting centred?为什么这个 html div 元素没有居中?
【发布时间】:2019-04-19 17:35:59
【问题描述】:

我想在蓝色背景块中将顶部下拉菜单居中:

https://imgur.com/a/BfWxtes

我尝试了很多不同的方法,包括遵循本指南: https://www.w3schools.com/howto/howto_css_image_center.asp。我也尝试过添加一个 float:center 参数,但我一辈子都无法让它发挥作用。

这是我编写的代码的相关部分:(我想将“#下拉菜单的第一行”部分下的元素居中)

    html.Div([

        # First row of the dropdown menus

        html.Div([

            html.Div([
                dcc.Dropdown(
                        id="court_dd_0",
                        options=[
                            {'label': i, 'value': i} for i in sorted(list(crest['court_name'].unique()))
                            ],
                        value = 'Aylesbury Crown Court',
                        #placeholder='[Placeholder] John Doe',
                            ),
                ], style={'width': '50%', 
                        'display': 'block',
                        'margin-left': 'auto',
                        'margin-right': 'auto',
                        'textAlign': 'center'}, className='six columns'),

            ], className='row'),

        # Second row of the dropdown menus

        html.Div([

            html.Div([
                dcc.Dropdown(
                        id="offence_group_dd_2",
                        options=[
                            {'label': i, 'value': i} for i in sorted(list(df_gb_offences.offence_ho_group_desc_mso.unique()))
                            ],
                        value = None,
                        placeholder='Select breakdown by offence group',
                            ),
                ], style={'padding': 10}, className="six columns"),


            html.Div([
                dcc.Dropdown(
                        id="offence_code_dd_3",
                        options=[
                            {'label': i, 'value': i} for i in sorted(list(crest.offence_ho_code_desc_mso.unique()))
                            ],
                        #value = None,
                        placeholder='Select breakdown by offence',
                            ),
            ], style={'padding': 10}, className="six columns"),

        ], className='row'),

        # Third row of the dropdown menus

        html.Div([


            html.Div([
                    dcc.Slider(
                                id='year_slider_0',
                                min=crest['year'].min(),
                                max=crest['year'].max(),
                                value=crest['year'].max(),
                                marks={str(year): str(year) for year in crest['year'].unique()}
                                ),
                        ], className="twelve columns"), 

            ], style={'padding': 10}, className='row'),

        # End of third row of the drop down menus

        ], style={
            'textAlign': 'center',
            'margin':25,
            'backgroundColor': colors['background'],
            'width': '95%',
            'height': '100%',
            'display': 'inline-block',
            'padding': 10
            }),

【问题讨论】:

    标签: css python-3.x plotly-dash


    【解决方案1】:

    看起来您指定 offence_group_dd_2 在行中占据 6 列,这正是它看起来正在做的事情。尝试删除“六列”类名。

    【讨论】:

    • 这听起来不错。我在本地加载应用程序并运行它。如果没有 @falc 的 CSS 文件,类名什么都不会做,并且组件对我来说是正确的。
    • 啊,是的,就是这样!我花了太长时间试图让它工作。在我的脑海中,我在想,好吧,我将它设为 6 列(宽),然后将这 6 列居中放在块的中间,我没有意识到将其设置为 6 列意味着将其设置为前六列列。非常感谢。
    猜你喜欢
    • 2017-05-15
    • 2017-04-29
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 2023-01-02
    • 2018-11-19
    • 2012-11-28
    • 1970-01-01
    相关资源
    最近更新 更多