I'm trying to replicate the navigation bar from this website and can't work out how to do the following two things:
Create the page background overlay on hover of the submenu
How to make the submenu have the same expand reveal on hover of the link.
body {
font-family: acumin-pro, sans-serif;
font-size: 16px;
letter-spacing: .25px;
margin: 0;
}
a {
text-decoration: none;
}
.main {
width: 100%;
}
.section {
width: 100%;
background: #ededed;
}
.header {
display: flex;
width: 100%;
}
.nav {
width: 1100px;
margin: auto;
position: relative;
padding: 0 40;
}
.nav li {
color: #000;
}
.nav li a {
cursor: pointer
}
.nav ul {
list-style: none;
padding: 0;
margin-bottom: 0;
width: fit-content;
}
.nav ul:hover li {
color: #eee;
padding-bottom: 20px;
}
.nav ul li:hover {
color: #333;
padding-bottom: 20px;
}
.nav li:last-child {
margin: 0;
}
.nav ul li {
display: inline-block;
padding: 0 35px 20px 0;
}
.three:hover>.sub-menu {
display: flex;
opacity: 1;
height: 200px;
}
.sub-menu {
height: 0px;
display: flex;
flex-direction: row;
position: absolute;
top: 100%;
background: #2E2E2E;
display: none;
opacity: 0;
left: 0;
right: 0;
}
.col-1-4 {
height: 100%;
width: 25%;
position: relative;
}
.service {
padding: 20%;
}
.nav.servicesIsHovered {
background-color: #272727;
}
<div class="header">
<div class="nav">
<ul>
<li>
<a class="one">Home</a>
</li>
<li>
<a class="two">About</a>
</li>
<li class="three">
<a class="">Services</a>
<div class="sub-menu">
<div class="col-1-4"></div>
<div class="col-1-4"></div>
<div class="col-1-4"></div>
<div class="col-1-4"></div>
</div>
</li>
<li>
<a class="four">Contact</a>
</li>
</ul>
</div>
</div>
I previously tried getting the overlay to work by adding a separate div to the end of the code and setting the opacity to zero, I then added more code to change the opacity to one, however, this failed.
What would be the best practise to achieve the two aspects above?
The sub-menu is done using absolute positioning and making all the ancestors up until the container not have any positioning (or default static positioning), so the sub-menu is positioned absolutely to the container.
The expand reveal is done using a height animation - the sub-menu has a fixed height that is animated on hover of the parent li
The overlay is done using fixed positioning and javascript - whenever you see inline styles change like that, you know it's js animation and not css.
Below I have added jQuery and shown a quick demo of how they may have done it
var $overlay = $('.overlay');
$('.sub-menu').parent()
.on('mouseenter', function() {
$overlay.fadeIn('fast');
})
.on('mouseleave', function() {
$overlay.fadeOut('fast');
});
body {
font-family: acumin-pro, sans-serif;
font-size: 16px;
letter-spacing: .25px;
margin: 0;
}
a {
text-decoration: none;
}
.main {
width: 100%;
}
.section {
width: 100%;
background: #ededed;
}
.header {
z-index: 2;
position: relative;
display: flex;
width: 100%;
background:blue; /* needs a colour so you cannot see overlay behind */
}
.nav {
width: 1100px;
margin: auto;
position: relative;
padding: 0 40;
}
.nav li {
color: #000;
}
.nav li a {
cursor: pointer
}
.nav ul {
list-style: none;
padding: 0;
margin-bottom: 0;
width: fit-content;
}
.nav ul:hover li {
color: #eee;
padding-bottom: 20px;
}
.nav ul li:hover {
color: #333;
padding-bottom: 20px;
}
.nav li:last-child {
margin: 0;
}
.nav ul li {
display: inline-block;
padding: 0 35px 20px 0;
}
li:hover>.sub-menu {
height: 200px;
}
.sub-menu {
height: 0px;
overflow: hidden;
/* hide the overflow so you don't need to worry about display and opacity */
display: flex;
flex-direction: row;
position: absolute;
top: 100%;
background: #2E2E2E;
left: 0;
right: 0;
transition: height 1s ease;
/* add this for your animation and remove display none */
}
.col-1-4 {
height: 100%;
width: 25%;
position: relative;
}
.service {
padding: 20%;
}
.nav.servicesIsHovered {
background-color: #272727;
}
.overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 1;
display:none;
/* above the body but below the header */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="header">
<div class="nav">
<ul>
<li>
<a class="one">Home</a>
</li>
<li>
<a class="two">About</a>
</li>
<li class="three">
<a class="">Services</a>
<div class="sub-menu">
<div class="col-1-4"></div>
<div class="col-1-4"></div>
<div class="col-1-4"></div>
<div class="col-1-4"></div>
</div>
</li>
<li>
<a class="four">Contact</a>
</li>
</ul>
</div>
</div>
<div class="overlay"></div>
Related
I'm creating navbar with submenu. The elements of the navbar have a bottom border that becomes visible when hovered on. Further, a submenu is also visible. Currently, the bottom border goes invisible when the cursor is moved to submenu. I want it to be visible as long as the submenu is open.
nav {
display: inline-flex;
width: 100%;
}
.nav-list {
display: flex;
width: 100%;
margin-top: .7rem;
/*Use this to change the postition of dropdown*/
padding-left: 1.1rem;
/*Use this to move the dropdown left and right*/
}
.nav-list li {
position: relative;
}
.nav-list>li>a {
color: black;
display: block;
font-size: 1rem;
padding: 1.3rem 1rem;
text-transform: uppercase;
}
.nav-list>li>a::after {
content: "";
position: absolute;
background-color: #ff2a00;
height: 4px;
width: 0;
left: 0;
bottom: -0.5px;
}
.nav-list>li>a:hover:after {
width: 100%;
}
.sub-menu {
display: flex;
position: absolute;
box-sizing: border-box;
background-color: black;
visibility: hidden;
top: 3.89rem;
/*adjust postion */
left: -4rem;
width: 82.5rem;
height: 35rem;
z-index: 5000;
}
.sub-menu a {
position: relative;
top: 2rem;
color: white;
font-size: 1.1rem;
font-weight: 200;
padding: 3rem 40px 0 40px;
}
.sub-menu a:hover {
color: #7e7978;
}
<div class="main" id="navbar">
<nav>
<ul class="nav-list">
<li>
Men
<ul class="sub-menu">
<li>shirts </li>
</ul>
</li>
</ul>
</nav>
</div>
I have added the following code to the .nav-list li:hover .sub-menu block
.nav-list>li:hover .sub-menu {
visibility: visible;
}
This makes sure that the sub-menu is visible as long as the parent element is hovered.
And also added the following code to the .nav-list>li>a:hover:after
.nav-list>li:hover>a::after {
width: 100%;
}
This makes sure that the bottom border is visible as long as the parent element is hovered.
nav {
display: inline-flex;
width: 100%;
}
.nav-list {
display: flex;
width: 100%;
margin-top: .7rem;
/*Use this to change the postition of dropdown*/
padding-left: 1.1rem;
/*Use this to move the dropdown left and right*/
}
.nav-list li {
position: relative;
}
.nav-list>li>a {
color: black;
display: block;
font-size: 1rem;
padding: 1.3rem 1rem;
text-transform: uppercase;
}
.nav-list>li>a::after {
content: "";
position: absolute;
background-color: #ff2a00;
height: 4px;
width: 0;
left: 0;
bottom: -0.5px;
}
.nav-list>li:hover>a::after {
width: 100%;
}
.nav-list>li:hover .sub-menu {
visibility: visible;
}
.sub-menu {
display: flex;
position: absolute;
box-sizing: border-box;
background-color: black;
visibility: hidden;
top: 3.89rem;
/*adjust postion */
left: -4rem;
width: 82.5rem;
height: 35rem;
z-index: 5000;
}
.sub-menu a {
position: relative;
top: 2rem;
color: white;
font-size: 1.1rem;
font-weight: 200;
padding: 3rem 40px 0 40px;
}
.sub-menu a:hover {
color: #7e7978;
}
.nav-list>li:hover .sub-menu {
visibility: visible;
}
.nav-list>li:hover>a::after {
width: 100%;
}
<div class="main" id="navbar">
<nav>
<ul class="nav-list">
<li>
Men
<ul class="sub-menu">
<li>shirts </li>
</ul>
</li>
</ul>
</nav>
</div>
I have the following nav element in my html:
<nav>
<ul>
<li>Unternehmen<div class="navunderline"></div></li>
<li>Leistungen<div class="navunderline"></div></li>
<li>Referenzen<div class="navunderline"></div></li>
<li>News<div class="navunderline"></div></li>
<li>Kontakt<div class="navunderline"></div></li>
</ul>
</nav>
My CSS design for the normal size of the homepage looks like this:
nav {
position: relative;
background-color: #61625B;
width: 100%;
height: 100px;
}
nav ul {
position: absolute;
bottom: 0;
width: 70%;
list-style: none;
padding: 0 15%;
display: flex;
margin: 0;
}
nav li {
width: 125px;
text-align: center;
}
.navunderline {
width: 125px;
height: 0;
margin: 5px 0 0 0;
background-color: #DAD9D7;
transition: 500ms;
}
nav a {
color: #DAD9D7;
}
nav a:hover {
text-decoration: none;
}
nav li:hover .navunderline {
height: 5px;
margin: 0;
}
This works perfectly fine. However, for smaller displays, I want to display the navigation items vertically, so I have the following CSS code:
#media screen and (max-width: 1000px) {
nav {
position: relative;
background-color: #61625B;
width: 100%;
height: auto;
overflow: visible;
padding: 0;
margin: 0;
}
nav ul {
height: auto;
bottom: 0;
width: 100%;
list-style: none;
flex-direction: column;
padding: 0;
}
nav li {
width: 100%;
text-align: center;
margin: 0;
padding: 0;
}
.navunderline {
display: none;
}
nav li:hover {
background-color: #8b131f;
}
}
Here is the problem: The height of my nav element in this case is set to auto. If I set a fixed value, it displays correctly. However, I want to adjust it automatically to the size of the content. Problem is, that with height set to auto, the nav element completely vanishes to zero height. How can I set it, so it's height is adjusted by the elements it contains?
nav {
position: relative;
background-color: #61625B;
width: 100%;
height: 100px;
}
nav ul {
position: absolute;
bottom: 0;
width: 70%;
list-style: none;
padding: 0 15%;
display: flex;
margin: 0;
}
nav li {
width: 125px;
text-align: center;
}
.navunderline {
width: 125px;
height: 0;
margin: 5px 0 0 0;
background-color: #DAD9D7;
transition: 500ms;
}
nav a {
color: #DAD9D7;
}
nav a:hover {
text-decoration: none;
}
nav li:hover .navunderline {
height: 5px;
margin: 0;
}
#media screen and (max-width: 1000px) {
nav {
position: relative;
background-color: #61625B;
width: 100%;
height: auto;
overflow: visible;
padding: 0;
margin: 0;
}
nav ul {
height: auto;
bottom: 0;
width: 100%;
list-style: none;
flex-direction: column;
padding: 0;
}
nav li {
width: 100%;
text-align: center;
margin: 0;
padding: 0;
}
.navunderline {
display: none;
}
nav li:hover {
background-color: #8b131f;
}
}
<nav>
<ul>
<li>Unternehmen<div class="navunderline"></div></li>
<li>Leistungen<div class="navunderline"></div></li>
<li>Referenzen<div class="navunderline"></div></li>
<li>News<div class="navunderline"></div></li>
<li>Kontakt<div class="navunderline"></div></li>
</ul>
</nav>
Problem is, that with height set to auto, the nav element completely vanishes to zero height. How can I set it, so its height is adjusted by the elements it contains?
This problem is occurring because your ul element has position: absolute. Because of this property, your ul element is no longer "living" in the same space of the normal document layout.
To fix this, you can add this to your media query:
#media screen and (max-width: 1000px) {
nav ul {
position: relative;
/* ... */
}
}
nav {
position: relative;
background-color: #61625B;
width: 100%;
height: 100px;
}
nav ul {
position: absolute;
bottom: 0;
width: 70%;
list-style: none;
padding: 0 15%;
display: flex;
margin: 0;
}
nav li {
width: 125px;
text-align: center;
}
.navunderline {
width: 125px;
height: 0;
margin: 5px 0 0 0;
background-color: #DAD9D7;
transition: 500ms;
}
nav a {
color: #DAD9D7;
}
nav a:hover {
text-decoration: none;
}
nav li:hover .navunderline {
height: 5px;
margin: 0;
}
#media screen and (max-width: 1000px) {
nav {
position: relative;
background-color: #61625B;
width: 100%;
height: auto;
overflow: visible;
padding: 0;
margin: 0;
}
nav ul {
position: relative; /* Add this */
height: auto;
bottom: 0;
width: 100%;
list-style: none;
flex-direction: column;
padding: 0;
}
nav li {
width: 100%;
text-align: center;
margin: 0;
padding: 0;
}
.navunderline {
display: none;
}
nav li:hover {
background-color: #8b131f;
}
}
<nav>
<ul>
<li>
Unternehmen
<div class="navunderline"></div>
</li>
<li>
Leistungen
<div class="navunderline"></div>
</li>
<li>
Referenzen
<div class="navunderline"></div>
</li>
<li>
News
<div class="navunderline"></div>
</li>
<li>
Kontakt
<div class="navunderline"></div>
</li>
</ul>
</nav>
See below code. That i have tried but not getting box-shadow for that triangle shape.
As seen in above image i want to create a triangle shape with shadow using css.(For responsive view also).
I have also add extra div and try using rotate properties but not getting proper solution.
ul {
margin-bottom: 0px;
}
ul li {
list-style: none;
}
.main-header {
background-color: #F16322;
display: inline-block;
vertical-align: top;
width: 100%;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 9;
}
.main-header:before {
content: '';
width: 50%;
height: 190px;
background: #cccccc;
top: 0;
position: fixed;
z-index: 0;
clip-path: polygon(100% 0, 0 0, 0 100%);
}
.main-menu {
float: right;
display: inline-block;
}
.main-menu ul {
float: left;
}
.main-menu li {
float: left;
margin-right: 10px;
}
.main-menu li a {
display: inline-block;
padding: 34px 16px;
color: #ffffff;
font-size: 20px;
}
.main-menu>ul>li:last-child {
margin-right: 0px;
}
.main-menu>ul>li:last-child>a {
padding-right: 0px;
}
.main-menu li a:hover,
.main-menu li.current-menu-item>a {
color: #000000;
}
.header-logo {
float: left;
}
.header-wrap {
display: flex;
align-items: center;
justify-content: space-between;
position: relative;
}
<header class="main-header">
<div class="container">
<div class="header-wrap">
<div class="header-logo">
<img src="images/logo.png" alt="BNPK logo">
</div>
<div class="main-menu">
<ul>
<li>
Features
</li>
<li>
Pricing
</li>
<li>
Contact
</li>
</ul>
</div>
</div>
</div>
</header>
As seen in above image i want to create a triangle shape with shadow using css.(For responsive view also).
I have also add extra div and try using rotate properties but not getting proper solution.
You can not add a box-shadow to an element you are using clip-path on, but i fixed it for you using a workaround ive found once.
<head>
<style>
ul {
margin-bottom: 0px;
}
ul li {
list-style: none;
}
.main-header {
background-color: #F16322;
display: inline-block;
vertical-align: top;
width: 100%;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 9;
}
.triangle {
filter: drop-shadow(3px 1px 1px #b0b0aa);
position: absolute;
width: 50%;
height: 190px;
}
.triangle::before {
position: absolute;
z-index: -1;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: #cccccc;
color: #cccccc;
--p: polygon(100% 0, 0 0, 0 100%);
-webkit-clip-path: var(--p);
clip-path: var(--p);
content: '';
}
.main-menu {
float: right;
display: inline-block;
}
.main-menu ul {
float: left;
}
.main-menu li {
float: left;
margin-right: 10px;
}
.main-menu li a {
display: inline-block;
padding: 34px 16px;
color: #ffffff;
font-size: 20px;
}
.main-menu>ul>li:last-child {
margin-right: 0px;
}
</style>
</head>
<body>
<header class="main-header">
<div class="triangle">
</div>
<div class="main-menu">
<ul>
<li>
Features
</li>
<li>
Pricing
</li>
<li>
Contact
</li>
</ul>
</div>
</header>
</body>
</html>
As #Pitzas proposed, a filter would be the solution. Personally, I would prefer to use an SVG for the triangle shape and then apply the shadow to it. Filters are extremely powerful combined with SVGs.
For your shadow, I would use the feGaussianBlur filter:
https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feGaussianBlur
Here you have an example using it:
http://xn--dahlstrm-t4a.net/svg/filters/arrow-with-dropshadow.svg
I'm recreating the navigation from this website but can't get the animations quite the same. It's all working but seems to be a little off.
so far I've tried to increase and decrease the transition timing but it didn't get it anywhere near the example.
const styleParent = (event) => {
let ancestor = document.querySelector('div.nav');
ancestor.classList[event.type === 'mouseenter' ? 'add' : 'remove']('servicesIsHovered');
};
let child = document.querySelector('li.three');
child.addEventListener('mouseenter', styleParent);
child.addEventListener('mouseleave', styleParent);
body {
font-family: acumin-pro, sans-serif;
font-size: 16px;
letter-spacing: .25px;
margin: 0;
}
a {
text-decoration: none;
}
.main {
width: 100%;
}
.section {
width: 100%;
background: #ededed;
}
.header {
position: relative;
display: flex;
width: 100%;
}
.nav {
width: 1100px;
margin: auto;
position: relative;
padding: 0 40;
transition: background-color 0.3s ease;
}
.nav li {
color: #000;
}
.nav li a {
cursor: pointer
}
.nav ul {
list-style: none;
padding: 0;
margin-bottom: 0;
width: fit-content;
}
.nav ul:hover li {
color: #6C6C6C;
padding-bottom: 20px;
}
.nav ul li:hover {
color: #000;
padding-bottom: 20px;
}
.nav li:last-child {
margin: 0;
}
.nav ul li {
display: inline-block;
padding: 0 35px 20px 0;
}
li:hover>.sub-menu {
height: 200px;
}
.sub-menu {
height: 0px;
overflow: hidden;
display: flex;
flex-direction: row;
position: absolute;
top: 100%;
background: #2E2E2E;
left: 0;
right: 0;
transition: height 0.5s ease;
}
.col-1-4 {
height: 100%;
width: 25%;
position: relative;
}
.service {
padding: 20%;
}
.nav.servicesIsHovered {
background-color: #272727;
}
<div class="header">
<div class="nav">
<ul>
<li>
<a class="one">Home</a>
</li>
<li>
<a class="two">About</a>
</li>
<li class="three">
<a class="">Services</a>
<div class="sub-menu">
<div class="col-1-4"></div>
<div class="col-1-4"></div>
<div class="col-1-4"></div>
<div class="col-1-4"></div>
</div>
</li>
<li>
<a class="four">Contact</a>
</li>
</ul>
</div>
</div>
The animation doesn't seem to be as smooth as from the website. Also, they don't seem to be as in sync.
I am stumped on the following. I just added a logo to a site and for some reason, my nav panel links that are to the right of the logo/image are now not clickable. It appears that the image is somehow over-taking them, but I do not see how. In the console/inspect it doesn't show the image over-taking them?
Does anyone see why this is happening?
.header {
margin: 0;
background-color: rgba(0,0,0,0);
height: 80px;
z-index: 9999;
position: absolute;/*test*/
width: 100%;
}
.header_wrap {
margin: 0 4%;
padding: 2% 0 0 0;
}
.logo {
position: absolute;
margin-top: -15px;
cursor: pointer;
}
.logo-img {
/*height: 75px;
width: auto;*/
height: auto;
width: 25%;
}
.logo a {
color: #000;
text-decoration: none;
}
.nav-list {
margin: 0;
list-style: none;
text-align: right;
width: 100%;
padding: 0;
}
.nav-list > a {
display: inline-block;
padding: 0px 15px;
text-decoration: none;
}
.nav-list > a > li {
text-decoration: none;
font-size: 1.2em;
color: #000;
}
.nav-list > a > li:hover {
color: #3f3f3f;
}
<header class="header">
<div class="header_wrap">
<div class="logo"><img src="http://optimumwebdesigns.com/images/LogoOpt2.png" class="logo-img" alt="Optimum Designs"></div>
<ul class="nav-list">
<li>WORK</li>
<li>APPROACH</li>
<li>SERVICES</li>
<li>PROJECT</li>
<li>CONTACT</li>
</ul>
</div>
</header>
I don't understood whay you have give position:absolute to logo but, add z-index: -1; to .logo will make your link clickable..
.header {
margin: 0;
background-color: rgba(0,0,0,0);
height: 80px;
z-index: 9999;
position: absolute;/*test*/
width: 100%;
}
.header_wrap {
margin: 0 4%;
padding: 2% 0 0 0;
}
.logo {
position: absolute;
margin-top: -15px;
cursor: pointer;
z-index: -1;
}
.logo-img {
/*height: 75px;
width: auto;*/
height: auto;
width: 25%;
}
.logo a {
color: #000;
text-decoration: none;
}
.nav-list {
margin: 0;
list-style: none;
text-align: right;
width: 100%;
padding: 0;
}
.nav-list > a {
display: inline-block;
padding: 0px 15px;
text-decoration: none;
}
.nav-list > a > li {
text-decoration: none;
font-size: 1.2em;
color: #000;
}
.nav-list > a > li:hover {
color: #3f3f3f;
}
<header class="header">
<div class="header_wrap">
<div class="logo"><img src="http://optimumwebdesigns.com/images/LogoOpt2.png" class="logo-img" alt="Optimum Designs"></div>
<ul class="nav-list">
<li>WORK</li>
<li>APPROACH</li>
<li>SERVICES</li>
<li>PROJECT</li>
<li>CONTACT</li>
</ul>
</div>
</header>
Edit:
Other solution is give display: block; to .logo a will work. Fiddle
The image is not overtaking them but the <div> the image is sitting in is. It's full width so you have a transparent div sitting on top of your navbar. Limit the width of your logo container, use a span instead or float it as suggestions.
Check that the z-index of the image is below the z-index of the links.
You don't really need to use position: absolute;. Instead use display:inline or inline-block and avoid overlapping.
You CSS would look like this:
.nav-list {
display:inline; /* Add this */
margin: 0;
/* width:100%; you can remove this */
list-style: none;
text-align: right;
padding: 0;
}
.logo {
display:inline; /* add this*/
margin-top: -15px;
cursor: pointer;
/* z-index: -1; no need for z-index */
}
JsFiddle