Is it possible to connect multiple different elements with a single event on a specific element with only CSS3?
Below is my example case; read the comment in the header selector.
css
#header {
border-color: darkred; /* change this along when .nav > li:hover {} */
border-width: 0 0 3px;
}
.nav > li {
background-color: darkred;
}
.nav > li:hover {
background-color: red;
/*border-bottom: 3px red;*/
}
html
<nav id="header" class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Example</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="{{ nav_home }}"><a href="#">Home</a></li>
</ul>
</div>
</div>
</nav>
You cannot do this in CSS alone, because it has no ancestor selectors.
However, you can fake the effect by giving #header relative positioning, and adding an empty li at the end of the list, which has the height and width of #header, but with a negative z-index.
Give the empty li these styles:
.nav > li:last-of-type {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
background-color: transparent;
}
And add this style:
.nav > li:hover ~ li:last-of-type {
background-color: red;
}
Snippet:
#header {
border-color: darkred;
border-width: 0 0 3px;
position: relative;
}
.nav > li {
background-color: darkred;
}
.nav > li:hover {
background-color: red;
}
.nav > li:hover ~ li:last-of-type {
background-color: red;
}
.nav > li:last-of-type {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
background-color: transparent;
}
<nav id="header" class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Konecranes Trainer</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="{{ nav_home }}"><a href="#">Home</a></li>
<li></li>
</ul>
</div>
</div>
</nav>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With