nav{
display: block;
text-align: center;
}
.theme_tabs{
list-style: none;
padding-left: 0;
width: 100%;
max-width: 1200px;
margin: 0 auto;
}
.theme_tab_item.active{
border-top-color: #0069ff;
}
.theme_tab_item{
display: block;
border-top: 2px solid #e5e8ed;
padding: 16px;
transition: border-left-color .25s linear;
float: left;
}
.theme_tab_item.active a{
color: #031b4e;
cursor: default;
}
.theme_tab_item a{
color: rgba(3,27,78,.5);
font-size: 18px;
transition: color .25s linear;
}
<nav>
<ul class="theme_tabs small_margin">
<li class="theme_tab_item active">
<a>Example1</a>
</li>
<li class="theme_tab_item">
<a>Example2</a>
</li>
<li class="theme_tab_item">
<a>Example3</a>
</li>
</ul>
</nav>
Anyone can please help me How can I horizontally center navigation bar? I tried margin 0 auto but that was not working anyone can please tell me any alter way to achieve this result?
Just add display: inline-block to your .theme-tabs class and remove width: 100%.
.theme_tabs{
list-style: none;
padding-left: 0;
display: inline-block;
max-width: 1200px;
margin: 0 auto;
}
Add the following style
.theme_tabs{
display: flex;
justify-content: center;
}
nav{
display: block;
text-align: center;
}
.theme_tabs{
display: flex;
justify-content: center;
list-style: none;
padding-left: 0;
width: 100%;
max-width: 1200px;
margin: 0 auto;
}
.theme_tab_item.active{
border-top-color: #0069ff;
}
.theme_tab_item{
display: block;
border-top: 2px solid #e5e8ed;
padding: 16px;
transition: border-left-color .25s linear;
float: left;
}
.theme_tab_item.active a{
color: #031b4e;
cursor: default;
}
.theme_tab_item a{
color: rgba(3,27,78,.5);
font-size: 18px;
transition: color .25s linear;
}
<nav>
<ul class="theme_tabs small_margin">
<li class="theme_tab_item active">
<a>Example1</a>
</li>
<li class="theme_tab_item">
<a>Example2</a>
</li>
<li class="theme_tab_item">
<a>Example3</a>
</li>
</ul>
</nav>
try setting its position: relative
its left: 50%
and try playing around with transform so you can center it.
nav{
display: block;
text-align: center;
}
.theme_tabs{
list-style: none;
padding-left: 0;
width: 100%;
max-width: 1200px;
transform: translate(-25%, 0);
position: relative;
left: 50%;
display: inline-block;
}
.theme_tab_item.active{
border-top-color: #0069ff;
}
.theme_tab_item{
display: block;
border-top: 2px solid #e5e8ed;
padding: 16px;
transition: border-left-color .25s linear;
float: left;
}
.theme_tab_item.active a{
color: #031b4e;
cursor: default;
}
.theme_tab_item a{
color: rgba(3,27,78,.5);
font-size: 18px;
transition: color .25s linear;
}
<nav>
<ul class="theme_tabs small_margin">
<li class="theme_tab_item active">
<a>Example1</a>
</li>
<li class="theme_tab_item">
<a>Example2</a>
</li>
<li class="theme_tab_item">
<a>Example3</a>
</li>
</ul>
</nav>
Method A)
.theme_tab_item {
display: block;<--------change to inline-block
float: left;<-----------remove
//other codes
}
nav{
display: block;
text-align: center;
}
.theme_tabs{
list-style: none;
padding-left: 0;
width: 100%;
max-width: 1200px;
margin: 0 auto;
}
.theme_tab_item.active{
border-top-color: #0069ff;
}
.theme_tab_item{
display: inline-block;
border-top: 2px solid #e5e8ed;
padding: 16px;
transition: border-left-color .25s linear;
}
.theme_tab_item.active a{
color: #031b4e;
cursor: default;
}
.theme_tab_item a{
color: rgba(3,27,78,.5);
font-size: 18px;
transition: color .25s linear;
}
<nav>
<ul class="theme_tabs small_margin">
<li class="theme_tab_item active"><a>Example1</a></li>
<li class="theme_tab_item"><a>Example2</a></li>
<li class="theme_tab_item"><a>Example3</a></li>
</ul>
</nav>
Method B)
.theme_tabs{
width: 100%;<-----remove
display: inline-block;<----add
//Other codes...
}
nav{
display: block;
text-align: center;
}
.theme_tabs{
list-style: none;
padding-left: 0;
display: inline-block;
max-width: 1200px;
margin: 0 auto;
}
.theme_tab_item.active{
border-top-color: #0069ff;
}
.theme_tab_item {
display: block;
border-top: 2px solid #e5e8ed;
padding: 16px;
transition: border-left-color .25s linear;
float: left;
}
.theme_tab_item.active a{
color: #031b4e;
cursor: default;
}
.theme_tab_item a {
color: rgba(3,27,78,.5);
font-size: 18px;
transition: color .25s linear;
}
<nav>
<ul class="theme_tabs small_margin">
<li class="theme_tab_item active"><a>Example1</a></li>
<li class="theme_tab_item"><a>Example2</a></li>
<li class="theme_tab_item"><a>Example3</a></li>
</ul>
</nav>
Related
I want to create 2 submenus when the menu in the sidebar is clicked
Here what i've done
$(document).ready(function () {
$("[data-toggle]").click(function () {
var toggle_el = $(this).data("toggle");
$(toggle_el).toggleClass("open-sidebar");
});
$('.parent').click(function () {
$('.submenu').toggle('visible');
});
});
body, html {
height: 100%;
margin: 0;
overflow:hidden;
font-family: helvetica;
font-weight: 100;
}
.container {
position: relative;
height: 100%;
width: 100%;
right: 0;
-webkit-transition: right 0.4s ease-in-out;
-moz-transition: right 0.4s ease-in-out;
-ms-transition: right 0.4s ease-in-out;
-o-transition: right 0.4s ease-in-out;
transition: right 0.4s ease-in-out;
}
.container.open-sidebar {
right: 240px;
}
#sidebar {
position: absolute;
right: -240px;
background: #DF314D;
width: 240px;
height: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box;
border-radius: 4px;
}
#sidebar ul {
margin: 0;
padding: 0;
list-style: none;
}
#sidebar ul li {
margin: 0;
}
#sidebar ul li a {
padding: 15px 20px 15px 35px;
font-size: 16px;
font-weight: 100;
color: white;
text-decoration: none;
display: block;
border-bottom: 1px solid #C9223D;
-webkit-transition: background 0.3s ease-in-out;
-moz-transition: background 0.3s ease-in-out;
-ms-transition: background 0.3s ease-in-out;
-o-transition: background 0.3s ease-in-out;
transition: background 0.3s ease-in-out;
}
#sidebar li:hover {
background: #C9223D;
border-radius: 4px;
}
.main-content {
width: 100%;
height: 100%;
padding: 10px;
box-sizing: border-box;
-moz-box-sizing: border-box;
position: relative;
}
.main-content .content{
box-sizing: border-box;
-moz-box-sizing: border-box;
padding-left: 60px;
width: 100%;
}
.main-content .content h1{
font-weight: 100;
}
.main-content .content p{
width: 100%;
line-height: 160%;
}
.main-content #sidebar-toggle {
background: #DF314D;
border-radius: 3px;
display: block;
position: relative;
padding: 10px 7px;
float: right;
}
.main-content #sidebar-toggle .bar{
display: block;
width: 18px;
margin-bottom: 3px;
height: 2px;
background-color: #fff;
border-radius: 1px;
}
.main-content #sidebar-toggle .bar:last-child{
margin-bottom: 0;
}
.main-content #sidebar-toggle .bar{
display: block;
width: 18px;
margin-bottom: 3px;
height: 2px;
background-color: #fff;
border-radius: 1px;
}
.main-content #sidebar-toggle .bar:last-child{
margin-bottom: 0;
}
.menu a.home {
display: inline-block;
background: url(../imagenes/bpi.png) no-repeat 4px 10px;
}
.menu a.explore {
display: inline-block;
background: url(../imagenes/gasi.png) no-repeat 4px 10px;
}
.menu a.users {
display: inline-block;
background: url(../imagenes/bicici.png) no-repeat 4px 10px;
}
.menu a.signout {
display: inline-block;
background: url(../imagenes/bai.png) no-repeat 4px 10px;
}
#sidebar .menu .submenu {
display: none;
}
#sidebar ul li ul.visible{
display: block;
}
.submenu a.home {
display: inline-block;
background: url(../imagenes/bpi.png) no-repeat 4px 10px;
}
.submenu a.users {
display: inline-block;
background: url(../imagenes/bicici.png) no-repeat 4px 10px;
}
.submenu a.signout {
display: inline-block;
background: url(../imagenes/bai.png) no-repeat 4px 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="container">
<div id="sidebar">
<ul class="menu">
<li class="parent">Home ></li>
<ul class="submenu">
<li>Home 11</li>
<li>Home 12</li>
<li>Home 13</li>
</ul>
<li>Explore</li>
<li> Users</li>
<li class="parent"> Sign Out ></li>
<ul class="submenu">
<li>Home 11</li>
<li>Home 12</li>
<li>Home 13</li>
</ul>
</ul>
</div>
<div class="main-content">
<a href="#" data-toggle=".container" id="sidebar-toggle">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</a>
<div class="content">
</div>
</div>
</div>
I provided my CSS and HTML down below. I am trying to finish off my dropdown, but there's an issue with it that makes the transition not so smooth. It's hard to explain with words on what it's doing.
I have tried changing the display, max-height, padding, and margin but there have been no results. Perhaps I need to add some JavaScript to it rather than CSS? Any suggestions or problems that can be pointed out will be great.
#navigation {
height: 100px;
padding: 10px 3px 3px;
background-color: #FFF;
margin-bottom: 210px;
border-radius: 5px;
}
#nav-container {
display: table;
margin: 10px auto;
}
#nav-items {
list-style-type: none;
margin-left: -45px;
margin-top: -26px;
}
#nav-items li {
display: inline-block;
vertical-align: top;
width: 400px;
}
/* nav-dropdown */
#dropdown {
display: block;
position: absolute;
text-align: center;
width: 300px;
height: 50px;
}
#dropdown p {
font-family: 'Work Sans', sans-serif;
font-size: 35px;
letter-spacing: 5px;
display: table-cell;
margin: 0;
cursor: pointer;
background-color: transparent;
text-align: center;
}
#extensions {
display: table;
border-collapse: separate;
border-spacing: 40px;
height: 50px;
width: 350px;
}
.label {
font-family: 'Work Sans', sans-serif;
font-size: 35px;
letter-spacing: 5px;
text-align: center;
position: absolute;
width: 300px;
}
#dropdown-content {
position: absolute;
margin: 0 auto;
opacity: 0;
width: 300px;
height: 50px;
background-color: #C9C9C9;
border-radius: 8px;
box-shadow: 1px 1px 50px 0px white;
z-index: 1;
overflow-y: hidden;
transition-property: all;
transition-duration: 0.5s;
transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
}
.nav-dropdown-container {width: 350px;height: 800px;}
#dropdown-content p {
font-size: 20px;
font-family: 'Work Sans', sans-serif;
font-size: 30px;
letter-spacing: 2px;
}
#dropdown-content:hover {
opacity: 1;
max-height: 500px;
padding-top: 1em;
padding-bottom: 1em;
margin-top: 50px;
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
display: block;
}
#dropdown-content:hover p {
display: block;
text-decoration: none;
transition: 0.5s;
}
#dropdown-content p {display: none;}
#dropdown-link {color: white;}
#dropdown-link:link {
color: white;
text-decoration: none;
}
#dropdown-link:hover {
color: lightgrey;
}
<nav id="navigation">
<div id="nav-container">
<ul id="nav-items">
<li>
<div id="extensions">
<div class="nav-dropdown-container">
<div id="dropdown">
<p class="label">TEST</p>
<div id="dropdown-content">
<p><a id="dropdown-link" href="hello.html">HELLO</a></p>
<p><a id="dropdown-link" href="world.html">WORLD</a></p>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
</nav>
As stated in the comments, the reason is because when the :hover state is triggered on #dropdown-content, the mouse cursor quickly enters and leaves the element causing the state to be quickly toggled on and off repeatedly, resulting in a janky transition.
The fastest fix is this: you should change your selectors so that you bind the :hover state to the nearest common ancestor of both the label AND the dropdown content, i.e. #dropdown.
So you should change this:
#dropdown-content:hover { ... }
#dropdown-content:hover p { ... }
...to this:
#dropdown:hover #dropdown-content { ... }
#dropdown:hover #dropdown-content p { ... }
This is of course a stop-gap solution since I find your markup unnecessarily bloated in order to achieve a simple dropdown effect.
#navigation {
height: 100px;
padding: 10px 3px 3px;
background-color: #FFF;
margin-bottom: 210px;
border-radius: 5px;
}
#nav-container {
display: table;
margin: 10px auto;
}
#nav-items {
list-style-type: none;
margin-left: -45px;
margin-top: -26px;
}
#nav-items li {
display: inline-block;
vertical-align: top;
width: 400px;
}
/* nav-dropdown */
#dropdown {
display: block;
position: absolute;
text-align: center;
width: 300px;
height: 50px;
}
#dropdown p {
font-family: 'Work Sans', sans-serif;
font-size: 35px;
letter-spacing: 5px;
display: table-cell;
margin: 0;
cursor: pointer;
background-color: transparent;
text-align: center;
}
#extensions {
display: table;
border-collapse: separate;
border-spacing: 40px;
height: 50px;
width: 350px;
}
.label {
font-family: 'Work Sans', sans-serif;
font-size: 35px;
letter-spacing: 5px;
text-align: center;
position: absolute;
width: 300px;
}
#dropdown-content {
position: absolute;
margin: 0 auto;
opacity: 0;
width: 300px;
height: 50px;
background-color: #C9C9C9;
border-radius: 8px;
box-shadow: 1px 1px 50px 0px white;
z-index: 1;
overflow-y: hidden;
transition-property: all;
transition-duration: 0.5s;
transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
}
.nav-dropdown-container {
width: 350px;
height: 800px;
}
#dropdown-content p {
font-size: 20px;
font-family: 'Work Sans', sans-serif;
font-size: 30px;
letter-spacing: 2px;
}
#dropdown:hover #dropdown-content {
opacity: 1;
max-height: 500px;
padding-top: 1em;
padding-bottom: 1em;
margin-top: 50px;
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
display: block;
}
#dropdown:hover #dropdown-content p {
display: block;
text-decoration: none;
transition: 0.5s;
}
#dropdown-content p {
display: none;
}
#dropdown-link {
color: white;
}
#dropdown-link:link {
color: white;
text-decoration: none;
}
#dropdown-link:hover {
color: lightgrey;
}
<nav id="navigation">
<div id="nav-container">
<ul id="nav-items">
<li>
<div id="extensions">
<div class="nav-dropdown-container">
<div id="dropdown">
<p class="label">TEST</p>
<div id="dropdown-content">
<p><a id="dropdown-link" href="hello.html">HELLO</a></p>
<p><a id="dropdown-link" href="world.html">WORLD</a></p>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
</nav>
I am currently designing the navigation bar for my website and I've come across a problem in firefox. I have my logo to the left and my navigation content to the right, but the entire navigation content is being pushed out of the nav div (only on Firefox). What can I do to fix it?
Firefox:
http://i.cubeupload.com/OoAJIe.png
Chrome:
http://i.cubeupload.com/QXo2KS.png
Here is my HTML:
<div class="nav" id="nav">
<div class="nav_content">
<ul>
<li class="logo"><img src="images/Spendr.png" /></li>
<li class="tab btn"><button>Kurv: 0</button></li>
<li class="tab txt">Nye varer</li>
<li class="tab txt">Udsalg</li>
<li class="tab txt" id="kategorierMenu"><a>Kategorier</a></li>
</ul>
</div>
</div>
Here is my CSS:
.nav {
width: 100%;
background: #262626;
transition: background 0.5s, border 0.5s, color 0.5s, height 0.5s, padding-top 0.3s, opacity 0.5s;
position: fixed /*fixed*/;
z-index: 999;
height: 71px;
float: left;
}
.nav_content {
max-width: 1150px;
width: 90%;
height: 100%;
background: none;
margin: 0 auto;
white-space: nowrap;
}
.nav ul li, .nav ul li a {
list-style: none;
text-decoration: none;
display: inline-block;
} .nav .tab{
float: right;
font-family: DroidSans, sans-serif;
font-size: 16px;
color: #FFFFFF;
text-decoration: none;
transition: color 0.3s;
}
.txt a {
float: right;
font-family: DroidSans, sans-serif;
font-size: 16px;
color: #FFFFFF;
text-decoration: none;
transition: color 0.3s;
height: 100%;
padding-top: 26px;
box-sizing: border-box;
transition: background 0.3s;
}
.txt a:hover {
cursor: pointer;
background: #1A1A1A;
}
.nav .tab { border-radius: 3px; }
.nav .txt a {
padding-left:18px;
padding-right: 18px;
}
.logo img {
height: 36px;
width: auto;
margin-top: 18px;
float: left;
}
Since you're floating the .tab elements right, add .logo { float: left; } and that will make the list items all float on either side of the menu.
.nav {
width: 100%;
background: #262626;
transition: background 0.5s, border 0.5s, color 0.5s, height 0.5s, padding-top 0.3s, opacity 0.5s;
position: fixed/*fixed*/
;
z-index: 999;
height: 71px;
float: left;
}
.nav_content {
max-width: 1150px;
width: 90%;
height: 100%;
background: none;
margin: 0 auto;
white-space: nowrap;
}
.nav ul li,
.nav ul li a {
list-style: none;
text-decoration: none;
display: inline-block;
}
.nav .tab {
float: right;
font-family: DroidSans, sans-serif;
font-size: 16px;
color: #FFFFFF;
text-decoration: none;
transition: color 0.3s;
}
.txt a {
float: right;
font-family: DroidSans, sans-serif;
font-size: 16px;
color: #FFFFFF;
text-decoration: none;
transition: color 0.3s;
height: 100%;
padding-top: 26px;
box-sizing: border-box;
transition: background 0.3s;
}
.txt a:hover {
cursor: pointer;
background: #1A1A1A;
}
.nav .tab {
border-radius: 3px;
}
.nav .txt a {
padding-left: 18px;
padding-right: 18px;
}
.logo img {
height: 36px;
width: auto;
margin-top: 18px;
float: left;
}
.logo {
float: left;
}
<div class="nav" id="nav">
<div class="nav_content">
<ul>
<li class="logo">
<img src="images/Spendr.png" />
</li>
<li class="tab btn"><button>Kurv: 0</button></li>
<li class="tab txt">Nye varer</li>
<li class="tab txt">Udsalg</li>
<li class="tab txt" id="kategorierMenu"><a>Kategorier</a></li>
</ul>
</div>
</div>
You could also change the menu to a flex layout and use an auto margin on .logo to separate the elements.
.nav {
width: 100%;
background: #262626;
transition: background 0.5s, border 0.5s, color 0.5s, height 0.5s, padding-top 0.3s, opacity 0.5s;
position: fixed/*fixed*/
;
z-index: 999;
height: 71px;
float: left;
}
.nav_content {
max-width: 1150px;
width: 90%;
height: 100%;
background: none;
margin: 0 auto;
white-space: nowrap;
}
.nav ul li,
.nav ul li a {
list-style: none;
text-decoration: none;
display: inline-block;
}
.nav .tab {
float: right;
font-family: DroidSans, sans-serif;
font-size: 16px;
color: #FFFFFF;
text-decoration: none;
transition: color 0.3s;
}
.txt a {
float: right;
font-family: DroidSans, sans-serif;
font-size: 16px;
color: #FFFFFF;
text-decoration: none;
transition: color 0.3s;
height: 100%;
padding-top: 26px;
box-sizing: border-box;
transition: background 0.3s;
}
.txt a:hover {
cursor: pointer;
background: #1A1A1A;
}
.nav .tab {
border-radius: 3px;
}
.nav .txt a {
padding-left: 18px;
padding-right: 18px;
}
.logo img {
height: 36px;
width: auto;
margin-top: 18px;
float: left;
}
.nav ul {
display: flex;
}
.logo {
margin-right: auto;
}
<div class="nav" id="nav">
<div class="nav_content">
<ul>
<li class="logo">
<img src="images/Spendr.png" />
</li>
<li class="tab btn"><button>Kurv: 0</button></li>
<li class="tab txt">Nye varer</li>
<li class="tab txt">Udsalg</li>
<li class="tab txt" id="kategorierMenu"><a>Kategorier</a></li>
</ul>
</div>
</div>
The problem I am having is the formatting of the CSS once the windows resize to smaller. I have been having a lot of trouble, and think that the problem lies in my initial code; I just am not sure where.
I am pretty new to coding and hoping to learn.
.nav-main {
position: absolute;
top: 0;
height: 65px;
width: 100%;
text-align: center;
}
.nav-main ul {
position: relative;
margin: 0 auto;
padding: 0;
list-style: none;
font-size: 22px;
line-height: 100%;
font-family: 'Futura W01 Bold', sans-serif;
text-align: center;
text-transform: uppercase;
display: inline-block;
width: 100%;
}
.nav-top {
position: relative;
margin: 0;
padding: 0 66px 0 50px;
float: none;
display: inline-block;
list-style: none;
}
.nav-top:first-child {
padding-left: 0;
}
.nav-top:last-child {
background-image: none;
padding-right: 0;
}
.nav-top:last-child:after {
content: none;
}
.nav-top > a {
position: relative;
display: block;
margin: 0;
color: #6f6f6f;
text-decoration: none;
padding-top: 20px;
padding-bottom: 5px;
-moz-transition: all 0.3s ease;
-ms-transition: all 0.3s ease;
-o-transition: all 0.3s ease;
transition: all 0.3s ease;
}
.nav-top a:hover,
.nav-top.active > a {
color: #454545;
border-bottom: 4px solid #00e9d9;
text-decoration: none;
}
.nav-top ul {
display: block;
position: absolute;
left: -8.75px;
width: 105%;
top: calc(100% - 1px);
border-bottom-left-radius: .3em;
border-bottom-right-radius: .3em;
}
.nav-top:hover ul {
position: absolute;
top: calc(100% - 1px);
left: -8.75px;
width: 105%;
}
.nav-top li {
float: center;
background-color: #e9e9e9;
padding-top: 16px;
padding-bottom: 10px;
text-align: inherit;
}
.nav-top li:last-child {
padding-bottom: 16px;
border-bottom-left-radius: .3em;
border-bottom-right-radius: .3em;
}
.nav-top li > a {
position: relative;
display: inline;
margin: 0;
color: #6f6f6f;
text-decoration: none;
padding-top: 20px;
-moz-transition: all 0.3s ease;
-ms-transition: all 0.3s ease;
-o-transition: all 0.3s ease;
transition: all 0.3s ease;
}
.nav-top:after {
display: block;
position: absolute;
left: 100%;
top: -17px;
width: 22px;
z-index: 1;
transform: translateX(-50%);
height: 100%;
-ms-transform: translateX(-50%);
-webkit-transform: translateX(-50%);
}
#media only screen and (max-width:960px) {
.nav-main ul {
font-size: 18px;
}
.nav-top ul {
font-size: 15px;
}
.nav-main li {
padding: 0 46px 0 30px; /* 0 66 0 50 */
}
.nav-top li {
padding-top: 11px;
text-align: center;/* top 16 bottom 10*/
float: center;
}
<nav class="nav-main" role="navigation">
<ul>
<li class="nav-top">Welcome</li>
<li class="nav-top">About
<ul class="drop-down">
<li class="nav-drop">Services</li>
<li class="nav-drop">Clients</li>
<li class="nav-drop">Press</li>
<li class="nav-drop">Leadership</li>
<li class="nav-drop">Follow Us</li>
</ul>
</li>
<li class="nav-top">Contact</li>
</ul>
<span class="nav-arrow"></span>
</nav>
JSFIDDLEJS fiddle
Your .nav-main li on #media only screen and (max-width:960px) is affecting it.
You could either use the :not selector .nav-main li:not(.nav-drop) or overwrite the .nav-drop with padding-left: 0; padding-right: 0.
Don't forget to revise it in case you use other media queries.
JSFiddle
Why does the paragraph always cover the div that is shown when you hover over link 1 in this example? I have tried changing the z-index of both the paragraph and the list, but nothing works. The thing that really makes me not understand is the fact that during the transition, the list appears over the paragraph. Then, after the transition, the paragraph seems to change z-index. Any help would be great.
div {
background-color: #BFBFBF;
height: 50px;
width: 100%;
z-index: 1;
}
.NavList {
list-style: none;
margin: 0;
padding: 0;
}
.NavListItem {
display: inline-block;
text-align: center;
float: left;
}
.NavListItem:Hover .NavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #7F7F7F;
transition: background-color .5s;
}
.NavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #404040;
transition: background-color .5s;
}
.NavListItem:Hover .SubNavList {
display: block;
list-style: none;
margin: 0;
padding: 0;
opacity: 1;
z-index: 100;
transition: opacity 5s;
}
.NavListItem:Hover .SubNavListItem {
display: block;
text-align: center;
}
.NavListItem:Hover .SubNavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #7F7F7F;
transition: background-color .5s;
}
.NavListItem:Hover .SubNavLink:Hover {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #404040;
transition: background-color .5s;
}
.SubNavList {
display: block;
width: 0px;
height: 0px;
opacity: 0;
transition: opacity 5s;
z-index: 100;
}
.SubNavListItem {
display: none;
}
p {
z-index: -1;
}
<body>
<div>
<ul class="NavList">
<li class="NavListItem">
Home
</li>
<li class="NavListItem">
Link 1
<ul class="SubNavList">
<li class="SubNavListItem">
SubLink 1
</li>
<li class="SubNavListItem">
SubLink 2
</li>
</ul>
</li>
<li class="NavListItem">
Link 2
<ul class="SubNavList">
<li class="SubNavListItem">
SubLink 3
</li>
<li class="SubNavListItem">
SubLink 4
</li>
</ul>
</li>
<li class="NavListItem">
Link3
</li>
</ul>
</div>
<p>Lorem ipsum dolor sit amet</p>
</body>
Z-index depends on the element having "positioning." Give your div and you p position like so:
div,p {
position: relative;
}
Fiddle example: http://jsfiddle.net/9dp7p5LL/
Just do this:
p {
display: block;
position: absolute;
z-index: -1;
}
See the updated fiddle here
z-index applies to positioned elements.
source
In your example you don't need negative z-index to p elements. You can just apply position: relative with a high z-index to .NavListItem:Hover .SubNavLink:hover like:
div {
background-color: #BFBFBF;
height: 50px;
width: 100%;
z-index: 1;
}
.NavList {
list-style: none;
margin: 0;
padding: 0;
}
.NavListItem {
display: inline-block;
text-align: center;
float: left;
}
.NavListItem:Hover .NavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #7F7F7F;
transition: background-color .5s;
}
.NavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #404040;
transition: background-color .5s;
}
.NavListItem:Hover .SubNavList {
display: block;
list-style: none;
margin: 0;
padding: 0;
opacity: 1;
z-index: 100;
transition: opacity 5s;
}
.NavListItem:Hover .SubNavListItem {
display: block;
text-align: center;
}
.NavListItem:Hover .SubNavLink {
display: block;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #7F7F7F;
transition: background-color .5s;
}
.NavListItem:Hover .SubNavLink:Hover {
display: block;
position: relative;
z-index: 10;
padding-top: 10px;
padding-bottom: 10px;
width: 100px;
color: #BFBFBF;
text-decoration: none;
background-color: #404040;
transition: background-color .5s;
}
.SubNavList {
display: block;
width: 0px;
height: 0px;
opacity: 0;
transition: opacity 5s;
z-index: 100;
}
.SubNavListItem {
display: none;
}
<body>
<div>
<ul class="NavList">
<li class="NavListItem">
Home
</li>
<li class="NavListItem">
Link 1
<ul class="SubNavList">
<li class="SubNavListItem">
SubLink 1
</li>
<li class="SubNavListItem">
SubLink 2
</li>
</ul>
</li>
<li class="NavListItem">
Link 2
<ul class="SubNavList">
<li class="SubNavListItem">
SubLink 3
</li>
<li class="SubNavListItem">
SubLink 4
</li>
</ul>
</li>
<li class="NavListItem">
Link3
</li>
</ul>
</div>
<p>Lorem ipsum dolor sit amet</p>
</body>