Center child divs inside parent div

前端 未结 3 952
终归单人心
终归单人心 2020-12-13 05:11

I have a parent div that must stay at 100% with 3 child divs inside. I need to center the 3 child divs, but don\'t know how.

相关标签:
3条回答
  • 2020-12-13 05:18

    Try using display: inline-block and text-align: center

    .parent {
        width: 100%;
        border: 1px solid blue;
        text-align: center;
    }
    
    .child {
        display: inline-block;  
        border: 1px solid red;
        margin: 2px;
    }
    

    jsFiddle: http://jsfiddle.net/qdHH3/3/

    0 讨论(0)
  • 2020-12-13 05:28

    Flex solution: set justify-content: center; to the parent element:

    .parent {
      display: flex;
      justify-content: center;
      /* align-items: center; /* To align vertically, if needed */
    }
    
    .parent, .child { border: 1px solid #000; }
    <div class="parent">
        <div class="child">child1</div>
        <div class="child">child2 - center us child divs! :)</div>
        <div class="child">child3</div>
    </div>

    0 讨论(0)
  • 2020-12-13 05:37
    .child {
    
        $box-size: 100%;
        
        width: $box-size;
        height: $box-size;
    
        display: flex;  
        justify-content: center;
        align-items: center;
    }
    
    0 讨论(0)
提交回复
热议问题