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

前端 未结 18 2339
猫巷女王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:18
    .processList
      text-align: center
      li
      .leftProcess
        float: left
      .centerProcess
        float: none
        display: inline-block
      .rightProcess
        float: right
    
    html
    ul.processList.clearfix
      li.leftProcess
    
    li.centerProcess
    li.rightProcess
    
    0 讨论(0)
  • 2020-11-21 21:19

    Aligning Three Divs Horizontally Using Flexbox

    Here is a CSS3 method for aligning divs horizontally inside another div.

    #container {
      display: flex;                  /* establish flex container */
      flex-direction: row;            /* default value; can be omitted */
      flex-wrap: nowrap;              /* default value; can be omitted */
      justify-content: space-between; /* switched from default (flex-start, see below) */
      background-color: lightyellow;
    }
    #container > div {
      width: 100px;
      height: 100px;
      border: 2px dashed red;
    }
    <div id="container">
      <div></div>
      <div></div>
      <div></div>
    </div>

    jsFiddle

    The justify-content property takes five values:

    • flex-start (default)
    • flex-end
    • center
    • space-between
    • space-around

    In all cases, the three divs are on the same line. For a description of each value see: https://stackoverflow.com/a/33856609/3597276


    Benefits of flexbox:

    1. minimal code; very efficient
    2. centering, both vertically and horizontally, is simple and easy
    3. equal height columns are simple and easy
    4. multiple options for aligning flex elements
    5. it's responsive
    6. unlike floats and tables, which offer limited layout capacity because they were never intended for building layouts, flexbox is a modern (CSS3) technique with a broad range of options.

    To learn more about flexbox visit:

    • Methods for Aligning Flex Items
    • Using CSS flexible boxes ~ MDN
    • A Complete Guide to Flexbox ~ CSS-Tricks
    • What the Flexbox?! ~ YouTube video tutorial

    Browser support: Flexbox is supported by all major browsers, except IE < 10. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add prefixes use Autoprefixer. More details in this answer.

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

    possible answer, if you want to keep the order of the html and not use flex.

    HTML

    <div class="a">
      <div class="c">
        the 
      </div>
      <div class="c e">
        jai ho 
      </div>
      <div class="c d">
        watsup
      </div>
    </div>
    

    CSS

    .a {
      width: 500px;
      margin: 0 auto;
      border: 1px solid red;
      position: relative;
      display: table;
    }
    
    .c {
      display: table-cell;
      width:33%;
    }
    
    .d {
      text-align: right;
    }
    
    .e {
      position: absolute;
      left: 50%;
      display: inline;
      width: auto;
      transform: translateX(-50%);
    }
    

    Code Pen Link

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

    The easiest solution is to crate a table with 3 columns and center that table.

    html:

     <div id="cont">
            <table class="aa">
                <tr>
                    <td>
                        <div id="left">
                            <h3 class="hh">Content1</h3>
                            </div>
                        </td>
                    <td>
                        <div id="center">
                            <h3 class="hh">Content2</h3>
                            </div>
                     </td>
                    <td>
                        <div id="right"><h3 class="hh">Content3</h3>
                            </div>
                    </td>
                </tr>
            </table>
        </div>
    

    css:

    #cont 
    {
      margin: 0px auto;    
      padding: 10px 10px;
    }
    
    #left
    {    
      width: 200px;
      height: 160px;
      border: 5px solid #fff;
    }
    
    #center
    {
      width: 200px;
      height: 160px;
      border: 5px solid #fff;
    }
    
    #right
    {
      width: 200px;
      height: 160px;
      border: 5px solid #fff;
    }
    
    0 讨论(0)
  • 2020-11-21 21:25

    If you do not want to change your HTML structure you can also do by adding text-align: center; to the wrapper element and a display: inline-block; to the centered element.

    #container {
        width:100%;
        text-align:center;
    }
    
    #left {
        float:left;
        width:100px;
    }
    
    #center {
        display: inline-block;
        margin:0 auto;
        width:100px;
    }
    
    #right {
        float:right;
        width:100px;
    }
    

    Live Demo: http://jsfiddle.net/CH9K8/

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

    I did another attempt to simplify this and achieve it without the necessity of a container.

    HTML

    .box1 {
      background-color: #ff0000;
      width: 200px;
      float: left;
    }
    
    .box2 {
      background-color: #00ff00;
      width: 200px;
      float: right;
    }
    
    .box3 {
      background-color: #0fffff;
      width: 200px;
      margin: 0 auto;
    }
    

    CSS

      .box1 {
      background-color: #ff0000;
      width: 200px;
      float: left;
    }
    
    .box2 {
      background-color: #00ff00;
      width: 200px;
      float: right;
    }
    
    .box3 {
      background-color: #0fffff;
      width: 200px;
      margin: 0 auto;
    }
    

    You can see it live at JSFiddle

    0 讨论(0)
提交回复
热议问题