Stretch horizontal ul to fit width of div

前端 未结 4 1876
梦毁少年i
梦毁少年i 2020-12-01 01:51

For the main nav of my site, there is a 980px wide div with a ul for the main nav links. I am trying to make the nav links stretch to fit the width of the div evenly.

相关标签:
4条回答
  • 2020-12-01 02:26

    I Hope that this helps you out... Because I tried all the answers but nothing worked perfectly. So, I had to come up with a solution on my own.

    #horizontal-style {
      padding-inline-start: 0 !important; // Just in case if you find that there is an extra padding at the start of the line 
      justify-content: space-around;
      display: flex;
    }
    
    #horizontal-style a {
        text-align: center;
        color: white;
        text-decoration: none;
    }
    
    0 讨论(0)
  • 2020-12-01 02:42

    This is the easiest way to do it: http://jsfiddle.net/thirtydot/jwJBd/

    (or with table-layout: fixed for even width distribution: http://jsfiddle.net/thirtydot/jwJBd/59/)

    This won't work in IE7.

    #horizontal-style {
        display: table;
        width: 100%;
        /*table-layout: fixed;*/
    }
    #horizontal-style li {
        display: table-cell;
    }
    #horizontal-style a {
        display: block;
        border: 1px solid red;
        text-align: center;
        margin: 0 5px;
        background: #999;
    }
    

    Old answer before your edit: http://jsfiddle.net/thirtydot/DsqWr/

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

    People hate on tables for non-tabular data, but what you're asking for is exactly what tables are good at. <table width="100%">

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

    inelegant (but effective) way: use percentages

    #horizontal-style {
        width: 100%;
    }
    
    li {
        width: 20%;
    }
    

    This only works with the 5 <li> example. For more or less, modify your percentage accordingly. If you have other <li>s on your page, you can always assign these particular ones a class of "menu-li" so that only they are affected.

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