【问题标题】:Dropdown list alignment issue (HTML/CSS)下拉列表对齐问题 (HTML/CSS)
【发布时间】:2016-02-13 16:12:11
【问题描述】:

我以前问过这样的问题,但我使用的代码与上次不同。

我正在尝试创建一个下拉菜单。该主列表中的某些元素具有下拉列表(新闻和团队)。由于某种原因,它们被移到了右边。我希望下拉列表中的项目与其父项对齐。

任何帮助将不胜感激。

谢谢

http://codepen.io/DocRow10/pen/hjIkq

<body>
    <div id="container">
        <div id="banner" class="clearfix">

                    <img id="crest" src="images/cecc-logo.png" />
                    <h1>Test Website</h1>
        </div>
        <nav class="clearfix">
            <ul class="clearfix">
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">News</a>
                    <ul>
                        <li><a href="#">Social Events</a></li>
                    </ul>
                </li>
                <li><a href="#">Team</a>
                    <ul>
                        <li><a href="#">Players</a></li>
                        <li><a href="#">Fixtures/Results</a></li>
                        <li><a href="#">Statistics</a></li>
                    </ul>
                </li>
                <li><a href="#">Gallery</a></li>
                <li><a href="#">Contact</a></li>  
            </ul>
            <a href="#" id="pull">Menu</a>
        </nav>
        <main>

        </main>
        <footer>

        </footer>
    </div>

</body>

    body {
  background-color: rgb(200, 220, 255);
/* #455868 */

}

#container {
    height: 1000px;
    margin-left: auto;
    margin-right: auto;
}

#banner {
    width: 100%;
    text-align: center;

}

#crest {
    height: 150px;
    width: 180px;
    display: inline-block;
}

#banner h1 {

    display: inline-block;
}
/* Bat Colour rgb(38, 124, 196); */


@media only screen and (min-width : 480px) {


    #banner h1 {
        font-size: 30px;
        display: inline-block;
    }
}

@media only screen and (min-width : 768px) {
    #banner h1 {
        font-size: 36px;
        display: inline-block;
    }
}
@media only screen and (min-width : 980px) {
    #banner h1 {
        font-size: 47px;
        display: inline-block;
    }
}

nav {
    height: 40px;
    width: 90%;
    margin-left: auto;
    margin-right: auto;
    background-color: rgb(238, 0, 0);
    font-size: 13pt;
    font-family: neris;
    border-bottom: 2px solid #283744;
}

nav > ul {
    padding: 0;
    margin: 0 auto;
    width: 600px;
    height: 40px;
}

nav > ul > li {
    display: inline;
    float: left;
}

nav ul a {
    color: #fff;
    display: inline-block;
    width: 100px;
    text-align: center;
    text-decoration: none;
    line-height: 40px;
    text-shadow: 1px 1px 0px #283744;
}

nav li a {
    border-right: 1px solid #576979;
    box-sizing:border-box;
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
}
nav li:last-child a {
    border-right: 0;
}

nav a:hover, nav a:active {
    background-color: #8c99a4;
}

nav a#pull {
    display: none;
} 

nav ul li:hover ul {
    display: block;
}

nav ul ul {
    display: none;
    position: absolute;
}

nav ul ul li {
    display: block;
}

main {
    width: 90%;
    height: 200px;
    margin-right: auto;
    margin-left: auto;
    background-color: rgb(38, 124, 196);
}

footer {
    width: 90%;
    height: 50px;
    margin-right: auto;
    margin-left: auto;
    background-color: rgb(0, 0, 0);
}

【问题讨论】:

    标签: html css drop-down-menu html-lists


    【解决方案1】:

    网页上的某些元素具有标准的padding 值。 例如,所有列表都有padding-left。如果你想改变这个,试试这个:

    在你的 CSS 代码中添加这个:

    ul {
       padding: 0;
    }
    

    或者您可以为此菜单添加特定的idclass,并为它们更改填充。

    【讨论】:

    • 你能给我一个 codepen/jsfiddle 的例子吗?我无法让它工作。
    • 如果你能展示一个活生生的例子,那将不胜感激。
    【解决方案2】:

    您只需将padding: 0 添加到您的nav ul ul 规则中,使其如下所示:

    nav ul ul {
        display: none;
        position: absolute;
        padding: 0;
    }
    

    默认情况下,ul 元素的左内边距为 40px,您需要移除该内边距以使第二级 ul 与第一级对齐。

    【讨论】:

    • 标记了你的答案,因为(间接地)你帮助了我,而一分钱就下降了,为什么我会遇到这个问题。检查我的答案,你会明白我的意思。谢谢!
    【解决方案3】:

    我只想对您的所有回答表示非常感谢。我不知道为什么花了这么长时间,但我终于弄明白了。

    在我展示的 codepen 中,我遗漏了一件重要的事情:Mobile-Responsive 框架的 CSS 文件,称为 Foundation。该 CSS 文件的所有 ul 标签的左侧边距为 1.25em。这就是为什么 ul 标签被移到了右边。

    为了解决这个问题,我更改了以下内容:

    nav ul ul {
        display: none;
        position: absolute;
        padding: 0;
        margin: 0;
    }
    

    我添加了一个边距属性,现在一切正常。

    再次感谢所有帮助我回答这个问题的人,很抱歉浪费了您的时间。

    祝你一切顺利。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 2011-11-07
      相关资源
      最近更新 更多