【问题标题】:Centering a ul submenu居中 ul 子菜单
【发布时间】:2012-04-11 00:08:28
【问题描述】:

我已经在这个子菜单上工作了 8 个小时,我想不出的最后一件事是如何将 li 项(或 ul 项)放在子菜单中的中心。可能这张图有帮助:

我需要将蓝色箭头下的所有 li 项目居中。这是我的 ul ul 和 ul ul li 的代码。如果您需要更多信息、所有提示提示和 ofc,请发表评论。感谢您的回答:)

 /*
 *
 * Menu
 *
 */
#access {
    display: block;
    width: 46.4em;
    float: right;
    height: 7.3em;
    line-height: 7.3em;
    margin: 0 1.5em 0 0;
}
#access ul {
    list-style: none;
    margin: 0;
    padding-left: 0;
    text-align: center;
}
#access li {
    float: left;
    padding: 0 2em;
}
#access a {
    position: relative;
    height: 73px;
    font-size: 1.4em;
    display: block;
    line-height: 5.071428571em;
    font-family: Georgia, Times New Roman, serif;
    text-transform: uppercase;
    color: #009cda;
}
.current-menu-item {
    background: url("images/arrow.png") no-repeat;
    background-position: 50% 100%;
}
.current-cat-menu-item  a {
    text-decoration: underline;
}
ul li .current-menu-item{
    background: none;
}
.current-menu-item.parent-item  a:hover {
    background: url("images/arrow-blue.png") no-repeat;
    background-position: 50% 229%;
}
#access ul ul {
    display: none;
    top: 100%;
    z-index: 998;
    position: absolute;
    height: 36px;
    text-align: center;
}
#access ul ul li{
    display: inline;
    list-style: none;
}
#access ul ul a {
    color: #fff;
    font-size: 12px;
    line-height: 3em;
    height: auto;
    z-index: 1000;
    text-transform: none;
    font-family: Arial, sans-serif;
}
#access li:hover > a,
#access ul ul :hover > a {
    text-decoration: underline;
}
#access .parent-item:hover > a  {
    background: url("images/arrow-blue.png") no-repeat;
    background-position: 50% 229%;
    z-index: 999;
}
#access ul ul :hover > a {
        background: none;
}
#access ul li:hover > ul {
    display: block;
}
.sub-menu-wrapper {
    display: none;
    width: 100%;
    height: 36px;
    top: 71px;
    left: 0;
    position: absolute;
    background: url("images/blue.png") repeat-x bottom;
    z-index: 997;
    /* #009CDA;
    -webkit-box-shadow: inset 0px -1px 5px 0px rgba(0, 0, 0, 0.3);
    -moz-box-shadow: inset 0px -1px 5px 0px rgba(0, 0, 0, 0.3);
    box-shadow: inset 0px -1px 5px 0px rgba(0, 0, 0, 0.3);  */
}
#access ul li:hover > span {
    display: block;
}

这是我的 html 结构:

<nav class="menu-topp-meny-container">
    <ul id="menu-topp-meny" class="menu">
        <li id="menu-item-116" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-116">
            <a href="http://test3.clydemarketing.com/om-monster-2/">Om Monster</a>
        </li>
        <li id="menu-item-225" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-171 current_page_item current-menu-ancestor current-menu-parent current_page_parent current_page_ancestor menu-item-225 parent-item">
            <a href="http://test3.clydemarketing.com/entertainment/om-oss/">Avdelinger</a>
            <ul class="sub-menu">
                <li id="menu-item-254" class="menu-item menu-item-type-post_type menu-item-object-page current-page-ancestor current-page-parent menu-item-254">
                    <a href="http://test3.clydemarketing.com/entertainment/">Entertainment</a>
                </li>
                <li id="menu-item-260" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-260">
                    <a href="http://test3.clydemarketing.com/entertainment/kontakt/">Kontakt</a>
                </li>
                <li id="menu-item-261" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-261">
                    <a href="http://test3.clydemarketing.com/entertainment/arkiv/">Arkiv</a>
                </li>
                <li id="menu-item-262" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-171 current_page_item menu-item-262">
                    <a href="http://test3.clydemarketing.com/entertainment/om-oss/">Om Oss</a>
                </li>
            </ul>
        <span class="sub-menu-wrapper"></span>
        </li>
        <li id="menu-item-26" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-26">
            <a href="http://test3.clydemarketing.com/kontakt/">Kontakt</a>
        </li>
        <li id="menu-item-25" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-25">
            <a href="http://test3.clydemarketing.com/jobb/">Jobb</a>
        </li>
    </ul>
</nav>

【问题讨论】:

  • 这只是 jQuery 或 javascript 的一种方式。粘贴您的 HTML 结构。我可以写一些代码。
  • 谢谢 :) 我现在添加了菜单 html 结构。
  • @OleHenrikSkogstrøm 您的 css 根本没有说明您的菜单规则。
  • 是的,在小 css 中添加了一点,我现在用我的完整菜单 css 代码更新了这个问题:)

标签: html css html-lists centering


【解决方案1】:

计算您拥有的所有li 项目的总宽度并将其添加到您的ul 的css:

var total = 0;
$('li').each(function(){
    total += $(this).width();
});
$('ul').css({
    'width': total + 'px',
    'margin': '0px auto'
});

【讨论】:

  • 听起来不错的解决方案,我对 jQuery 没有经验,你有如何做到这一点的例子吗?
  • 您可能希望在此处使用选择器,因为简单使用 $('li') 将选择所有列表项并显示它们的宽度。
  • 不错!是的,我可以通过每个 .sub-menu 函数并在每个循环结束时输出 ul 的宽度来解决这个问题
【解决方案2】:

在 css 中尝试这样的事情:

#nav_row li#menu-item li.menu-item 
{
padding-bottom:10px !important;
float:none !important;
text-align:center;
}

【讨论】:

    猜你喜欢
    • 2014-12-05
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2021-09-21
    • 1970-01-01
    • 2017-04-22
    相关资源
    最近更新 更多