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

后端 未结 9 1110
有刺的猬
有刺的猬 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:54

    Make all spans used inline-block elements. Create an empty stretch span with a 100% width beneath the list of spans containing the menu items. Next make the div containing the spans text-align: justified. This would then force the inline-block elements [your menu items] to evenly distribute.

    https://jsfiddle.net/freedawirl/bh0eadzz/3/

      <div id="container">
    
              <div class="social">
                <a href="#" target="_blank" aria-label="facebook-link">
                <img src="http://placehold.it/40x40">
                </a>
                <a href="#" target="_blank" aria-label="twitter-link">
                    <img src="http://placehold.it/40x40">
                </a>
                <a href="#" target="_blank" aria-label="youtube-link">
                    <img src="http://placehold.it/40x40">
                </a>
                <a href="#" target="_blank" aria-label="pinterest-link">
                     <img src="http://placehold.it/40x40">
                </a>
                <a href="#" target="_blank" aria-label="snapchat-link">
                    <img src="http://placehold.it/40x40">
                </a>
                <a href="#" target="_blank" aria-label="blog-link">
                     <img src="http://placehold.it/40x40">
                </a>
    
                <a href="#" aria-label="phone-link">
                     <img src="http://placehold.it/40x40">
                </a>
                <span class="stretch"></span>
              </div>
                 </div>
    
    0 讨论(0)
  • 2020-12-02 07:56

    justify-content: space-betweenanddisplay: flex is all we needed, but thanks to @Pratul for the inspiration!

    0 讨论(0)
  • 2020-12-02 08:01

    You can use justify.

    This is similar to the other answers, except that the left and rightmost elements will be at the edges instead of being equally spaced - [a...b...c instead of .a..b..c.]

    <div class="menu">
        <span>1</span>
        <span>2</span>
        <span>3</span>
    </div>
    
    <style>
    .menu {text-align:justify;}
    .menu:after { content:' '; display:inline-block; width: 100%; height: 0 }
    .menu > span {display:inline-block} 
    </style>
    

    One gotcha is that you must leave spaces in between each element. [See the fiddle.]

    There are two reasons to set the menu items to inline-block:

    1. If the element is by default a block level item (such as an <li>) the display must be set to inline or inline-block to stay in the same line.
    2. If the element has more than one word (<span>click here</span>), each word will be distributed evenly when set to inline, but only the elements will be distributed when set to inline-block.

    See the JSFiddle

    EDIT:
    Now that flexbox has wide support (all non-IE, and IE 10+), there is a "better way".
    Assuming the same element structure as above, all you need is:

    <style>
        .menu { display: flex; justify-content: space-between; }
    </style>
    

    If you want the outer elements to be spaced as well, just switch space-between to space-around.
    See the JSFiddle

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