CSS: Fixed with horizontal menu, with variable width tabs, using ul

前端 未结 5 524
春和景丽
春和景丽 2020-12-15 09:40

I have a horizontal menu. The markup looks like this:

  • Item 1
  • Longer Item 2
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 10:39

    If you're happy using JavaScript, jQuery could solve the issue as follows

    var $menu, totalwidth;
    
    $menu = $('ul.menu');
    totalwidth = ($menu.width()/100);
    
    $('ul.menu li').each(function(){
        this.css('width',String(Math.floor(this.width()/totalwidth))+'%');
    });
    
    $menu.css('width','100%');
    

    That's untested, but looks right to me. Comment if you've any questions at all.

提交回复
热议问题