Dropdown list alignment issue (HTML/CSS)

非 Y 不嫁゛ 提交于 2019-12-04 06:57:27

问题


I've asked a question like this before, but I am using differnt code to last time.

I'm trying to create a dropdown menu. Ther are certain elements in that main list that have a dropdown list (News and Team). For some reason, they are moved over to the right. What I would like is for the items in the dropdown to be aligned with its parent.

Any help would be appreciated.

Thanks

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);
}

回答1:


Some element on the web page have standard padding values. For example all lists have padding-left. If you want to change this try this:

Add this in your CSS code:

ul {
   padding: 0;
}

Or you can add come specific id or class for this menu, and change padding for them.




回答2:


You just need to add padding: 0 to your nav ul ul rule so it appears like so:

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

By default, ul elements have a left padding of 40px, you need to remove that padding to make the second level ul align with the first.




回答3:


I just want to say a big thank you for all your answers. I don't know why it's taken so long, but I've finally figured it out.

In the codepen I displayed, there was one important thing I was leaving off: The CSS file for the Mobile-Responsive framework known as Foundation. That CSS File had all ul tags have a margin-left of 1.25em. That is why the ul tag was moved over to the right.

To fix this problem, I changed the following:

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

I added a margin property and now it is all fine.

Again, thank you to everyone who helped me with this answer and sorry for wasting your time.

I wish you all well.



来源:https://stackoverflow.com/questions/35382139/dropdown-list-alignment-issue-html-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!