Reputation: 603
Let's say I have a 1000px page width, my header contains 10 horizontal nav elements using justify-content: space-between
.
If I hover nav 1
link, an absolute menu div will appear width a specific width and left: 0
, if I hover nav 2
, another menu div will appear still with left: 0
. But when I hover nav 7
or nav 8
, how can I automatically make the menu get aligned right (right: 0
)?
here is a code snippet of my html & css:
<nav>
<ul>
<li>
<a href="#">Nav menu1</a>
<div class="menu">Content menu 1</div>
</li>
<li>
<a href="#">Nav menu2</a>
<div class="menu">Content menu 2</div>
</li>
<li>
<a href="#">Nav menu3</a>
<div class="menu">Content menu 3</div>
</li>
<li>
<a href="#">Nav menu4</a>
<div class="menu">Content menu 4</div>
</li>
<li>
<a href="#">Nav menu5</a>
<div class="menu">Content menu 5</div>
</li>
<li>
<a href="#">Nav menu6</a>
<div class="menu">Content menu 6</div>
</li>
<li>
<a href="#">Nav menu7</a>
<div class="menu">Content menu 7</div>
</li>
<li>
<a href="#">Nav menu8</a>
<div class="menu">Content menu 8</div>
</li>
<li>
<a href="#">Nav menu9</a>
<div class="menu">Content menu 9</div>
</li>
<li>
<a href="#">Nav menu10</a>
<div class="menu">Content menu 10</div>
</li>
</ul>
</nav>
ul {
display: flex;
font-size: 15px;
height: 100%;
border: solid 1px #ccc;
max-width: 1200px;
margin: 0 auto;
padding: 24px;
align-items: center;
justify-content: space-between;
}
ul > li {
display: flex;
position: relative;
cursor: pointer;
padding-left: 6px;
padding-right: 6px;
height: 40px;
align-items: center;
white-space: nowrap;
list-style: none;
}
.menu {
display: block;
position: absolute;
visibility: hidden;
color: #333;
width: 500px;
background: #fff;
padding: 18px;
min-height: 80px;
box-shadow: 0px 2px 1px -1px rgba(0,0,0,0.2), 0px 1px 1px 0px rgba(0,0,0,0.14), 0px 1px 3px 0px rgba(0,0,0,0.12);
opacity: 0;
transition: all .4s ease;
transition-delay: .3s;
left: 0;
top: 140%;
z-index: 99999;
}
ul > li:hover .menu {
display: block;
visibility: visible;
opacity: 1;
top: 100%;
}
Upvotes: 0
Views: 5061
Reputation: 1567
Your question is missing a lot of information including the code you are working with. It seems you may be looking for the nth child pseudo class to define a range.
The following is generic as you have not provided an HTML sample and will affect the 6th to 10th children.
CSS:
li:nth-child(n+6):nth-child(-n+10){
right:0;
}
Edit: Updated answer now that the question is clearer...
See the below example with added (vanilla) Javascript.
<!doctype html>
<html>
<head>
<style>
ul{
display:flex;
font-size:15px;
height:100%;
border:solid 1px #ccc;
max-width:1200px;
margin:0 auto;
padding:24px;
align-items:center;
justify-content:space-between;
}
ul > li{
display:flex;
position:relative;
cursor:pointer;
padding-left:6px;
padding-right:6px;
height:40px;
align-items:center;
white-space:nowrap;
list-style:none;
}
.menu{
display:block;
position:absolute;
visibility:hidden;
color:#333;
width:500px;
background:#fff;
padding:18px;
min-height:80px;
box-shadow:0px 2px 1px -1px rgba(0,0,0,0.2), 0px 1px 1px 0px rgba(0,0,0,0.14), 0px 1px 3px 0px rgba(0,0,0,0.12);
opacity:0;
transition:all .4s ease;
transition-delay:.3s;
top:140%;
}
ul > li:hover .menu{
display:block;
visibility:visible;
opacity:1;
top:100%;
}
</style>
</head>
<body>
<nav>
<ul>
<li>
<a href="#">Nav menu1</a>
<div class="menu">Content menu 1</div>
</li><li>
<a href="#">Nav menu2</a>
<div class="menu">Content menu 2</div>
</li><li>
<a href="#">Nav menu3</a>
<div class="menu">Content menu 3</div>
</li><li>
<a href="#">Nav menu4</a>
<div class="menu">Content menu 4</div>
</li><li>
<a href="#">Nav menu5</a>
<div class="menu">Content menu 5</div>
</li><li>
<a href="#">Nav menu6</a>
<div class="menu">Content menu 6</div>
</li><li>
<a href="#">Nav menu7</a>
<div class="menu">Content menu 7</div>
</li><li>
<a href="#">Nav menu8</a>
<div class="menu">Content menu 8</div>
</li><li>
<a href="#">Nav menu9</a>
<div class="menu">Content menu 9</div>
</li><li>
<a href="#">Nav menu10</a>
<div class="menu">Content menu 10</div>
</li>
</ul>
</nav>
<script>
var elems=document.querySelectorAll('div.menu'); //Get NodeList of elements
elems.forEach(function(elem){ //Iterate
var pos=elem.getBoundingClientRect(); //Get element info
let stat=document.elementFromPoint(pos.right,0); //Check position
if(stat===null){ //Will be null if out of viewport
elem.style.right='0'; //Add the right:0; style
}else{
elem.style.left='0'; //Add the left:0; style
}});
</script>
</body>
</html>
Edit 2: As requested, just change the JavaScript to check against the container.
As it is now this will match against the first <ul>
found in the document. You will probably want to <ul id="cont">
and match with the id instead:
<script>
//var cont=document.getElementById('cont'); //Match by ID
var cont=document.querySelector('ul'); //Match first <ul>
var posc=cont.getBoundingClientRect(); //Get container info
var elems=document.querySelectorAll('div.menu'); //Get NodeList of elements
elems.forEach(function(elem){ //Iterate
var posv=elem.getBoundingClientRect(); //Get element info
var offset=posc.right-posv.right; //Calculate difference
if(offset<0){ //Will be <0 if out of container
elem.style.right='0'; //Add the right:0; style
}else{
elem.style.left='0'; //Add the left:0; style
}});
</script>
Upvotes: 4