开发者

Nav menu that spans entire container width with constant horizontal padding

开发者 https://www.devze.com 2023-03-12 01:39 出处:网络
I\'ve created a nav menu like in the screenshot below. It spans the entire width of the container and the left/right padding of each menu item is constant. This was easy to do by hardcoding the left/r

I've created a nav menu like in the screenshot below. It spans the entire width of the container and the left/right padding of each menu item is constant. This was easy to do by hardcoding the left/right padding in the CSS, but I want the paddings to be able to change as the site admin edits the menus.

Is there 开发者_开发知识库a way to do this with pure CSS (CSS3 is okay)?

This was easy enough to do with jQuery (I totaled up the width of the menu items and calculated the necessary padding). But I ran into some issues on some browsers due to our use of Google Web Fonts. On Chrome and Firefox 4 on Windows (not on Mac), the web font was not loaded at the time that my script ran, resulting in incorrect width measurements. I tried running the script in the jQuery's DOM ready event and in the Google Font API's active event. The active event worked in Chrome but in Firefox 4 it was often fired before the font had been applied.

Thanks in advance.

Nav menu that spans entire container width with constant horizontal padding


Here's a jsfiddle of a potential different solution.

Using that layout, and assuming the number of menu items is going to change, you call a recalculation method once a list item is added/removed. In this example provided, I've used YUI3 to do the DOM manipulation, but you could do that a number of ways. Note - I didn't test the javascript function, its "probably working pseudo code".

(You may need to make subtract a further 2% or so from the list item widths, if you're trying to deal with IE6/7)


Use jQuery's .load event as suggested by user thirtydot.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号