简体   繁体   中英

My html/css navigation bar hover function causes the list items to move out of place

i dont know why but i am having trouble explaining this so sorry if the title isnt what im trying to say. I have created a different kind of html/css navigation bar just for good practice, but i have come across a slight problem. As you see if you run the code below, when you hover over the nav items, the background color goes out of place, i would like it to stretch like it does in the image below, but it dosent, it overlaps! Thank you for the help! Heres the code and image! 图片

 *{margin:0; padding:0;} body{ background:black; /*background-image:url(../res/back.jpg);*/ background-size:cover; } header{ width:200px; height:600px; background-color:rgba(255, 255, 255, 0.5); border:4px solid white; border-radius:7px; margin:10px 0px 0px 360px; } header h1{ font-family:Impact; font-size:30px; color:white; font-weight:900; text-align:center; margin-top:75px; } header nav{ } header nav ul{ } header nav ul li{ list-style:none; text-align: center; margin-top:25px; position:relative; top:60px; } header nav ul li a{ font-family:Impact; font-size:25px; text-decoration: none; color:white; font-weight:900; text-align:center; } header nav ul li a:hover{ background:#ADABAB; color:black; padding:5px 70px 5px 70px; } 
 <html> <head> <meta charset="UTF-8"> <title>Home</title> <link rel="stylesheet" type="text/css" href="style/style.css"> <script type="text/javascript" src="script/jquery.js"></script> <script type="text/javascript" src="script/script.js"></script> </head> <body> <header> <h1>WELCOME</h1> <nav> <ul> <li><a href="#">HOME</a></li> <li><a href="#">GALLERY</a></li> <li><a href="#">ABOUT</a></li> <li><a href="#">PRODUCTS</a></li> <li><a href="#">CONTACT</a></li> <li><a href="#">DONATE</a></li> </ul> </nav> </header> </body> </html> 

You were very close. Let me know if this is what you wanted.

See my comments in the css

 *{margin:0; padding:0;} body{ background:black; /*background-image:url(../res/back.jpg);*/ background-size:cover; } header{ width:200px; height:600px; background-color:rgba(255, 255, 255, 0.5); border:4px solid white; border-radius:7px; margin:10px 0px 0px 360px; } header h1{ font-family:Impact; font-size:30px; color:white; font-weight:900; text-align:center; margin-top:75px; } header nav{ } header nav ul{ } header nav ul li{ list-style:none; text-align: center; margin-top:25px; position:relative; top:60px; /*added padding*/ padding:10px; } header nav ul li a{ font-family:Impact; font-size:25px; text-decoration: none; color:white; font-weight:900; text-align:center; } /* chaged from: header nav ul li a:hover to:header nav ul li:hover also removed the padding on hover */ header nav ul li:hover{ background:#ADABAB; color:black; } 
 <html> <head> <meta charset="UTF-8"> <title>Home</title> <link rel="stylesheet" type="text/css" href="style/style.css"> <script type="text/javascript" src="script/jquery.js"></script> <script type="text/javascript" src="script/script.js"></script> </head> <body> <header> <h1>WELCOME</h1> <nav> <ul> <li><a href="#">HOME</a></li> <li><a href="#">GALLERY</a></li> <li><a href="#">ABOUT</a></li> <li><a href="#">PRODUCTS</a></li> <li><a href="#">CONTACT</a></li> <li><a href="#">DONATE</a></li> </ul> </nav> </header> </body> </html> 

JSFiddle: http://jsfiddle.net/hqadcurt/

What I've done here is make sure the <a> is the same width as the list.

header nav ul li a{
    font-family:Impact;
    font-size:25px;
    text-decoration: none;
    color:white;
    font-weight:900;
    text-align:center;
    display:inline-block;
    max-width:100%;
    width:100%;
}

If you don't want that jerking around on hover just remove the padding on the hover.

header nav ul li a:hover{
    background:#ADABAB;
    color:black;
}

Use li:hover instead of li a:hover and remove the padding on it.

 * { margin: 0; padding: 0; } body { background: black; /*background-image:url(../res/back.jpg);*/ background-size: cover; } header { width: 200px; height: 600px; background-color: rgba(255, 255, 255, 0.5); border: 4px solid white; border-radius: 7px; margin: 10px 0px 0px 360px; } header h1 { font-family: Impact; font-size: 30px; color: white; font-weight: 900; text-align: center; margin-top: 75px; } header nav {} header nav ul {} header nav ul li { list-style: none; text-align: center; margin-top: 25px; position: relative; top: 60px; } header nav ul li a { font-family: Impact; font-size: 25px; text-decoration: none; color: white; font-weight: 900; text-align: center; } header nav ul li:hover { background: #ADABAB; } header nav ul li:hover a { color: black; } 
 <html> <head> <meta charset="UTF-8"> <title>Home</title> <link rel="stylesheet" type="text/css" href="style/style.css"> <script type="text/javascript" src="script/jquery.js"></script> <script type="text/javascript" src="script/script.js"></script> </head> <body> <header> <h1>WELCOME</h1> <nav> <ul> <li><a href="#">HOME</a> </li> <li><a href="#">GALLERY</a> </li> <li><a href="#">ABOUT</a> </li> <li><a href="#">PRODUCTS</a> </li> <li><a href="#">CONTACT</a> </li> <li><a href="#">DONATE</a> </li> </ul> </nav> </header> </body> </html> 

 *{margin:0; padding:0;} body{ background:black; /*background-image:url(../res/back.jpg);*/ background-size:cover; } header{ width:200px; height:600px; background-color:rgba(255, 255, 255, 0.5); border:4px solid white; border-radius:7px; margin:10px 0px 0px 360px; } header h1{ font-family:Impact; font-size:30px; color:white; font-weight:900; text-align:center; margin-top:75px; } header nav{ } header nav ul{ } header nav ul li{ list-style:none; text-align: center; margin:15px 0px; position:relative; top:60px; } header nav ul li a{ font-family:Impact; font-size:25px; text-decoration: none; color:white; font-weight:900; display:block; text-align:center; padding:5px 0px; } header nav ul li a:hover{ background:#ADABAB; color:black; } 
 <html> <head> <meta charset="UTF-8"> <title>Home</title> <link rel="stylesheet" type="text/css" href="style/style.css"> <script type="text/javascript" src="script/jquery.js"></script> <script type="text/javascript" src="script/script.js"></script> </head> <body> <header> <h1>WELCOME</h1> <nav> <ul> <li><a href="#">HOME</a></li> <li><a href="#">GALLERY</a></li> <li><a href="#">ABOUT</a></li> <li><a href="#">PRODUCTS</a></li> <li><a href="#">CONTACT</a></li> <li><a href="#">DONATE</a></li> </ul> </nav> </header> </body> </html> 

Just changed the margins and paddings at some places. You were close.

Try this:

header nav ul li a{
    font-family:Impact;
    font-size:25px;
    text-decoration: none;
    color:white;
    font-weight:900;
    text-align:center;
    padding: 5px 0px;
    display: block;
}
header nav ul li a:hover{
    background:#ADABAB;
    color:black;
}

See Jsfiddle here

 *{margin:0; padding:0;} body{ background:black; /*background-image:url(../res/back.jpg);*/ background-size:cover; } header{ width:200px; height:600px; background-color:rgba(255, 255, 255, 0.5); border:4px solid white; border-radius:7px; margin:10px 0px 0px 360px; } header h1{ font-family:Impact; font-size:30px; color:white; font-weight:900; text-align:center; margin-top:75px; } header nav{ } header nav ul{ } header nav ul li{ list-style:none; text-align: center; margin-top:25px; position:relative; top:60px; } header nav ul li a{ font-family:Impact; font-size:25px; text-decoration: none; color:white; font-weight:900; text-align:center; padding: 5px 0px; display: block; } header nav ul li a:hover{ background:#ADABAB; color:black; } 
 <html> <head> <meta charset="UTF-8"> <title>Home</title> <link rel="stylesheet" type="text/css" href="style/style.css"> <script type="text/javascript" src="script/jquery.js"></script> <script type="text/javascript" src="script/script.js"></script> </head> <body> <header> <h1>WELCOME</h1> <nav> <ul> <li><a href="#">HOME</a></li> <li><a href="#">GALLERY</a></li> <li><a href="#">ABOUT</a></li> <li><a href="#">PRODUCTS</a></li> <li><a href="#">CONTACT</a></li> <li><a href="#">DONATE</a></li> </ul> </nav> </header> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM