CSS center content inside div

后端 未结 7 898
离开以前
离开以前 2020-12-23 13:15

I need to center html content inside a div class=\"partners\" (top div with 2 images). As you can see from the image below (it floats left instead of center of the div):

相关标签:
7条回答
  • 2020-12-23 13:33

    You just do CSS changes for parent div

    .parent-div { 
            text-align: center;
            display: block;
    }
    
    0 讨论(0)
  • 2020-12-23 13:39

    You just need

    .parent-div { text-align: center }
    
    0 讨论(0)
  • 2020-12-23 13:49

    do like this :

    child{
        position:absolute;
        margin: auto;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
    }
    
    0 讨论(0)
  • 2020-12-23 13:51

    To center a div, set it's width to some value and add margin: auto.

    #partners .wrap {
        width: 655px;
        margin: auto;
    }
    

    EDIT, you want to center the div contents, not the div itself. You need to change display property of h2, ul and li to inline, and remove the float: left.

    #partners li, ul, h2 {
        display: inline;
        float: none;
    }
    

    Then, they will be layed out like normal text elements, and aligned according to text-align property of their container, which is what you want.

    0 讨论(0)
  • 2020-12-23 13:52

    There are many ways to center any element. I listed some

    1. Set it's width to some value and add margin: 0 auto.

    .partners {
        width: 80%;
        margin: 0 auto;
    }


    1. Split into 3 column layout

    .partners {
        width: 80%;
        margin-left: 10%;
    }


    1. Use bootstrap layout

    <div class="row">
        <div class="col-sm-4"></div>
        <div class="col-sm-4">Your Content / Image here</div>
    </div>

    0 讨论(0)
  • 2020-12-23 13:52

    The problem is that you assigned a fixed width to your .wrap DIV. The DIV itself is centered (you can see that when you add a border to it) but the DIV is just too wide. In other words the content does not fill the whole width of the DIV.

    To solve the problem you have to make sure, that the .wrap DIV is only as wide as it's content.

    To achieve that you have to remove the floating in the content elements and set the display property of the block levels elements to inline:

    #partners .wrap {
     display: inline;
    } 
    
    .wrap { margin: 0 auto; position: relative;}
    
    #partners h2 {
    color: #A6A5A5;
    font-weight: normal;
    margin: 2px 15px 0 0;
    display: inline;
    }
    
    #partners ul {
    display: inline;
    }
    
    #partners li {display: inline}
    
    ul { list-style-position: outside; list-style-type: none; }
    
    0 讨论(0)
提交回复
热议问题