how to evenly distribute elements in a div next to each other?

后端 未结 9 1109
有刺的猬
有刺的猬 2020-12-02 07:26

This is meant for a menu.
For example I have a div element with 3 spans in it, all of which have some margin, max-width and float (left or right).
It is positioned s

相关标签:
9条回答
  • 2020-12-02 07:34

    .container {
      padding: 10px;
    }
    .parent {
      width: 100%;
      background: #7b7b7b;
      display: flex;
      justify-content: space-between;
      height: 4px;
    }
    .child {
      color: #fff;
      background: green;
      padding: 10px 10px;
      border-radius: 50%;
      position: relative;
      top: -8px;
    }
    <div class="container">
      <div class="parent">
        <span class="child"></span>
        <span class="child"></span>
        <span class="child"></span>
        <span class="child"></span>
        <span class="child"></span>
        <span class="child"></span>
        <span class="child"></span>
        <span class="child"></span>
        <span class="child"></span>
        <span class="child"></span>
      </div>
    </div>

    0 讨论(0)
  • 2020-12-02 07:49

    In the 'old days' you'd use a table and your menu items would be evenly spaced without having to explicitly state the width for the number of items.

    If it wasn't for IE 6 and 7 (if that is of concern) then you can do the same in CSS.

    <div class="demo">
        <span>Span 1</span>
        <span>Span 2</span>
        <span>Span 3</span>
    </div>
    

    CSS:

    div.demo {
        display: table;
        width: 100%;
        table-layout: fixed;    /* For cells of equal size */
    }
    div.demo span {
        display: table-cell;
        text-align: center;
    }
    

    Without having to adjust for the number of items.

    Example without table-layout:fixed - the cells are evenly distributed across the full width, but they are not necessarily of equal size since their width is determined by their contents.

    • http://jsfiddle.net/w3dx/ULQwf/

    Example with table-layout:fixed - the cells are of equal size, regardless of their contents. (Thanks to @DavidHerse in comments for this addition.)

    • http://jsfiddle.net/kqHWM/

    If you want the first and last menu elements to be left and right justified, then you can add the following CSS:

    div.demo span:first-child {
        text-align: left;
    }
    div.demo span:last-child {
        text-align: right;
    }
    
    0 讨论(0)
  • 2020-12-02 07:49

    If someone wants to try a slightly different approach, they can use FLEX.

    HTML

    <div class="test">
        <div>Div 1</div>
        <div>Div 2</div>
        <div>Div 3</div>
        <div>Div 4</div>
    </div>
    

    CSS

    .test {
        display: flex;
        flex-flow: row wrap;
        justify-content: space-around;
    }
    .test > div {
        margin-top: 10px;
        padding: 20px;
        background-color: #FF0000;
    }
    

    Here is the fiddle: http://jsfiddle.net/ynemh3c2/ (Try adding/removing divs as well)

    Here is where I learned about this: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

    0 讨论(0)
  • 2020-12-02 07:50

    This is the quick and easy way to do it

    <div>
        <span>Span 1</span>
        <span>Span 2</span>
        <span>Span 3</span>
    </div>
    

    css

    div{
        width:100%;
    }
    span{
        display:inline-block;    
        width:33%;
        text-align:center;
    }
    

    Then adjust the width of the spans for the number you have.

    Example: http://jsfiddle.net/jasongennaro/wvJxD/

    0 讨论(0)
  • 2020-12-02 07:52

    I have managed to do it with the following css combination:

    text-align: justify;
    text-align-last: justify;
    text-justify: inter-word;
    
    0 讨论(0)
  • 2020-12-02 07:53

    You just need to display the div with id #menu as flex container like this:

    #menu{
        width: 800px;
        display: flex;
        justify-content: space-between;
    }
    
    0 讨论(0)
提交回复
热议问题