Distribute elements evenly using CSS

后端 未结 13 877
一生所求
一生所求 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:29

    Thanks to the CSS3 Flexbox module, this is possible with two lines of CSS.

    Check the Browser compatibility table for Flexbox

    HTML

    
    

    CSS

    ul {
      display: flex;
    }
    li {
      flex: 1; /* Short hand for flex-grow: 1 and flex-shrink: 1 */
    }
    

    Output:

    ul, li {
      margin: 0;
      padding: 0;
    }
    ul {
      display: flex;
      list-style: none;
    }
    li {
      flex: 1;
      text-align: center;
    }

提交回复
热议问题