未加星标

How can I center a graphic under a <ul>-based nav menu?

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

I have a simple, basic CSS drop-down menu and I want to center an arrow graphic under the selected item. Ideally, completely with CSS. I thought about doing this with the :after selector, but I'm already using that to draw dividing lines between the menu options:

HTML: <ul> <li>Option Zero</li> <li>Option One Is Longer</li> <li class='selected'>Option Two <ul class='sub-menu'> <li>Sub One</li> </ul> </li> <li>Option Three Is Not the Best</li> </ul> <div class='arrowholder'> <div class='arrow'></div> </div> CSS: * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } ul { list - style: none; width: 100 % ; background: red; height: 50px; padding: 10px 10px 0 10px; margin: 0px; } li { float: left; margin - top: 10px; } li: after { content: "|"; margin: 0 8px; }.sub - menu { display: none; } .arrowholder { clear: both; width: 100 % ; background: green; height: 50px; } .arrow { width: 50px; height: 50px; background: url(http: //upload.wikimedia.org/wikipedia/en/1/1c/Up_Arrow_Icon.png) no-repeat;border:solid 1px white; }

JSFiddle: http://jsfiddle.net/pnoeric/MrW58/2/

I've already sorted out a jQuery-based solution (you'll see it on the fiddle) but for some reason, it's not working cross-browser for me... the position() method is returning different values in Chrome vs Firefox so it's not reliable.

UPDATE:grrr, my jQuery-based solution works perfectly when the Chrome javascript Console isn't open. When that's open, the jQuery position() method gets confused and returns weird values. Sooo... I have a solution, the jQuery-based solution does it, I just have to close the developer console.

Ideally I'm looking for a solution that is 100% CSS, and barring that, a little bit of supporting jQuery. How can I accomplish this?

Problem courtesy of: Eric

Solution

You could use a pseudo class on the .selected li that holds that arrow. Example:

.selected:after { content:''; position:absolute; width:50px; height:50px; background:url(http://upload.wikimedia.org/wikipedia/en/1/1c/Up_Arrow_Icon.png) no-repeat; top:30px; left:50%; margin-left:-25px; border:solid 1px white; }

Just be sure to give your list item position:relative.

Here's a demo: http://jsfiddle.net/9u2J6/4/

Solution courtesy of: mikowl

Discussion

Why not simply apply a background-image to each li, use background-position:bottom center; to position the arrow, background-repeat:no-repeat; and then use padding-bottom so the content doesn't overlap the arrow.

Discussion courtesy of: jono

It turns out my jQuery-based solution works perfectly... as long as Chrome's JavaScript Console panel isn't open. When that's open, the position() method gets confused and it stops functioning. Super frustrating to discover that (so much time wasted) but glad I found it.

Here's the solution... it just manually moves the arrow graphic over to where it needs to be to center it.

$(function() { p = $('.current').position(); w = $('.current').width(); naw = $('.arrow').width(); f = Math.floor(p.left + (w/2) - (naw/2))-20; $('.arrow').css('margin-left', f+'px'); });

Discussion courtesy of: Eric

This recipe can be found in it's original form on Stack Over Flow .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:How can I center a graphic under a &lt;ul&gt;-based nav menu?
本站链接:https://www.codesec.net/view/628365.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(14)