Align to center child DIVs inside parent DIV

心不动则不痛 提交于 2021-01-28 18:17:33

问题


I'm trying to align a DIV in the middle of the page when the size is less than 768px

I have one parent DIV that contains three different DIVs. So I used media queries and within one display size I added this "clear:both" to the display_type and buttons classes. So in that way each div will be in its own row.

<div class="category_header container">
    <div class="products">
        <label class="header t_left">Sorter efter:</label>
        <div class="select t_left">
    </div>
    <div class="display_type">
        <ul class="clearfix">
            <li></li>
            <li></li>
        </ul>
    </div>
    <div class="buttons">
        <span class="button"></span>
        <span class="button"></span>
    </div>
    </div>
</div>

Then I tried several ways to align them in the center but they didn't work. Here what the CSS looks like:

.container {
    width: 1180px;
    margin: 0 auto;
    padding: 0;
}
.category_header {
    margin-top: 30px;
    margin-bottom: 30px;
    padding: 0 0 0 25px;
}
.products {
    float: left;
    width: 350px;
    margin: 3.5px 0 0;
}
.products label {
    margin: 0 30px 0 0;
    line-height: 40px;
    height: 40px;
    display: block;
}
.display_type {
    float: left;
    width: 350px;
    color: #A1ABB6;
    margin: 12px 0 0;
}
.category_header .buttons {
    float: right;
    width: 427px;
}

I tried to add the following to the parent div "category_header" with no luck. I also tried the same to the child divs. I found a lot of solutions on google but noone seems to work. (display: inline-block; text-align: center etc etc)

@media screen and (max-width: Number px) and (min-width: Number px) 
{
    .category_header {
        margin: 0 auto;
        width: 50%;
    }
}

Can anyone help me with this? Thanks


回答1:


try this:

@media screen and (max-width: 768px)
{
    .display_type, .buttons, .products 
    {
        float:none;
        margin:auto;
    }
}


来源:https://stackoverflow.com/questions/28987733/align-to-center-child-divs-inside-parent-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!