Fluid width menu using CSS

2019-08-01 15:45发布

I have a horizontal CSS menu. The problem is when the user creates more than 5 menu items the li tabs move onto a new line. I dont want this to happen.

How can I make it fluid width so if the user creates just 3 menu items then each tab increases in width so it fits full width?

HTML:

<div class="container">
 <div id="new-menu-lower">
  <ul class="menuul">
   <li class="menuli">
       <a href="/test.aspx">Test</a>
   </li>
      <li class="menuli">
       <a href="/test.aspx">Test</a>
   </li>
      <li class="menuli">
       <a href="/test.aspx">Test</a>
   </li>
      <li class="menuli">
       <a href="/test.aspx">Test</a>
   </li>
      <li class="menuli">
       <a href="/test.aspx">Test</a>
   </li>
      <li class="menuli">
       <a href="/test.aspx">Test</a>
   </li>
      <li class="menuli">
       <a href="/test.aspx">Test</a>
   </li>

  </ul>
 </div>
</div>

CSS:

div.container
{
    clear: both;
    margin: 10px auto 0;
    width:960px;
    border:1px solid red;
    height:700px;
}

div#new-menu-lower {
    border-radius: 3px 3px 3px 3px;
    border-top: 0 solid lightgrey;
    margin: 0 8px 10px;
}

#new-menu-lower ul {
    border: 1px solid white;
    display: block;
    height: 27px;
}

#new-menu-lower ul li:first-child {
    border-left: 0 solid lightgrey !important;
    border-radius: 7px 0 0 7px;
}

#new-menu-lower ul li {
    background-image: url("http://i45.tinypic.com/16if95z.png");
    background-repeat: repeat;
    border-right: 0 solid lightgrey !important;
    float: left;
    height: 27px;
    padding-left: 10px;
    padding-right: 10px;
    text-align: center;
    width: 168px;
}

JSFIDDLE EXAMPLE: http://jsfiddle.net/rM9MW/

7条回答
Luminary・发光体
2楼-- · 2019-08-01 16:04

Live demo http://jsfiddle.net/rM9MW/29/

Hey now you can used display:table-cell properties in your css as like this

and update your css and change to color according to your layout

div.container
{
    margin: 10px auto 0;
    width:960px;
    border:1px solid red;
    height:700px;
    background:red;
}

div#new-menu-lower {
    border-radius: 3px 3px 3px 3px;
    border-top: 0 solid lightgrey;
    margin: 0 8px 10px;
    background:green;
}

#new-menu-lower ul {
    border: 10px solid yellow;
}

#new-menu-lower ul li:first-child {
    border-left: 0 solid lightgrey !important;
    border-radius: 7px 0 0 7px;
}

#new-menu-lower ul li{
display:table-cell;
    padding-left: 10px;
    padding-right: 10px;
}
#new-menu-lower ul li a{
    background: url("http://i45.tinypic.com/16if95z.png");

    border-right: 0 solid lightgrey !important;
    height: 27px;
    padding-left: 10px;
    padding-right: 10px;
    text-align: center;
    display:block;
    width: 168px;
}

Live demo http://jsfiddle.net/rM9MW/29/


Updated demo http://jsfiddle.net/rM9MW/39/

查看更多
三岁会撩人
3楼-- · 2019-08-01 16:14

You don't jeed javascript!

demo jsBin

Just change this lines in your CSS:

#new-menu-lower ul {

/*display: block; //////////////////REMOVE//////////////////// */
display:table; /*  //////////////////ADD//////////////////// */

and:

#new-menu-lower ul li {

/* float: left;      //////////////////REMOVE//////////////////// */
display:table-cell; /* //////////////////ADD//////////////////// */
查看更多
狗以群分
4楼-- · 2019-08-01 16:16

Using the following Jquery you can achieve your goal;

JQUERY

$ulWidth = $('.menuul').width();
$listLength = $('.menuli').length;
$('.menuli').css('width', $ulWidth / $listLength )

See this demo: http://jsfiddle.net/rathoreahsan/nxurR/

查看更多
Deceive 欺骗
5楼-- · 2019-08-01 16:22

I created an example that (I think) works:

http://jsfiddle.net/ybmGr/2/

It depends how much text is inside of the list element. It won't work for X elements. You just can't go unlimited X with this.

EDIT: Well It seems that undefined posted the solution that works for as many elements you want...

查看更多
淡お忘
6楼-- · 2019-08-01 16:25

like this?

var mw = $('.menuul').width();
var ll = $('.menuli').length;
var c = mw / ll;
$('.menuli').css('width', c)

http://jsfiddle.net/rM9MW/8/

查看更多
该账号已被封号
7楼-- · 2019-08-01 16:26

I came from the future, for future viewers :)

You can use a display: flex; for the ul, and a flex: 1; for menu items, I think it's a very good solution for recent browsers.

ul{display: flex;}
li{flex: 1;} /* modify witdh of item */
查看更多
登录 后发表回答