I am learning CSS and I am fed up with positioning, can somebody tell me, why vertical navbar overlaps horizontal? I want vertical to be sticky and link elements inside to be absolute to navbar, as you see i tried with div, but still first vertical link overlaps horizontal.
This is the code:
`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>replit</title>
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="style.css" rel="stylesheet" type="text/css" /> -->
<style>
*{
box-sizing: border-box;
}
#hor{
position: static;
text-align: center;
background-color: aqua;
display: block;
}
#hor a{
float: left;
width: 20%;
position: static;
padding: 2%;
}
/* #w{
position: relative;
left: 70px;
bottom: 20px;
} */
header {
text-align: center;
padding: 5vw;
background-color: antiquewhite;
font-weight: bold;
font-family: 'Courier New', Courier, monospace;
}
#ver {
width: 10vw;
background-color: gray;
display: block;
position: relative;
}
#ver a{
display: inherit;
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
color: blue;
}
#ver a:hover{
color: red;
background-color: green;
}
</style>
</head>
<body>
<div>
<header> THIS IS HEADER</header>
<nav id="hor" style="width: 100%;">
<a href="" style="background-color: aqua;">LINK 1</a>
<a href="" style="background-color: orange;">LINK 1</a>
<a href="" style="background-color: beige;">LINK 1</a>
<a href="" style="background-color: gray;">LINK 1</a>
<a href="" style="background-color: red;">LINK 1</a>
</nav>
</div>
<div style="position: relative; width:10vw; height:10vw;">
<nav id="ver">
<a href="https://www.youtube.com" >LINK 1</a>
<a href="https://www.youtube.com" >LINK 1</a>
<a href="https://www.youtube.com" >LINK 1</a>
<a href="https://www.youtube.com" >LINK 1</a>
<a href="https://www.youtube.com" >LINK 1</a>
</nav>
</div>
</body>
</html>
How to position everything to make it vertical navbar sticky?
2
Answers
To make the vertical navbar sticky and prevent overlap with the horizontal navbar, you can adjust the CSS as follows:
Changes made:
<div>
around the vertical navbar.position: sticky;
to#ver
to make it sticky.top: 0;
to#ver
to ensure it sticks to the top of the viewport.#ver a
todisplay: block;
to make each link appear on a new line.#ver a
for better spacing.This issue are coming due to combination of absolute position and lack of proper spacing between the vertical and horizontal navigation bars.