Navbar dropdown width resize - html

I have a navigation bar that is fluid on both desktop and mobile devices. However, the drop-down items widths don't resize based on the parent's width. I would like to hover over a drop-down item and it has the same width as the list item I hovered over. At the moment it has a fixed width but I can't seem to get it working by trying other options. I also cannot get the entire navigation bar to center, as at the moment it is stuck to the left. Thanks for the help!
Here's a codepen of the code: https://codepen.io/Macast/pen/rYQPNe
HTML:
<nav>
<div id="logo">
<img src="images/J.-Freeman-&-Son-Landscape-Logo-White.png">
</div>
<label for="drop" class="toggle">Menu</label>
<input type="checkbox" id="drop" />
<ul class="menu">
<li>Home</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-1" class="toggle">Short +</label>
Short
<input type="checkbox" id="drop-1" />
<ul>
<li>History</li>
<li>Our Services</li>
<li>Our Aim</li>
</ul>
</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-2" class="toggle">Dropdown Even Longer +</label>
Dropdown Even Longer
<input type="checkbox" id="drop-2" />
<ul>
<li>Option</li>
<li>Option</li>
<li>Option</li>
<li>
<!-- Second Tier Drop Down -->
<label for="drop-3" class="toggle">More Options +</label>
More Options
<input type="checkbox" id="drop-3" />
<ul>
<li>Option</li>
<li>Option</li>
<li>Option</li>
</ul>
</li>
</ul>
</li>
<li>Option</li>
<li>Option</li>
<li>Option</li>
<li>Option</li>
</ul>
</nav>
CSS:
.toggle,
[id^=drop] {
display: none;
}
/* Giving a background-color to the nav container. */
nav {
margin: 0;
padding: 0;
background-color: #254441;
}
#logo {
display: block;
text-align: center;
/*padding: 0 30px;*/
/*float: left;*/
/*font-size: 20px;*/
/*line-height: 60px; */
}
#logo img {
width: 30%;
}
/* Since we'll have the "ul li" "float:left"
* we need to add a clear after the container. */
nav:after {
content: "";
display: table;
clear: both;
}
/* Removing padding, margin and "list-style" from the "ul",
* and adding "position:reltive" */
nav ul {
/*float: right;*/
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
/* Positioning the navigation items inline */
nav ul li {
margin: 0px;
display: inline-block;
float: left;
background-color: #254441;
text-align: center;
}
/* Styling the links */
nav a {
display: block;
padding: 14px 20px;
color: #FFF;
font-size: 17px;
text-decoration: none;
text-align: center;
}
nav ul li ul li:hover {
background: #000000;
}
/* Background color change on Hover */
nav a:hover {
background-color: #000000;
}
/* Hide Dropdowns by Default
* and giving it a position of absolute */
nav ul ul {
display: none;
position: absolute;
/* has to be the same number as the "line-height" of "nav a" */
top: 60px;
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display: inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
width: 170px;
float: none;
display: list-item;
position: relative;
text-align: center;
}
/* Second, Third and more Tiers
* We move the 2nd and 3rd etc tier dropdowns to the left
* by the amount of the width of the first tier.
*/
nav ul ul ul li {
position: relative;
top: -60px;
/* has to be the same number as the "width" of "nav ul ul li" */
left: 170px;
}
/* Change ' +' in order to change the Dropdown symbol */
li > a:after {
content: ' +';
}
li > a:only-child:after {
content: '';
}
/* Media Queries
--------------------------------------------- */
#media all and (max-width: 768px) {
#logo {
display: block;
padding: 0;
width: 100%;
text-align: center;
float: none;
}
#logo img {
width: 100%;
box-sizing: border-box;
padding: 20px;
}
nav {
margin: 0;
}
/* Hide the navigation menu by default */
/* Also hide the */
.toggle + a,
.menu {
display: none;
}
/* Stylinf the toggle lable */
.toggle {
display: block;
background-color: #254441;
padding: 14px 20px;
color: #FFF;
font-size: 17px;
text-decoration: none;
border: none;
text-align: center;
}
.toggle:hover {
background-color: #000000;
}
/* Display Dropdown when clicked on Parent Lable */
[id^=drop]:checked + ul {
display: block;
}
/* Change menu item's width to 100% */
nav ul li {
display: block;
width: 100%;
}
nav ul ul .toggle,
nav ul ul a {
padding: 0 40px;
}
nav ul ul ul a {
padding: 0 80px;
}
nav a:hover,
nav ul ul ul a {
background-color: #000000;
}
nav ul li ul li .toggle,
nav ul ul a,
nav ul ul ul a {
padding: 14px 20px;
color: #FFF;
font-size: 17px;
}
nav ul li ul li .toggle,
nav ul ul a {
background-color: #212121;
}
/* Hide Dropdowns by Default */
nav ul ul {
float: none;
position: static;
color: #ffffff;
/* has to be the same number as the "line-height" of "nav a" */
}
/* Hide menus on hover */
nav ul ul li:hover > ul,
nav ul li:hover > ul {
display: none;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
display: block;
width: 100%;
}
nav ul ul ul li {
position: static;
/* has to be the same number as the "width" of "nav ul ul li" */
}
}
#media all and (max-width: 330px) {
nav ul li {
display: block;
width: 94%;
}
}

To adjust the dropdown menu size to the same size as the containing list-item, declare a left and right property value of 0 on the dropdown element in question (nav ul ul), then remove the explicitly declared width on the nested list items of the dropdown menu (nav ul ul li).
Example:
nav ul ul {
display: none;
position: absolute;
top: 60px;
/* additional property values declared */
left: 0;
right: 0;
}
This works because the dropdown menus are already positioned absolute and the containing parent element list item (nav ul li) is already positioned relative.
You can position absolute elements relative to their parents if their parents are relative.
So all we are doing here is defining the extent of the dropdown menu width to "stretch" from "left to right" of the parent's width.
Code Snippet Demonstration:
.toggle,
[id^=drop] {
display: none;
}
/* Giving a background-color to the nav container. */
nav {
margin: 0;
padding: 0;
background-color: #254441;
}
#logo {
display: block;
text-align: center;
/*padding: 0 30px;*/
/*float: left;*/
/*font-size: 20px;*/
/*line-height: 60px; */
}
#logo img {
width: 30%;
}
/* Since we'll have the "ul li" "float:left"
* we need to add a clear after the container. */
nav:after {
content: "";
display: table;
clear: both;
}
/* Removing padding, margin and "list-style" from the "ul",
* and adding "position:reltive" */
nav ul {
text-align: center;
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
/* Positioning the navigation items inline */
nav ul li {
margin: 0px;
display: inline-block;
background-color: #254441;
text-align: center;
position: relative;
}
/* Styling the links */
nav a {
display: block;
padding: 14px 20px;
color: #FFF;
font-size: 17px;
text-decoration: none;
text-align: center;
}
nav ul li ul li:hover {
background: #000000;
}
/* Background color change on Hover */
nav a:hover {
background-color: #000000;
}
/* Hide Dropdowns by Default
* and giving it a position of absolute */
nav ul ul {
display: none;
position: absolute;
/* has to be the same number as the "line-height" of "nav a" */
top: 60px;
left: 0;
right: 0;
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display: inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
/*width: 170px;*/
float: none;
display: list-item;
position: relative;
text-align: center;
}
/* Second, Third and more Tiers
* We move the 2nd and 3rd etc tier dropdowns to the left
* by the amount of the width of the first tier.
*/
nav ul ul ul li {
position: relative;
top: -60px;
/* has to be the same number as the "width" of "nav ul ul li" */
left: 170px;
}
/* Change ' +' in order to change the Dropdown symbol */
li > a:after {
content: ' +';
}
li > a:only-child:after {
content: '';
}
/* Media Queries
--------------------------------------------- */
#media all and (max-width: 768px) {
#logo {
display: block;
padding: 0;
width: 100%;
text-align: center;
float: none;
}
#logo img {
width: 100%;
box-sizing: border-box;
padding: 20px;
}
nav {
margin: 0;
}
/* Hide the navigation menu by default */
/* Also hide the */
.toggle + a,
.menu {
display: none;
}
/* Stylinf the toggle lable */
.toggle {
display: block;
background-color: #254441;
padding: 14px 20px;
color: #FFF;
font-size: 17px;
text-decoration: none;
border: none;
text-align: center;
}
.toggle:hover {
background-color: #000000;
}
/* Display Dropdown when clicked on Parent Lable */
[id^=drop]:checked + ul {
display: block;
}
/* Change menu item's width to 100% */
nav ul li {
display: block;
width: 100%;
}
nav ul ul .toggle,
nav ul ul a {
padding: 0 40px;
}
nav ul ul ul a {
padding: 0 80px;
}
nav a:hover,
nav ul ul ul a {
background-color: #000000;
}
nav ul li ul li .toggle,
nav ul ul a,
nav ul ul ul a {
padding: 14px 20px;
color: #FFF;
font-size: 17px;
}
nav ul li ul li .toggle,
nav ul ul a {
background-color: #212121;
}
/* Hide Dropdowns by Default */
nav ul ul {
float: none;
position: static;
color: #ffffff;
/* has to be the same number as the "line-height" of "nav a" */
}
/* Hide menus on hover */
nav ul ul li:hover > ul,
nav ul li:hover > ul {
display: none;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
display: block;
width: 100%;
}
nav ul ul ul li {
position: static;
/* has to be the same number as the "width" of "nav ul ul li" */
}
}
#media all and (max-width: 330px) {
nav ul li {
display: block;
width: 94%;
}
}
<nav>
<div id="logo">
<img src="images/J.-Freeman-&-Son-Landscape-Logo-White.png">
</div>
<label for="drop" class="toggle">Menu</label>
<input type="checkbox" id="drop" />
<ul class="menu">
<li>Home</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-1" class="toggle">Short +</label>
Short
<input type="checkbox" id="drop-1" />
<ul>
<li>History</li>
<li>Our Services</li>
<li>Our Aim</li>
</ul>
</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-2" class="toggle">Dropdown Even Longer +</label>
Dropdown Even Longer
<input type="checkbox" id="drop-2" />
<ul>
<li>Option</li>
<li>Option</li>
<li>Option</li>
<li>
<!-- Second Tier Drop Down -->
<label for="drop-3" class="toggle">More Options +</label>
More Options
<input type="checkbox" id="drop-3" />
<ul>
<li>Option</li>
<li>Option</li>
<li>Option</li>
</ul>
</li>
</ul>
</li>
<li>Option</li>
<li>Option</li>
<li>Option</li>
<li>Option</li>
</ul>
</nav>
In Addition:
You can horizontally center the navigation menu by removing the float rule declared on nested list-items (nav ul li), as this will negate any attempt to align content (unless you use flex-box), then declare text-align: center on the containing unordered list (.menu), as demonstrated below:
nav ul {
text-align: center;
padding: 0;
margin: 0;
list-style: none;
position: relative;
}

Related

Menu is not clickable when mouse is on middle of button

I'm running into this little issue where when i put the mouse on the very top of a button i can click on it but when it goes just a little down the dropdown menu appears and i can't click anymore on the button :
Here's a codepen for the code : https://codepen.io/amrouhab/pen/jObXxOy
And here's my code :
<body>
<div id="header_bar">
<div id="header_bar_text" class="gpd-text">Syndicat Constructif, Partenaire du Dialogue Social
</div>
</div>
<nav>
<div href="#default" id="logo"><img src="logo_header.png"></div>
<label for="drop" class="toggle">Menu</label>
<input type="checkbox" id="drop" />
<ul class="menu">
<li>Accueil</li>
<li>
<label for="drop-1" class="toggle">Connaitre la CFTC +</label>
Connaitre la CFTC
<input type="checkbox" id="drop-1" />
<ul>
<li>Histoire</li>
<li>Valeurs</li>
<li>Identité</li>
</ul>
</li>
<li>
<label for="drop-1" class="toggle">Syndicat CFTC-FPT 34 +</label>
Syndicat CFTC-FPT 34
<input type="checkbox" id="drop-2" />
<ul>
<li>Organisation</li>
<li>Accueil</li>
<li>Rôle et définition</li>
<!-- <li>
SECOND TIER DROP
<label for="drop-3" class="toggle">Tutorials +</label>
Tutorials
<input type="checkbox" id="drop-3" />
<ul>
<li>HTML/CSS</li>
<li>jQuery</li>
<li>Other</li>
</ul>
</li> -->
</ul>
</li>
<li>
<label for="drop-1" class="toggle">Vie Professionnelle +</label>
Vie Professionnelle
<input type="checkbox" id="drop-1" />
<ul>
<li>Formation</li>
<li>Statut</li>
<li>Carrière</li>
<li>Temps de Travail</li>
<li>Congés</li>
</ul>
</li>
<li>
<label for="drop-1" class="toggle">Vie Pratique +</label>
Vie Pratique
<input type="checkbox" id="drop-1" />
<ul>
<li>Action Logement</li>
<li>Pensions Alimentaires impayées</li>
<li>Prime d'activité</li>
<li>Apprentissage</li>
</ul>
</li>
<li>
<label for="drop-1" class="toggle">Activités +</label>
Activités
<input type="checkbox" id="drop-1" />
<ul>
<li>Actualités</li>
<li>Presse</li>
</ul>
</li>
<li>
<label for="drop-1" class="toggle">Adhésion +</label>
Adhésion
<input type="checkbox" id="drop-1" />
<ul>
<li>Les + adhérents</li>
<li>Espace adhérents</li>
<li>Nous rejoindre</li>
</ul>
</li>
<li>
<label for="drop-1" class="toggle">Coordonnées +</label>
Coordonnées
<input type="checkbox" id="drop-1" />
<ul>
<li>Syndicat CFTC-FPT 34</li>
<li>UD34</li>
<li>Fédération CFTC FPT</li>
</ul>
</li>
</ul>
</nav>
</body>
And my CSS :
/* CSS Document */
#font-face {
font-family: "oswald";
src: url("fonts\oswald.regular.ttf");
font-family: "lato";
src: url("fonts\Lato-Regular.ttf");
font-family: "playfairdisplay";
src: url("fonts\PlayfairDisplaySC-Regular.otf");
}
body {
background: #e9dfdf;
/* font-size: 30px; */
/* line-height: 32px; */
color: #ffffff;
margin: 0;
/* padding: 0; */
word-wrap: break-word !important;
font-family: oswald;
}
/* h1 {
font-size: 60px;
text-align: center;
color: #FFF;
}
h3 {
font-size: 30px;
line-height: 34px;
text-align: center;
color: #FFF;
}
h3 a {
color: #FFF;
} */
a {
color: #FFF;
}
/* h1 {
margin-top: 100px;
text-align: center;
font-size: 60px;
line-height: 70px;
font-family: 'Bree Serif', 'serif';
} */
#container {
margin: 0 auto;
max-width: 890px;
}
/* p {
text-align: center;
} */
.toggle,
[id^=drop] {
display: none;
}
/* Giving a background-color to the nav container. */
nav {
margin: 0px;
padding: 15px 10px 10px 10px;
background-color: #01B2C0;
position: relative;
}
#logo {
display: block;
padding: 0px 30px 0px 15px;
float: left;
/* font-size: 20px; */
/* line-height: 60px; */
}
/* Since we'll have the "ul li" "float:left"
* we need to add a clear after the container. */
nav:after {
content: "";
display: table;
clear: both;
}
/* Removing padding, margin and "list-style" from the "ul",
* and adding "position:reltive" */
nav ul {
float: none;
padding: 0;
margin: 0;
list-style: none;
position: absolute;
/* bottom: 0; */
left: 137px;
padding: 44px 37px 35px 120px;
}
/* Positioning the navigation items inline */
nav ul li {
margin: 0px;
display: inline-block;
background-color: #01B2C0;
position: relative;
padding: 0px 5px 0px 5px;
}
/* Styling the links */
nav a {
display: block;
padding: 10px 15px;
color: #000000;
font-size: 24px;
text-decoration: none;
position: relative;
}
nav ul li ul li:hover {
border: solid 1px;
background: #07858f;
}
/* Background color change on Hover */
nav a:hover {
background-color: #07858f;
}
/* Hide Dropdowns by Default
* and giving it a position of absolute */
nav ul ul {
display: none;
position: absolute;
/* has to be the same number as the "line-height" of "nav a" */
top: 15px;
left: -100px;
}
/* Display Dropdowns on Hover */
nav ul li:hover>ul {
display: inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
width: 170px;
float: none;
display: list-item;
position: relative;
}
/* Second, Third and more Tiers
* We move the 2nd and 3rd etc tier dropdowns to the left
* by the amount of the width of the first tier.
*/
nav ul ul ul li {
position: relative;
top: -60px;
/* has to be the same number as the "width" of "nav ul ul li" */
left: 170px;
}
/* Change ' +' in order to change the Dropdown symbol */
li>a:after {
content: ' +';
}
li>a:only-child:after {
content: '';
}
#header_bar{
padding:5px 0px 0px 0px;
min-height:50px;
background-color:#01B2C0;
margin: auto;
color: #000000;
font-size: 25px;
}
#header_bar_text{
background-color:#01B2C0;
margin: 0px 0px 0px 0px;
text-align: center;
}
/* Media Queries
--------------------------------------------- */
#media all and (max-width : 768px) {
#logo {
display: block;
padding: 0;
width: 100%;
text-align: center;
float: none;
}
nav {
margin: 0;
}
/* Hide the navigation menu by default */
/* Also hide the */
.toggle+a,
.menu {
display: none;
}
/* Stylinf the toggle lable */
.toggle {
display: block;
background-color: #254441;
padding: 14px 20px;
color: #FFF;
font-size: 17px;
text-decoration: none;
border: none;
}
.toggle:hover {
background-color: #000000;
}
/* Display Dropdown when clicked on Parent Lable */
[id^=drop]:checked+ul {
display: block;
}
/* Change menu item's width to 100% */
nav ul li {
display: block;
width: 100%;
}
nav ul ul .toggle,
nav ul ul a {
padding: 0 40px;
}
nav ul ul ul a {
padding: 0 80px;
}
nav a:hover,
nav ul ul ul a {
background-color: #000000;
}
nav ul li ul li .toggle,
nav ul ul a,
nav ul ul ul a {
padding: 14px 20px;
color: #FFF;
font-size: 17px;
}
nav ul li ul li .toggle,
nav ul ul a {
background-color: #212121;
}
/* Hide Dropdowns by Default */
nav ul ul {
float: none;
position: static;
color: #ffffff;
/* has to be the same number as the "line-height" of "nav a" */
}
/* Hide menus on hover */
nav ul ul li:hover>ul,
nav ul li:hover>ul {
display: none;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
display: block;
width: 100%;
}
nav ul ul ul li {
position: static;
/* has to be the same number as the "width" of "nav ul ul li" */
}
}
#media all and (max-width : 330px) {
nav ul li {
display: block;
width: 94%;
}
}
<nav>
That's it thanks for the help !
The easiest way is to add for example z-index: 2 for the nav a selector. This is because nav ul has padding on top and overlaps the element.

CSS Menu Toggle isn't correct color when viewed in smaller screenHi

I have been trying to make this responsive CSS/HTML menu work, and it's displayed in the colors I want when it's in full screen/normal desktop view, but when I view it in "responsive" view e.g a phone resolution, the menu colors aren't displaying the right way...
As per the full screen version the colors should be as follows
menu text - #278189
background - #fff
hover background - #3A3A3A
I have trawled through the code time after time but cannot see why this isn't working? for some other strange reason "home" "about" and "contact" have a black hover background.
Another minor issue (not massively bothered but would be nice to know) is when in full screen there is a "home" button which will link to the index page, but in the smaller version there is a "menu" button too...?
TIA
Dan
<html>
<head>
<meta charset="utf-8">
<title>Primeheat | Chichester Plumbing & Heating Installation,
Maintenance & Emergency Breakdown</title>
<meta name="viewport" content="width=device-width">
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<nav>
<div id="logo">Your Logo here</div>
<label for="drop" class="toggle">Menu</label>
<input type="checkbox" id="drop" />
<ul class="menu">
<li>Home</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-1" class="toggle">Plumbing</label>
Plumbing
<input type="checkbox" id="drop-1"/>
<ul>
<li>Installation</li>
<li>Service</li>
<li>Breakdown</li>
</ul>
</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-2" class="toggle">Heating</label>
Heating
<input type="checkbox" id="drop-2"/>
<ul>
<li>Installation</li>
<li>Service</li>
<li>Service</li>
<li>
</ul>
</li>
<li>Contact</li>
<li>About</li>
</ul>
</nav>
<div id="navbar2" div id class="navbar2">
<h1 class="h1">Welcome to Primeheat Plumbing & Heating</h1>
</div>
</body>
</html>
body {
background: #212121;
font-size:22px;
line-height: 32px;
color: #278189;
margin: 0;
padding: 0;
word-wrap:break-word !important;
font-family: 'Open Sans', sans-serif;
}
h1 {
font-size: 60px;
text-align: center;
color: #278189;
}
h3 {
font-size: 30px;
line-height: 34px;
text-align: center;
color: ##278189;;
}
h3 a {
color: ##278189;;
}
a {
color: ##278189;;
}
h1 {
margin-top: 100px;
text-align:center;
font-size:60px;
line-height: 70px;
font-family: 'Bree Serif', 'serif';
}
#container {
margin: 0 auto;
max-width: 890px;
}
p {
text-align: center;
}
.toggle,
[id^=drop] {
display: none;
}
/* Giving a background-color to the nav container. */
nav {
margin:0;
padding: 0;
background-color: #FFFFFF;
}
#logo {
display: block;
padding: 0 30px;
float: left;
font-size:20px;
line-height: 60px;
}
/* Since we'll have the "ul li" "float:left"
* we need to add a clear after the container. */
nav:after {
content:"";
display:table;
clear:both;
}
/* Removing padding, margin and "list-style" from the "ul",
* and adding "position:reltive" */
nav ul {
float: right;
padding:0;
margin:0;
list-style: none;
position: relative;
}
/* Positioning the navigation items inline */
nav ul li {
margin: 0px;
display:inline-block;
float: left;
background-color: #fff;
}
/* Styling the links */
nav a {
display:block;
padding:14px 20px;
color:#278189;
font-size:17px;
text-decoration:none;
}
nav ul li ul li:hover { background: #3A3A3A; }
/* Background color change on Hover */
nav a:hover {
background-color: #3A3A3A;
}
/* Hide Dropdowns by Default
* and giving it a position of absolute */
nav ul ul {
display: none;
position: absolute;
/* has to be the same number as the "line-height" of "nav a" */
top: 60px;
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:inherit;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
width:170px;
float:none;
display:list-item;
position: relative;
}
/* Second, Third and more Tiers
* We move the 2nd and 3rd etc tier dropdowns to the left
* by the amount of the width of the first tier.
*/
nav ul ul ul li {
position: relative;
top:-60px;
/* has to be the same number as the "width" of "nav ul ul li" */
left:170px;
}
/* Change ' +' in order to change the Dropdown symbol */
li > a:after { content: ' +'; }
li > a:only-child:after { content: ''; }
/* Media Queries
-------------------------------------------- */
#media all and (max-width : 768px) {
#logo {
display: block;
padding: 0;
width: 100%;
text-align: center;
float: none;
}
nav {
margin: 0;
}
/* Hide the navigation menu by default */
/* Also hide the */
.toggle + a,
.menu {
display: none;
}
/* Stylinf the toggle lable */
.toggle {
display: block;
background-color: #FFF;
padding:14px 20px;
color:##278189;
font-size:17px;
text-decoration:none;
border:none;
}
.toggle:hover {
background-color: #278189;
}
/* Display Dropdown when clicked on Parent Lable */
[id^=drop]:checked + ul {
display: block;
}
/* Change menu item's width to 100% */
nav ul li {
display: block;
width: 100%;
}
nav ul ul .toggle,
nav ul ul a {
padding: 0 40px;
}
nav ul ul ul a {
padding: 0 80px;
}
nav a:hover,
nav ul ul ul a {
background-color: #000000;
}
nav ul li ul li .toggle,
nav ul ul a,
nav ul ul ul a{
padding:14px 20px;
color:#464646;
font-size:17px;
}
nav ul li ul li .toggle,
nav ul ul a {
background-color: #3a3a3a;
}
/* Hide Dropdowns by Default */
nav ul ul {
float: none;
position:static;
color: #278189;
/* has to be the same number as the "line-height" of "nav a" */
}
/* Hide menus on hover */
nav ul ul li:hover > ul,
nav ul li:hover > ul {
display: none;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
display: block;
width: 100%;
}
nav ul ul ul li {
position: static;
/* has to be the same number as the "width" of "nav ul ul li" */
}
}
#media all and (max-width : 330px) {
nav ul li {
display:block;
width: 94%;
}
}
Before going into your stated problem, I would like to point out some obvious mistakes in your code. First mistake, you typed some of the color hex codes wrongly. You type extra hashtags and semicolons. Not only this h3 tag, but others as well. Check your code properly.
h3 {
font-size: 30px;
line-height: 34px;
text-align: center;
color: ##278189;; /* should be #278189; */
}
Second mistake, why do you style the same tag (h1) twice? I can understand you style it twice if one of them is either inside a media query or nested under other tags or classes/ids, but it's not.
h1 {
font-size: 60px;
text-align: center;
color: #278189;
}
h1 {
margin-top: 100px;
text-align:center;
font-size:60px;
line-height: 70px;
font-family: 'Bree Serif', 'serif';
}
Ok, to solve your problem:
You mentioned that menu colors aren't displaying correctly in mobile resolution.
Under the media query #media all and (max-width : 768px), you have something like this below. Just change it to the color that you want, which is #3A3A3A.
.toggle:hover {
background-color: #278189; /* it's using the menu text color. change this to the color you want, which is #3A3A3A */
}
If you want the nav items to have the same hover background color, change this as well, also under the same media query.
nav a:hover,
nav ul ul ul a {
background-color: #000000; /* #000000 is black color. Change this */
}
If the submenu text is meant to have the #278189 too, change this.
nav ul li ul li .toggle,
nav ul ul a,
nav ul ul ul a{
padding:14px 20px;
color: #464646; /* change this too... */
font-size:17px;
}
Another minor issue (not massively bothered but would be nice to know) is when in full screen there is a "home" button which will link to the index page, but in the smaller version there is a "menu" button too...?
The "menu" button that you mention is for collapsing the dropdown menu on mobile resolution. You click it, the dropdown menu will be shown. It's from this line of html code:
<label for="drop" class="toggle">Menu</label>
If you were to remove this whole line, you can't get the dropdown menu on mobile resolution anymore, as the dropdown css code are all handled by the toggle class.
Basically, your mentioned problems aren't big. If you're not sure where does the color come from, why is it showing the wrong color, etc, you can always use inspect element from your browser to look for it (read here for more info if you don't know much about inspect element. Also, always make sure your code is free from syntax errors (read more about css syntax), like the color hex codes like I mention above.

Third drop down menu not showing up on mobile resize

I can't figure out why my third drop down menu is not working, the first two drop down menu are working perfectly on desktop, tablet and mobile resize, but the third drop down menu only works on desktop, but not on mobile. Any help will be appreciated thanks.This is my nav menu html code:
.toggle,
[id^=drop] {
display: none;
}
/* Giving a background-color to the nav container. */
nav {
margin:0;
padding: 0;
background-color: #000000;
}
/* Since we'll have the "ul li" "float:left"
* we need to add a clear after the container. */
nav:after {
content:"";
display:table;
clear:both;
}
/* Removing padding, margin and "list-style" from the "ul",
* and adding "position:reltive" */
nav ul {
display: flex;
justify-content: center;
padding:0;
margin:0;
list-style: none;
position: relative;
}
/* Positioning the navigation items inline */
nav ul li {
margin: 0px;
display:inline-block;
float: left;
background-color: #000000;
}
/* Styling the links */
nav a {
display:block;
padding:14px 20px;
color:#FFF;
font-size:17px;
text-decoration:none;
line-height: 32px;
}
nav ul li ul li:hover { background: #000000; }
/* Background color change on Hover */
nav a:hover {
background-color: #FFC213;
}
/* Hide Dropdowns by Default
* and giving it a position of absolute */
nav ul ul {
display: none;
position: absolute;
/* has to be the same number as the "line-height" of "nav a" */
top: 60px;
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:inherit;
z-index: 3;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
width:170px;
float:none;
display:list-item;
position: relative;
}
/* Second, Third and more Tiers
* We move the 2nd and 3rd etc tier dropdowns to the left
* by the amount of the width of the first tier.
*/
nav ul ul ul li {
position: relative;
top:-60px;
/* has to be the same number as the "width" of "nav ul ul li" */
left:170px;
}
/* Change ' +' in order to change the Dropdown symbol */
li > a:after { content: ' +'; }
li > a:only-child:after { content: ''; }
/* Media Queries
--------------------------------------------- */
#media all and (max-width : 768px) {
nav {
margin: 0;
}
/* Hide the navigation menu by default */
/* Also hide the */
.toggle + a,
.menu {
display: none;
}
/* Stylinf the toggle lable */
.toggle {
display: block;
background-color: #000000;
padding:14px 20px;
color:#FFF;
font-size:17px;
text-decoration:none;
border:none;
}
.toggle:hover {
background-color: #FFC213;
}
/* Display Dropdown when clicked on Parent Lable */
[id^=drop]:checked + ul {
display: block;
}
/* Change menu item's width to 100% */
nav ul li {
display: block;
width: 100%;
}
nav ul ul .toggle,
nav ul ul a {
padding: 0 40px;
}
nav ul ul ul a {
padding: 0 80px;
}
nav a:hover,
nav ul ul ul a {
background-color: #FFC213;
}
nav ul li ul li .toggle,
nav ul ul a,
nav ul ul ul a{
padding:14px 20px;
color:#FFF;
font-size:17px;
}
nav ul li ul li .toggle,
nav ul ul a {
background-color: #212121;
}
/* Hide Dropdowns by Default */
nav ul ul {
float: none;
position:static;
color: #ffffff;
/* has to be the same number as the "line-height" of "nav a" */
}
/* Hide menus on hover***********effect changes on mobile review */
nav ul ul li:hover > ul,
nav ul li:hover > ul {
display: none;
/*****mobile dorpdown code*********z-index:0;***********/
}
/* Fisrt Tier Dropdown */
nav ul ul li {
display: block;
width: 100%;
}
nav ul ul ul li {
position: static;
/* has to be the same number as the "width" of "nav ul ul li" */
}
}
#media all and (max-width : 150px) {
nav ul li {
display:block;
width: 94%;
}
}
<nav>
<label for="drop" class="toggle">Menu</label>
<input type="checkbox" id="drop" />
<ul class="menu">
<li>Home</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-1" class="toggle">About Us +</label>
About Us
<input type="checkbox" id="drop-1" />
<ul>
<li>Methodology</li>
</ul>
</li>
<li>Services</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-2" class="toggle">List of Courses +</label>
List of Courses
<input type="checkbox" id="drop-2" />
<ul>
<li>Administration</li>
<li>Agriculture</li>
</ul>
</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-2" class="toggle">Ongoing Courses +</label>
Ongoing Courses
<input type="checkbox" id="drop-2" />
<ul>
<li>Creative</li>
<li>Enterprise</li>
<li>Microsoft excel</li>
<li>Hardware</li>
<li>Management</li>
<li>Microsoft Office</li>
<li>Networking</li>
<li>Web design</li>
<li>Web development</li>
</ul>
</li>
<li>Events</li>
<li>Contact</li>
<li>
<img src="images/soc/facebook1.png" height="32" width="32">
</li>
<li>
<img src="images/soc/twitter1.png" height="32" width="32">
</li>
<li>
<img src="images/soc/instagram1.png" height="32" width="34">
</li>
</ul>
</nav>
You are using "drop-2" for both the second and third dropdown. Change it to for="drop-3" and id="drop-3" for the third menu, and it will work.
.toggle,
[id^=drop] {
display: none;
}
/* Giving a background-color to the nav container. */
nav {
margin:0;
padding: 0;
background-color: #000000;
}
/* Since we'll have the "ul li" "float:left"
* we need to add a clear after the container. */
nav:after {
content:"";
display:table;
clear:both;
}
/* Removing padding, margin and "list-style" from the "ul",
* and adding "position:reltive" */
nav ul {
display: flex;
justify-content: center;
padding:0;
margin:0;
list-style: none;
position: relative;
}
/* Positioning the navigation items inline */
nav ul li {
margin: 0px;
display:inline-block;
float: left;
background-color: #000000;
}
/* Styling the links */
nav a {
display:block;
padding:14px 20px;
color:#FFF;
font-size:17px;
text-decoration:none;
line-height: 32px;
}
nav ul li ul li:hover { background: #000000; }
/* Background color change on Hover */
nav a:hover {
background-color: #FFC213;
}
/* Hide Dropdowns by Default
* and giving it a position of absolute */
nav ul ul {
display: none;
position: absolute;
/* has to be the same number as the "line-height" of "nav a" */
top: 60px;
}
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
display:inherit;
z-index: 3;
}
/* Fisrt Tier Dropdown */
nav ul ul li {
width:170px;
float:none;
display:list-item;
position: relative;
}
/* Second, Third and more Tiers
* We move the 2nd and 3rd etc tier dropdowns to the left
* by the amount of the width of the first tier.
*/
nav ul ul ul li {
position: relative;
top:-60px;
/* has to be the same number as the "width" of "nav ul ul li" */
left:170px;
}
/* Change ' +' in order to change the Dropdown symbol */
li > a:after { content: ' +'; }
li > a:only-child:after { content: ''; }
/* Media Queries
--------------------------------------------- */
#media all and (max-width : 768px) {
nav {
margin: 0;
}
/* Hide the navigation menu by default */
/* Also hide the */
.toggle + a,
.menu {
display: none;
}
/* Stylinf the toggle lable */
.toggle {
display: block;
background-color: #000000;
padding:14px 20px;
color:#FFF;
font-size:17px;
text-decoration:none;
border:none;
}
.toggle:hover {
background-color: #FFC213;
}
/* Display Dropdown when clicked on Parent Lable */
[id^=drop]:checked + ul {
display: block;
}
/* Change menu item's width to 100% */
nav ul li {
display: block;
width: 100%;
}
nav ul ul .toggle,
nav ul ul a {
padding: 0 40px;
}
nav ul ul ul a {
padding: 0 80px;
}
nav a:hover,
nav ul ul ul a {
background-color: #FFC213;
}
nav ul li ul li .toggle,
nav ul ul a,
nav ul ul ul a{
padding:14px 20px;
color:#FFF;
font-size:17px;
}
nav ul li ul li .toggle,
nav ul ul a {
background-color: #212121;
}
/* Hide Dropdowns by Default */
nav ul ul {
float: none;
position:static;
color: #ffffff;
/* has to be the same number as the "line-height" of "nav a" */
}
/* Hide menus on hover***********effect changes on mobile review */
nav ul ul li:hover > ul,
nav ul li:hover > ul {
display: none;
/*****mobile dorpdown code*********z-index:0;***********/
}
/* Fisrt Tier Dropdown */
nav ul ul li {
display: block;
width: 100%;
}
nav ul ul ul li {
position: static;
/* has to be the same number as the "width" of "nav ul ul li" */
}
}
#media all and (max-width : 150px) {
nav ul li {
display:block;
width: 94%;
}
}
<nav>
<label for="drop" class="toggle">Menu</label>
<input type="checkbox" id="drop" />
<ul class="menu">
<li>Home</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-1" class="toggle">About Us +</label>
About Us
<input type="checkbox" id="drop-1" />
<ul>
<li>Methodology</li>
</ul>
</li>
<li>Services</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-3" class="toggle">List of Courses +</label>
List of Courses
<input type="checkbox" id="drop-3" />
<ul>
<li>Administration</li>
<li>Agriculture</li>
</ul>
</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-2" class="toggle">Ongoing Courses +</label>
Ongoing Courses
<input type="checkbox" id="drop-2" />
<ul>
<li>Creative</li>
<li>Enterprise</li>
<li>Microsoft excel</li>
<li>Hardware</li>
<li>Management</li>
<li>Microsoft Office</li>
<li>Networking</li>
<li>Web design</li>
<li>Web development</li>
</ul>
</li>
<li>Events</li>
<li>Contact</li>
<li>
<img src="images/soc/facebook1.png" height="32" width="32">
</li>
<li>
<img src="images/soc/twitter1.png" height="32" width="32">
</li>
<li>
<img src="images/soc/instagram1.png" height="32" width="34">
</li>
</ul>
</nav>

Collapsible nav bar not working as expected and creating unnecessary padding (Not using bootstrap)

I am trying to create a simple navigation bar which when resized (made smaller) will create a small menu which once clicked will show the links, much like Bootstrap.
Issue 1: When resized the links do disappear, as intended, but when I expand the mini menu (click show navigation), the links (link 1, link 2 etc) show up on my logo and just in general off place ( I want the links to show up neatly, like the image below).
Issue 2: As you will see with image below, the code I have, for some reason causes a gap to emerge between each <li>.
Here is a fiddle created to show the issues and the code used: https://jsfiddle.net/n00jg7xy/
FOr the second problem, about padding, the problem is here:
#nav > ul > li{
width: 25%; //Here you are forcing to 1/4 of the space, remove this line
height: 100%;
float: left;
}
I dont know if the snippet size is going to let you see the change, here is the fiddle.
#wrapper{
width: 100%;
height: auto;
}
#header{
height: auto;
border-bottom: 1px solid black;
}
#logo {
float: left;
}
/******************* Main Navigation ************************/
.mainNav{
margin: 10px;
}
#nav > a{
display: none;
}
#nav li{
position: relative;
}
#nav > ul{
height: 3.75em;
}
#nav > ul > li{
height: 100%;
float: left;
}
#nav li ul{
display: none;
position: absolute;
top: 100%;
}
#nav li:hover ul{
display: block;
}
#media only screen and ( max-width: 40em ){
#nav{
position: relative;
}
#nav > a{
}
#nav:not( :target ) > a:first-of-type,
#nav:target > a:last-of-type
{
display: block;
}
/* first level */
#nav > ul
{
height: auto;
display: none;
position: absolute;
left: 0;
right: 0;
}
#nav:target > ul
{
display: block;
}
#nav > ul > li
{
width: 100%;
float: none;
}
/* second level */
#nav li ul
{
position: static;
}
}
/***********************/
/* Remove margins and padding from the list*/
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
/* Float the list items side by side */
ul.topnav li {
float: left;
}
/* Style the links inside the list items */
ul.topnav li a {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 14px;
}
/* Change background color of links on hover */
ul.topnav li a:hover {
background-color: #f9f9f9;
color: darkred;
transition: 0.3s;
}
/* Hide the list item that contains the link that should open and close the topnav on small screens */
ul.topnav li.icon {
display: none;
}
/* When the screen is less than 680 pixels wide, hide all list items, except for the first one ("Home"). Show the list item that contains the link to open and close the topnav (li.icon) */
#media screen and (max-width:680px) {
ul.topnav li:not(:first-child) {display: none;}
ul.topnav li.icon {
float: right;
display: inline-block;
}
}
/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens */
#media screen and (max-width:680px) {
ul.topnav.responsive {position: relative;}
ul.topnav.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li {
float: none;
display: inline;
}
ul.topnav.responsive li a {
display: block;
text-align: left;
}
}
<div id="wrapper">
<p>
Resize the window
</p>
<div id="header">
<div><img id="logo" src="http://i65.tinypic.com/352i0jq.jpg" alt="logo" href="#"> </div>
<div class="mainNav">
<nav id="nav" role="navigation">
Show navigation
Hide navigation
<ul class="topnav" id="myTopnav">
<li>Home</li>
<li>Link 1</li>
<li>Link 2</li>
<li> Link 3</li>
<li> Link 4</li>
</ul>
</nav>
</div>
</div>
</div>
<!-- wrapper closed-->

Navbar in HTML/CSS not functioning properly

I am new to website design, and there are a few flaws in my navbar that I cannot fix.
I cannot get the navbar to center properly.
When the screen resolution changes, the list overflows into the next line.
there is 1 list element that is sized differently and I cannot seem to figure out why.
Here is the code:
https://jsfiddle.net/b02nm6ae/#update
CSS:
.nav_wrapper {
z-index: 9999;
padding: 0;
margin: 0;
width: 100%;
min-width: 50px;
}
.nav_wrapper ul {
display: block;
position: relative;
position: fixed;
/* fixes automatic values set by ul */
margin: 0;
padding: 0;
}
.nav_wrapper ul li {
list-style: none;
display: list-item;
background-color: #993300;
float: left;
}
/* hides the submenu by default */
.nav_wrapper ul ul {
display: none;
position: absolute;
}
/* makes the sub menu appear on hover over list element */
.nav_wrapper ul li:hover > .sub_nav1 {
display: list-item;
list-style: none;
}
/* lists the list items on top of one another */
.nav_wrapper ul .sub_nav1 li {
float: none;
position: relative;
}
.nav_wrapper ul li a{
display: block;
text-decoration: none;
color: #ffffff;
padding: 12px;
}
.nav_wrapper li a:hover{
color: #000;
background-color: #ffffff;
}
/* Dropdown Menu arrow */
.nav_wrapper ul li > a:after {
content: '\25BE';
}
.nav_wrapper ul li > a:only-child:after {
content: '';
}
HTML:
<body>
<!-- NAV -->
<div class="nav_wrapper">
<ul class="nav">
<li>Home</li>
<li>Calandar</li>
<li>About Us
<ul class="sub_nav1">
<li>The Pastor</li>
<li>History</li>
<li>About Byzantines</li>
</ul>
</li>
<li>Mass Times</li>
<li>Contact Us</li>
</ul>
<div>
<!-- SECTION 1 -->
</body>
</html>
Once you float the li then centering becomes problematical. In these instances, it's often preferred to use display:inline-block and center then by applying text-align:center to the parent ul.
This does have a white-space downside but there are methods around that, one of which (font-size) I have used here.
As for the single element with the greater height...that was caused by the pseudo-element...so slapped a quick patch over it. Frankly, I would be applying a class to the parent li and using a pseudo-element on the li but that's another debate entirely.
body {
font-family: 'Didact Gothic', sans-serif;
padding: 0;
margin: 0;
background-color: #CCCCFF;
}
.nav_wrapper ul {
display: block;
margin: 0;
padding: 0;
text-align: center;
font-size: 0;
/* remove whitespace */
}
.nav_wrapper ul li {
list-style: none;
display: inline-block;
vertical-align: top;
background-color: #993300;
position: relative;
font-size: 1rem;
/* font-size reset */
}
/* hides the submenu by default */
.nav_wrapper ul ul {
display: none;
top: 100%;
left: 0;
position: absolute;
}
/* makes the sub menu appear on hover over list element */
.nav_wrapper ul li:hover > .sub_nav1 {
display: block;
width: 100%;
}
/* lists the list items on top of one another */
.nav_wrapper ul .sub_nav1 li {
position: relative;
width: 100%;
}
.nav_wrapper ul li a {
display: block;
text-decoration: none;
color: #ffffff;
padding: 12px;
}
.nav_wrapper li a:hover {
color: #000;
background-color: #ffffff;
}
/* Dropdown Menu arrow */
.nav_wrapper ul> li > a:after {
content: '\25BE';
line-height: 0;
}
.nav_wrapper ul li > a:only-child:after {
content: '';
}
<div class="nav_wrapper">
<ul class="nav">
<li>Home
</li>
<li>Calendar
</li>
<li>About Us
<ul class="sub_nav1">
<li>The Pastor
</li>
<li>History
</li>
<li>About Byzantines
</li>
</ul>
</li>
<li>Mass Times
</li>
<li>Contact Us
</li>
</ul>
<div>
Well I notice that if I set a 25 pixel height to
.nav_wrapper ul li a
that removes the extra space for example..
.nav_wrapper ul li a{
height:25px;
display: block;
text-decoration: none;
color: #ffffff;
padding: 12px;
}
https://jsfiddle.net/b02nm6ae/9/