I'm trying to make a responsive menu without using bootstrap but i just can't make it
<ul class="nav">
<li class="active">Border</li>
<li>Gradient</li>
<li>Transform</li>
<li>Animations</li>
<li>Transition</li>
<li>Text Shadow</li>
<li>Box Shadow</li>
<li>Font-Face</li>
<li>RGBA</li>
</ul>
So this is the menu and on a specific viewport width it shoud become a drop down. Can you help me?
I have done my best to use javascript as less as possible and to fit to your markups. Hope it will help you!
document.querySelector(".nav").addEventListener("click", function(evt) {
console.log("nav");
if (!this.classList.contains("open")) {
this.classList.add("open");
this.classList.remove("closed");
} else {
this.classList.remove("open");
this.classList.add("closed");
}
evt.stopPropagation();
});
document.querySelector("body").addEventListener("click", function() {
var nav = document.querySelector(".nav");
if (!nav.classList.contains("open")) {
nav.classList.add("open");
nav.classList.remove("closed");
} else {
nav.classList.remove("open");
nav.classList.add("closed");
}
});
var li_elements = document.querySelectorAll(".nav li");
console.log(li_elements);
[].forEach.call(li_elements, function(li_element) {
li_element.addEventListener("click", function() {
console.log("remove actove");
document.querySelector(".nav li.active").classList.remove("active");
this.classList.add("active");
});
});
document.querySelector(".nav li.active").addEventListener("click", function(evt) {
evt.preventDefault();
});
.nav {
border: solid 1px #aaa;
background-image: linear-gradient(to bottom, #ddd, #eee);
height: 1.5em;
font-size: 1em;
padding: 0.10em 0.5em;
cursor: pointer;
width: 200px;
}
.nav.closed li.active:before {
content: "";
float: right;
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 10px solid #999;
margin-top: 10px
}
.nav li {
display: none;
}
.nav.open {
height: auto;
background: white;
}
.nav li {
border-bottom: solid 1px #ddd;
padding: 0;
margin: 0
}
.nav li a {
text-decoration: none;
color: #666;
display: block;
margin: 0;
padding: 0.25em;
}
.nav.open li {
display: block;
}
.nav.open li:hover {
background: #eee;
}
.nav.closed li.active {
display: block;
color: white;
border:none;
}
<ul class="nav closed">
<li class="active">Border
</li>
<li>Gradient
</li>
<li>Transform
</li>
<li>Animations
</li>
<li>Transition
</li>
<li>Text Shadow
</li>
<li>Box Shadow
</li>
<li>Font-Face
</li>
<li>RGBA
</li>
</ul>
Related
I am creating a list using HTML and CSS. What I am trying to achieve is all the elements will be black until they are hovered on. When hovered, all elements except the hovered text will change color. I am attaching my code; it would be helpful if anyone could point out the mistake or tell me why it's not working.
.sub-menu-mast {
line-height: 30px;
background-color: rgb(254, 254, 254);
width: 9rem;
cursor: pointer;
}
.sub-menu-mast>li>a {
color: black;
position: relative;
left: 10%;
}
.sub-menu-mast a:not(:hover) {
color: red;
}
<nav>
<ul>
<li>
<a>XYZ </a>
<ul class="sub-menu-mast">
<li>Track</li>
<li>Return</li>
</ul>
</li>
</ul>
</nav>
Not sure, if I understood your request. Do you mean something like this?
.sub-menu-mast {
line-height: 30px;
background-color: rgb(254, 254, 254);
width: 9rem;
cursor: pointer;
}
.nav,
.menu,
.sub-menu-mast {
outline: 1px dashed teal;
}
.sub-menu-mast>li>a {
color: black;
position: relative;
left: 10%;
}
.sub-menu-mast li a {
outline: 1px dashed red;
}
.sub-menu-mast:hover a:not(a:hover) {
color: red;
}
* {
box-sizing: border-box;
}
.my-nav {
width: fit-content;
display: flex;
flex-flow: row nowrap;
outline: 1px dashed teal;
}
.my-nav ul {
padding: 0;
margin: 0;
}
.my-nav li {
width: 100%;
}
.my-nav a {
color: black;
display: block;
padding: 5px;
border-bottom: 1px solid black;
}
.my-sub-menu li a {
padding-left: 2em;
}
.my-nav li:hover,
.my-sub-menu li:hover {
background: wheat;
}
/* either this ... */
.my-sub-menu:hover li a:not(:hover) {
color: red;
}
/* ... or that */
/*
.my-sub-menu:hover li a {
color: red;
}
.my-sub-menu:hover li a:hover {
color: black;
}*/
<nav class="nav">
<ul class="menu">
<li>
<a>XYZ </a>
<ul class="sub-menu-mast">
<li>Track</li>
<li>Return</li>
<li>More</li>
<li>Fixed</li>
</ul>
</li>
</ul>
</nav>
<nav class="my-nav">
<ul class="my-menu">
<li>Item A</li>
<ul class="my-sub-menu">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</ul>
</nav>
I'm trying to design the nav bar of the website I'm building. I include the code below, but I cannot figure out how to get the nav bar options to appear like a folder tab. See this image:
I don't understand how to do this with HTML and CSS. What I have at the moment is:
HTML
nav {
margin: 0px;
}
ul {
list-style-type: none;
color: lightskyblue;
background-color: royalblue;
font-family: Helvetica;
font-size: 180%;
margin: 0px;
}
li {
display: inline-block;
background-color: white;
padding-top: 2px;
border: 5px;
border-colour: red;
}
a {
display: block;
color: lightskyblue;
}
li a {
text-decoration: none;
padding-top: 8px;
padding-left: 0px;
padding-right: 0px;
}
li a:hover {
color: white;
background-color: lightskyblue;
text-decoration: none;
}
<nav>
<ul>
<li>
<a class="active" href="http://www.google.com/ncr">Home</a>
</li>
<li> Full list of Articles</li>
<li> Disciplines</li>
<li> More Resources</li>
<li> <a class="about" href="http://www.google.com/ncr">About Me</a></li>
</ul>
</nav>
In the below, I have created an offsett on the main 'ul' and set the 'li' items above it using "top: -41px" and "position: relative".
Then, using JavaScript, I added eventlisteners to each of the list items, that when hovered over will change the 'ul' background color.
Obviously the styling needs some work but that is something you should try to do yourself.
Please see below:
//event listeners for the 'hovers'
document.getElementById("firstitem").addEventListener("mouseover", function() {
document.getElementById("list").style.backgroundColor = "royalblue";
});
document.getElementById("seconditem").addEventListener("mouseover", function() {
document.getElementById("list").style.backgroundColor = "pink";
});
document.getElementById("thirditem").addEventListener("mouseover", function() {
document.getElementById("list").style.backgroundColor = "green";
});
document.getElementById("fourthitem").addEventListener("mouseover", function() {
document.getElementById("list").style.backgroundColor = "red";
});
document.getElementById("fifthitem").addEventListener("mouseover", function() {
document.getElementById("list").style.backgroundColor = "yellow";
});
* {
margin: 0px;
padding: 0px;
}
nav {
margin: 0px;
}
ul {
list-style-type: none;
color: lightskyblue;
background-color: royalblue;
font-family: Helvetica;
font-size: 180%;
margin: 0px;
margin-top: 41px;
}
li {
display: inline-block;
background-color: white;
padding-top: 0px;
border: 5px;
top: -41px;
padding: 0px 5px;
position: relative;
border-color: red;
}
li:first-child {
background-color: royalblue;
}
li:nth-child(2) {
background-color: pink;
}
li:nth-child(3) {
background-color: green;
}
li:nth-child(4) {
background-color: red;
}
li:nth-child(5) {
background-color: yellow;
}
a {
display: block;
color: rgb(108, 162, 196);
}
li a {
text-decoration: none;
padding-top: 8px;
padding-left: 0px;
padding-right: 0px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<nav>
<ul id="list">
<li id="firstitem">
<a class="active" href="http://www.google.com/ncr">Home</a>
</li>
<li id="seconditem"> Full list of Articles</li>
<li id="thirditem"> Disciplines</li>
<li id="fourthitem"> More Resources</li>
<li id="fifthitem"> <a class="about" href="http://www.google.com/ncr">About Me</a></li>
</ul>
</nav>
<script src="scripts.js"></script>
</body>
</html>
I did some research with font size and boxes sizes. I created ul li and styling them with some CSS to make them looks like a table and stay inline. after the epic code i realize that each words are different like home, tutorial, contact us, and registration. all font have a different sizes and different paddings to use.
so this is my code...
i tidy this up so it's not looks like in my localhost. what i did was made the percentage like 3.5754% to set the padding to get the exact SAME width of the boxes. so 4 of them should be the same size of boxes.
at home i created padding like 5% or so.
at tutorial i created padding like 3.something%
and etc.
because each words has a different width and number of letters.
sorry for my English, please let me know if i'm confusing u guys..
but please help about this percentage and font thing. i really don't get it. if u guys have some article to read it's also help... because i'm still looking for it. ty guys :)
body {
margin: 0;
padding: 0;
}
.navigation {
top: 0;
width: 90%;
height: 120px;
background: #ccc;
margin: 0 auto;
text-align: center;
}
.nav-head {
padding-top: 5%;
padding-left: 0;
width: 100%;
margin:
/*0 7.2%*/
;
}
.nav a {
color: #e74c3c;
text-decoration: none;
font-size: 1em;
}
.nav {
list-style: none;
display: inline;
border: solid 1px #e74c3c;
margin-left: 0;
}
#nav1 {
padding: 1% 1%;
}
#nav2 {
padding: 1% 5.74%;
}
#nav3 {
padding: 1% 3%;
}
#nav4 {
padding: 1% 3%;
}
.nav:hover {
padding: 2.2% 5%;
border: solid 1px #e74c3c;
}
.nav.currentpage {
padding: 2.2% 5%;
border: solid 1px #e74c3c;
}
<nav class="navigation">
<ul class="nav-head">
<li class="nav" id="nav1">HOME
</li>
<li class="nav" id="nav2">REGISTRATION
</li>
<li class="nav" id="nav3">TUTORIAL
</li>
<li class="nav" id="nav4">CONTACT US
</li>
</ul>
</nav>
2 easy option I believe:
1) display:table + table-layout:fixed;
body {
margin: 0;
padding: 0;
}
.navigation {
width: 90%;
height: 120px;
background: #ccc;
margin: 0 auto;
text-align: center;
}
.nav-head {
display:table;
width:100%;
padding:0;
table-layout:fixed;
}
.nav a {
color: #e74c3c;
text-decoration: none;
font-size: 1em;
}
.nav {
display:table-cell;
border: solid 1px #e74c3c;
}
#nav1 {
}
#nav2 {
}
#nav3 {
}
#nav4 {
}
.nav:hover {
border: solid 1px ;
}
.nav.currentpage {
border: solid 1px ;
}
<nav class="navigation">
<ul class="nav-head">
<li class="nav" id="nav1">HOME
</li>
<li class="nav" id="nav2">REGISTRATION
</li>
<li class="nav" id="nav3">TUTORIAL
</li>
<li class="nav" id="nav4">CONTACT US
</li>
</ul>
</nav>
2) display:flex + flex:1;
body {
margin: 0;
padding: 0;
}
.navigation {
width: 90%;
height: 120px;
background: #ccc;
margin: 0 auto;
text-align: center;
}
.nav-head {
display:flex;
padding:0;
list-style-type:none;
}
.nav a {
color: #e74c3c;
text-decoration: none;
font-size: 1em;
}
.nav {
flex:1;
border: solid 1px #e74c3c;
}
#nav1 {
}
#nav2 {
}
#nav3 {
}
#nav4 {
}
.nav:hover {
border: solid 1px ;
}
.nav.currentpage {
border: solid 1px ;
}
<nav class="navigation">
<ul class="nav-head">
<li class="nav" id="nav1">HOME
</li>
<li class="nav" id="nav2">REGISTRATION
</li>
<li class="nav" id="nav3">TUTORIAL
</li>
<li class="nav" id="nav4">CONTACT US
</li>
</ul>
</nav>
Using percentages is not always the best idea, specially for padding. Instead, use min-width, a normal padding and control the gutter with font-size:0 on the parent and normal padding on the LIs
Here is the code:
.nav-head{
font-size:0;
}
.nav {
padding: 2px 5px !important;
min-width: 116px;
display: inline-block;
margin: 0 !important;
font-size: 15px;
border:1px solid red;
}
And here is the DEMO
Here is the explanation on the font-zero concept link
You can use Flexbox to help you in making all the items with same width.
body {
margin: 0;
padding: 0;
}
.navigation {
top: 0;
width: 90%;
height: 120px;
background: #ccc;
margin: 0 auto;
text-align: center;
}
.nav-head {
display: flex;
padding-top: 5%;
padding-left: 0;
width: 100%;
}
.nav a {
color: #e74c3c;
text-decoration: none;
font-size: 1em;
}
.nav {
list-style: none;
flex: 1;
border: solid 1px #e74c3c;
margin-left: 0;
padding-top: 2.2%;
padding-bottom: 2.2%;
}
.nav:hover {
border: solid 1px #e74c3c;
}
.nav.currentpage {
padding-top: 2.2%;
padding-bottom: 2.2%;
border: solid 1px #e74c3c;
}
<nav class="navigation">
<ul class="nav-head">
<li class="nav" id="nav1">HOME
</li>
<li class="nav" id="nav2">REGISTRATION
</li>
<li class="nav" id="nav3">TUTORIAL
</li>
<li class="nav" id="nav4">CONTACT US
</li>
</ul>
</nav>
When I hovering over the menu the color changes properly but the link will activate only when i hovering over the text i want it to be activate at the time when i just touch the block or when it changes its color.Please help me any help would be appreciated
a {
text-decoration: none;
color: #fff;
}
#navbar {
background: #204d86;
border-radius: 5px;
}
#navbar>ul {
list-style: none;
margin: 0;
padding: 0;
height: 40;
margin-left: 300px;
}
#navbar>ul>li {
float: left;
padding: 10px 35px;
border-radius: 5px;
}
#navbar>ul>li:hover {
background: #5cadff;
border-radius: 5px;
}
#navbar>ul>li>ul {
display: none;
padding: 0;
position: absolute;
background: #204d86;
border-radius: 5px;
}
#navbar>ul>li>ul>li {
padding: 0 20px;
line-height: 40px;
display: block;
background: #5cadff;
}
#navbar>ul>li:hover>ul {
display: block;
}
#navbar>ul>li>ul>li>ul {
display: none;
padding: 0 30px;
position: absolute;
}
#navbar li:hover > a {
color: #000;
}
#navbar>ul>li>ul>li:hover {
background: #204d86;
}
#navbar:after {
content: "";
clear: both;
display: table;
}
<div id="navbar">
<ul>
<li><a class="active" href="../index/myindex.html">HOME</a>
</li>
<li>STAYING HEALTHY
<ul>
<li>Diet & Weight loss
</li>
<li>Exercises
</li>
<li>Physical Activity
</li>
<li>Healthy Eating
</li>
</ul>
</li>
<li>DISEASES
<ul>
<li>Stock
</li>
<li>Osteoporosis
</li>
<li>Diabetes
</li>
<li>Heart Disease
</li>
</ul>
</li>
<li>MIND&MOOD
<ul>
<li>Depression
</li>
<li>Anxiety
</li>
<li>Addiction
</li>
<li>Stress
</li>
</ul>
</li>
</ul>
</div>
a {
text-decoration:none;
color:#fff;
display: block;
}
THEN, move any padding you had on the li to the links themselves. Adjust as required.
body {
background: #000;
}
a {
text-decoration: none;
color: #fff;
display: block;
}
#navbar {
background: #204d86;
border-radius: 5px;
}
#navbar>ul {
list-style: none;
margin: 0;
padding: 0;
height: 40px;
}
#navbar>ul>li {
float: left;
border-radius: 5px;
}
#navbar>ul>li>a {
padding: 10px 35px;
}
#navbar>ul>li:hover {
background: #5cadff;
border-radius: 5px;
}
#navbar>ul>li>ul {
display: none;
padding: 0;
position: absolute;
background: #204d86;
border-radius: 5px;
}
#navbar>ul>li>ul>li {
line-height: 40px;
display: block;
background: #5cadff;
}
#navbar>ul>li>ul>li>a {
padding: 0 20px;
}
#navbar>ul>li:hover>ul {
display: block;
}
#navbar>ul>li>ul>li>ul {
display: none;
padding: 0 30px;
position: absolute;
}
#navbar li:hover > a {
color: #000;
}
#navbar>ul>li>ul>li:hover {
background: #204d86;
}
<div id="navbar">
<ul>
<li><a class="active" href="../index/myindex.html">HOME</a>
</li>
<li>STAYING HEALTHY
<ul>
<li>Diet & Weight loss
</li>
<li>Exercises
</li>
<li>Physical Activity
</li>
<li>Healthy Eating
</li>
</ul>
</li>
<li>DISEASES
<ul>
<li>Stock
</li>
<li>Osteoporosis
</li>
<li>Diabetes
</li>
<li>Heart Disease
</li>
</ul>
</li>
</ul>
</div>
You should also validate the CSS as I spotted a couple of errors.
You have to set this in your a-tag style:
display: block
width: 100%
height: 100%
and the padding of li should be 0 px,
have a look here.
I'd like to use collapsible menu inside the sections. But it's not working, when i Add class=”collapsed” to SECTION B-B, it displays expanded without clicking. I want section 2.2 or section B-B like Drop down. What should i add or change to do that? Your help is very much appreciated.
Here's my work what i tried
JavaScript:
$(document).ready(function () {
setTimeout(function () {
// Slide
$('#menu1 > li > a.expanded + ul').slideToggle('medium');
$('#menu1 > li > a').click(function () {
$(this).toggleClass('expanded').toggleClass('collapsed').parent().find('> ul').slideToggle('medium');
});
$('#example1 .expand_all').click(function () {
$('#menu1 > li > a.collapsed').addClass('expanded').removeClass('collapsed').parent().find('> ul').slideDown('medium');
});
$('#example1 .collapse_all').click(function () {
$('#menu1 > li > a.expanded').addClass('collapsed').removeClass('expanded').parent().find('> ul').slideUp('medium');
});
}, 250);
});
HTML
<div id="example1">
<ul id="menu1" class="example_menu">
<li><a class="collapsed" href="#">SECTION A</a>
<ul>
<li>SECTION A-A</li>
<li>SECTION A-B</li>
<li>SECTION A-C</li>
</ul>
</li>
<li><a class="collapsed" href="#">SECTION B</a>
<ul>
<li>SECTION B-A</li>
<ul>
<li><a class="collapsed" href="#">SECTION B-B</a></li>
<li>SECTION B-B-1</li>
<li>SECTION B-B-2</li>
<li>SECTION B-B-3</li>
<li>SECTION B-B-4</li>
</ul>
</ul>
</li>
<li><a class="collapsed" href="#">SECTION C</a>
<ul>
<li>SECTION C-A</li>
<li>SECTION C-B</li>
<li>SECTION C-C</li>
</ul>
</li>
</ul>
</div>
CSS
body {
font: 10pt Arial, Helvetica, Sans-serif;
background-image: url();
margin: 0px;
}
a {
text-decoration: none;
}
#example1,
#example2,
#example3,
#example4,
#example5 {
float: left;
}
.expand_all,
.collapse_all {
cursor: pointer;
}
.example_menu {
font-size: 95%;
list-style: none;
margin: 0;
padding: 0;
vertical-align: top;
width: 230px;
}
.example_menu ul {
display: none;
list-style: none;
margin: 0;
padding: 0;
}
#menu1,
#menu2,
#menu3,
#menu4,
#menu5 {
margin: 0;
color: #96C;
}
#menu1 li,
#menu2 li,
#menu3 li,
#menu4 li,
#menu5 li,
.example_menu li {
background-image: none;
margin: 0;
padding: 0;
}
.example_menu ul ul {
display: block;
}
.example_menu ul ul li a {
padding-left: 20px;
width: 202px;
}
.example_menu li.header3 a {
padding-left: 34px;
width: 188px;
}
.example_menu a {
color: #000;
cursor: pointer;
display: block;
font-weight: bold;
margin-left: 0;
width: 211px;
padding-top: 18px;
padding-right: 0px;
padding-bottom: 12px;
padding-left: 19px;
}
.example_menu a.expanded {
background: #bbb url('images/collapse.gif') no-repeat 3px 61%;
}
.example_menu a.collapsed {
border-top: 1px solid #E3E3E3;
background-color: #bbb;
background-image: url(images/expand.gif);
background-repeat: no-repeat;
background-position: 3px 61%;
}
.example_menu a.normal {
background: none repeat scroll 0 0 #BBBBBB;
}
.example_menu a:hover {
text-decoration: none;
}
.example_menu ul a {
background: #e8e8e8;
border-top: 2px solid #fff;
color: #000;
display: block;
font-weight: normal;
padding: 3px 8px 2px 17px;
width: 205px;
}
.example_menu ul a:link {
font-weight: bolder;
}
.example_menu ul a:hover {
background : #f5f5f5;
text-decoration: underline;
}
.example_menu li.active a {
background: #fff;
}
.example_menu li.active li a {
background: #e8e8e8;
}
#menu1 li.footer,
#menu2 li.footer,
#menu3 li.footer,
#menu4 li.footer,
#menu5 li.footer,
.example_menu .footer {
background: transparent url('images/footer.jpg') no-repeat 0 0;
border-top: 2px solid #fff;
height: 9px;
line-height: 15px;
margin: 0 0 10px 0;
width: 131px;
}
.example_menu .footer span {
display: none;
}
Fixing your html itself seems to solve half of the issue.
You need to add the following in css
a.collapsed + ul{
display:none;
}
note: i took the click event handler outside the setTimeout which seem to be unnecessary (no need of slideToggle('medium') and timeOut to compensate it since <ul> s are hidden using css)
check this Fiddle
I didn't dive fully into your code. But this would be a start:
Change HTML structure of the second level to match the first level drop down:
<li><a class="collapsed" href="#">SECTION B</a>
<ul>
<li>SECTION B-A
</li>
<li><a class="collapsed" href="#">SECTION B-B</a>
<ul>
<li>SECTION B-B-1
</li>
<li>SECTION B-B-2
</li>
<li>SECTION B-B-3
</li>
<li>SECTION B-B-4
</li>
</ul>
</li>
</ul>
</li>
And "generalise" your click handler:
[...]
$('#menu1 li > a.expanded + ul').slideToggle('medium');
$('#menu1 li > a').click(function () {
$(this).toggleClass('expanded').toggleClass('collapsed').parent().find('> ul').slideToggle('medium');
});
[...]
Not sure about your "all" handlers. Without source you have to figure it out yourself :)