Distribute elements evenly using CSS

后端 未结 13 736
一生所求
一生所求 2020-12-09 02:55

A method to distribute elements evenly in a container using CSS appeared on Smashing Magazine today.

I recently had to use Javascript to achieve the same effect for

相关标签:
13条回答
  • 2020-12-09 03:17

    If you use the Yahoo! User Interface Library (YUI) grids.css, it might work.

    0 讨论(0)
  • 2020-12-09 03:20

    There have been casual claims that tables are the obvious solution, however, there hasn't been any real discussion of how to implement it. I'll show you that displaying divs as a table is the right way to do this, but it is not as easy as centering all of the cells and setting an automatic width. The problem with this is that you have no control of the outer margins of the further-most left and right cell-contents. They both are inset from its containing box an arbitrary amount you cannot control. Here's a work around:

    First, a slight modification of Guder's html:

    <div id="menu">
        <ul>
            <li class="left"><a href="/">Home</a></li>
            <li><a href="/news">News</a></li>
            <li><a href="/theme">Theme</a></li>
            <li><a href="/activities">Activities</a></li>
            <li><a href="/contact">Contact</a></li>
        </ul>
    </div>
    

    Now the css:

    #menu {display:table; width:// some width in px //}
    #menu ul {display:table-row; width: 100%}
    #menu li.left {display: table-cell; text-align: left; width: 20px; white-space:nowrap;}
    #menu li {display: table-cell; text-align: right; width: auto;}
    

    Now, we have full control of the outer-most sides of the menu, which align with the far-left and far-right sides of the containing box, and the distance between each element is consistent. You'll notice that I used a trick to get the furthest left cell to be the exact-width of it's content. I set the width property to a small size obviously below what its contents would normally be. I then set the white-space to no-wrap, which stretches the cell the least amount to fit the text of the cell. You can see here an image which shows the effect of this (using different html elements):

    riffing on cats

    The beauty of this code is that it can accept however many cells and text-widths, without any knowledge of their actual widths, and distribute them evenly across the page. All the while, left and right elements reaching their perimeters, and ofcourse we have all our html in divs, no browser or internet geek is mislead to believe we're presenting tabular data. No known compromises here!

    0 讨论(0)
  • 2020-12-09 03:20

    Demo - http://codepen.io/vsync/pen/tFwxu

    all you need if to make the list itself text-align:justify and then add some pseudo item top the end of it and make it fill all the width, to trick the list into justifying all it's items across it's total width.

    0 讨论(0)
  • 2020-12-09 03:20

    Trevor Dixon's improved variant (without extra <li>)

    HTML

    <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/news">News</a></li>
        <li><a href="/theme">Theme</a></li>
        <li><a href="/activities">Activities</a></li>
        <li><a href="/contact">Contact</a></li>
    </ul>
    

    CSS

    ul {
        margin: 0;
        padding: 0;
    }
    ul li {
        display: inline-block;
        text-align: justify;
    }
    ul:after{
        display: inline-block;
        content: '';
        width: 100%;
        height: 0;
    }
    
    0 讨论(0)
  • 2020-12-09 03:22

    Of course this is exactly what the table element is for. It's sad and hilarious at the same time to see people twist themselves into a gordian knot with CSS, most of them not even knowing why they're avoiding tables.

    Whatever reason you might have dreamed up to reject tables, it can't possibly be worse than depending on Javascript to layout your page.

    Yes, I know this is not the answer you were looking for, but golly, it's so obvious.

    0 讨论(0)
  • 2020-12-09 03:24

    This is what display:table-cell is supposed to achieve - however, the IE's just don't do it, and FF<3 has problems with it too, I believe.

    These styles work in FF3, Chrome, Safari, and (I think) Opera 9:

    #menu ul {display:table;padding:0;}
    #menu li {display:table-cell;text-align:center;}
    

    But you'll need a fair few hacks to get them working in the usual, commercial set of browsers.

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