How to align 3 divs (left/center/right) inside another div?

前端 未结 18 2367
猫巷女王i
猫巷女王i 2020-11-21 20:48

I want to have 3 divs aligned inside a container div, something like this:

[[LEFT]       [CENTER]        [RIGHT]]

Container div is 100% wid

相关标签:
18条回答
  • 2020-11-21 21:26

    You can try this:

    Your html code like this:

    <div id="container">
      <div id="left"></div>
      <div id="right"></div>
      <div id="center"></div>
    </div>
    

    and your css code like this:

    #container{width:100%;}
    #left{float:left;width:100px;}
    #right{float:right;width:100px;}
    #center{margin:0 auto;width:100px;}
    

    so, it's output should be get like this:

    [[LEFT]       [CENTER]        [RIGHT]]
    
    0 讨论(0)
  • 2020-11-21 21:27

    HTML:

    <div id="container" class="blog-pager">
       <div id="left">Left</div>
       <div id="right">Right</div>
       <div id="center">Center</div>    
    </div>
    

    CSS:

     #container{width:98%; }
     #left{float:left;}
     #center{text-align:center;}
     #right{float:right;}
    

    text-align:center; gives perfect centre align.

    JSFiddle Demo

    0 讨论(0)
  • 2020-11-21 21:28

    This can be easily done using the CSS3 Flexbox, a feature which will be used in the future(When <IE9 is completely dead) by almost every browser.

    Check the Browser Compatibility Table

    HTML

    <div class="container">
      <div class="left">
        Left
      </div>
      <div class="center">
        Center
      </div>
      <div class="right">
        Right
      </div>
    </div>
    

    CSS

    .container {
      display: flex;
      flex-flow: row nowrap; /* Align on the same line */
      justify-content: space-between; /* Equal margin between the child elements */
    }
    

    Output:

    .container {
      display: flex;
      flex-flow: row nowrap; /* Align on the same line */
      justify-content: space-between; /* Equal margin between the child elements */
    }
    
    /* For Presentation, not needed */
    
    .container > div {
      background: #5F85DB;
      padding: 5px;
      color: #fff;
      font-weight: bold;
      font-family: Tahoma;
    }
    <div class="container">
      <div class="left">
        Left
      </div>
      <div class="center">
        Center
      </div>
      <div class="right">
        Right
      </div>
    </div>

    0 讨论(0)
  • 2020-11-21 21:28

    Here are the changes that I had to make to the accepted answer when I did this with an image as the centre element:

    1. Make sure the image is enclosed within a div (#center in this case). If it isn't, you'll have to set display to block, and it seems to centre relative to the space between the floated elements.
    2. Make sure to set the size of both the image and its container:

      #center {
          margin: 0 auto;
      }
      
      #center, #center > img {
          width: 100px;
          height: auto;
      }
      
    0 讨论(0)
  • 2020-11-21 21:30

    There are several tricks available for aligning the elements.

    01. Using Table Trick

    .container{
      display:table;
     }
    
    .left{
      background:green;
      display:table-cell;
      width:33.33vw;
    }
    
    .center{
      background:gold;
      display:table-cell;
      width:33.33vw;
    }
    
    .right{
      background:gray;
      display:table-cell;
      width:33.33vw;
    }
    <div class="container">
      <div class="left">
        Left
      </div>
      <div class="center">
        Center
      </div>
      <div class="right">
        Right
      </div>
    </div>

    02. Using Flex Trick

    .container{
      display:flex;
      justify-content: center;
      align-items: center;
       }
    
    .left{
      background:green;
      width:33.33vw;
    }
    
    .center{
      background:gold;
       width:33.33vw;
    }
    
    .right{
      background:gray;
       width:33.33vw;
    }
    <div class="container">
      <div class="left">
        Left
      </div>
      <div class="center">
        Center
      </div>
      <div class="right">
        Right
      </div>
    </div>

    03. Using Float Trick

    .left{
      background:green;
      width:100px;
      float:left;
    }
    
    .center{
       background:gold;
       width:100px;
       float:left;
    }
    
    .right{
       background:gray;
       width:100px;
       float:left;
    }
    <div class="container">
      <div class="left">
        Left
      </div>
      <div class="center">
        Center
      </div>
      <div class="right">
        Right
      </div>
    </div>

    0 讨论(0)
  • 2020-11-21 21:30

    I like my bars tight and dynamic. This is for CSS 3 & HTML 5

    1. First, setting the Width to 100px is limiting. Don't do it.

    2. Second, setting the container's width to 100% will work ok, until were talking about it being a header/footer bar for the whole app, like a navigation or credits/copyright bar. Use right: 0; instead for that scenario.

    3. You are using id's (hash #container, #left, etc) instead of classes (.container, .left, etc), which is fine, unless you want to repeat your style pattern elsewhere in your code. I'd consider using classes instead.

    4. For HTML, no need to swap order for: left, center, & right. display: inline-block; fixes this, returning your code to something cleaner and logically in order again.

    5. Lastly, you need to clear the floats all up so that it doesn't mess with future <div>. You do this with the clear: both;

    To summarize:

    HTML:

    <div class="container">
      <div class="left"></div>
      <div class="center"></div>
      <div class="right"></div>
      <div class="clear"></div>
    </div>
    

    CSS:

    .container {right: 0; text-align: center;}
    
    .container .left, .container .center, .container .right { display: inline-block; }
    
    .container .left { float: left; }
    .container .center { margin: 0 auto; }
    .container .right { float: right; }
    .clear { clear: both; }
    

    Bonus point if using HAML and SASS ;)

    HAML:

    .container
      .left
      .center
      .right
      .clear
    

    SASS:

    .container {
      right: 0;
      text-align: center;
    
      .left, .center, .right { display: inline-block; }
    
      .left { float: left; }
      .center { margin: 0 auto; }
      .right { float: right; }
      .clear { clear: both; }
    }
    
    0 讨论(0)
提交回复
热议问题