CSS3 Onclick Menu - css

I used the following code to create my menu, for learning purposes:
http://koen.kivits.com/articles/pure-css-menu/
My CSS:
html, body {
margin: 0;
padding: 0;
min-width: 1920;
}
.container {
position: relative;
display: inline-block;
width: 1920;
height: 20;
background: linear-gradient(#858585, #636263);
border-top: 1px solid #656565;
border-bottom: 1px solid #3663ab;
box-shadow: inset 0 1px 0 #a8a8a8;
}
.wrapper {
position: relative;
display: inline-block;
width: 1920;
height: 24;
background: linear-gradient(#c8bfb0, #f5efe6);
border-bottom: 1px solid #d3c7b6;
}
.seperator {
position: absolute;
display: inline;
width: 2;
height: 22;
border: 2px solid #fff;
background-color: #000;
}
.onclick-menu-red {
font-family: verdana;
font-size: 11px;
color: #eaeaea;
background: linear-gradient(#858585, #636263);
box-shadow: inset 0 1px 0 #a8a8a8;
position: relative;
display: inline-block;
cursor: pointer;
}
.onclick-menu-red:hover {
color: #fff;
}
.onclick-menu-red:active {
background-color: red;
}
.onclick-menu-red:after {
background-color: red;
}
.onclick-menu-red:focus {
pointer-events: none;
background-color: red;
}
.onclick-menu-red:focus .onclick-menu-content {
opacity: 1;
visibility: visible;
}
.onclick-menu-content {
font-family: verdana;
pointer-events: auto;
position: absolute;
z-index: 1;
opacity: 0;
visibility: hidden;
transition: visibility 0.5s;
float: left;
}
.onclick-menu-red.no-pointer-events {
pointer-events: auto !important;
}
.onclick-menu-red.no-visibility .onclick-menu-content {
visibility: visible !important;
display: none;
}
.onclick-menu-red.no-visibility:focus .onclick-menu-content {
display: block;
}
.onclick-menu-red.no-opacity .onclick-menu-content {
opacity: 1 !important;
}
.onclick-menu-red {
padding: 3 27 4 27;
margin: 0 0 1em 0;
outline: 0;
}
.onclick-menu-red:before {
padding: 5px 10px;
background-color: #94a4a5;
}
.onclick-menu-content {
display: inline-block;
background: linear-gradient(#c8bfb0, #f5efe6);
width: 600;
margin-top: 5px;
margin-left: 0;
padding: 5px;
float: left;
}
.onclick-menu-content ul {
display: inline;
}
.onclick-menu-content li {
display: inline;
color: #f2f5e9;
list-style-type: none;
white-space: nowrap;
}
a:link {
color: #000;
text-decoration: none;
}
a:visited {
color: #000;
text-decoration: none;
}
a:hover {
color: red;
text-decoration: none;
}
a:active {
color: #fff;
font-weight: bold;
text-decoration: none;
}
My HTML:
<div class="container">
<div tabindex="0" class="onclick-menu-red">Home
<ul class="onclick-menu-content">
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
</ul>
</div>
<div class="seperator"></div>
<div tabindex="0" class="onclick-menu-red">Settings
<ul class="onclick-menu-content">
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
</ul>
</div>
<div class="seperator"></div>
<div tabindex="0" class="onclick-menu-red">Expenses
<ul class="onclick-menu-content">
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
</ul>
</div>
<div class="seperator"></div>
<div tabindex="0" class="onclick-menu-red">Incomes
<ul class="onclick-menu-content">
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
</ul>
</div>
</div>
<div class="wrapper"></div>
My issue:
I did manage to fix a few issues I had, but my main issue right now is that I want the buttons to change to "red" once clicked.

This is a simple issue which comes from the use of the property background for elements basically but only using background-color when overwriting property.
You might change background-color: red to background: red to fix this issue.

Related

I have a responsive page and I cant get my drop own menu to center on the smallest window size,

I tired setting the anchor display to block and setting max-width on everything to 100%, but this isn't working. shouldnt setting setting the max-width to 100% an making anchor a block element, make everything expand the entire width? Why isn't this working and how can I get it to work? thanks
html {
font-size: 1em;
}
body {
margin: auto;
padding: 0;
max-width: 100%;
}
header {
background-image: url('../img/MountainsTestImage.jpg');
background: cover;
background-repeat: none;
margin: 0;
padding: 0;
max-width: 100%;
}
.main-title {
padding: 20px;
text-align: center;
margin: auto;
color: orange;
font-size: 2.5em;
}
nav {
color: white;
background-color: orange;
margin: auto;
padding: 0;
max-width: 100%;
}
nav li > span,
nav a {
font-size: 1.3em;
}
nav ul {
padding: 0;
text-align: center;
max-width: 100%;
margin: auto;
}
nav li {
border-style: solid;
border-width: 0 1px 0 0;
border-color: rgba(0, 0, 0, .1);
list-style: none;
max-width: 100%;
}
.main-nav:hover {
background-color: #ffcc33;
}
nav a {
text-decoration: none;
color: orange;
}
.home-page a:visited {
color: white;
}
nav a:visited {
color: orange;
}
nav a,
span {
font-weight: 700;
}
.active {
background-color: #ffcc33;
}
.main-nav {
position: relative;
}
.sub-nav li:hover {
text-shadow: 2px 2px 5px rgba(0, 0, 0, .4);
}
.sub-nav {
box-shadow: 5px 5px 10px rgba(0, 0, 0, .1);
z-index: 1;
position: absolute;
display: none;
background-color: white;
max-width: 100%;
}
.sub-nav li {
max-width: 100%;
}
.sub-nav li a {
display: block;
max-width: 100%;
}
.main-nav:hover .sub-nav {
display: block;
}
section {
margin: auto;
padding: 0;
max-width: 100%;
}
footer {
margin: auto;
padding: 0;
max-width: 100%;
}
#media screen and (min-width: 450px) {
nav li {
display: inline-block;
margin: 3px;
padding: 2px;
}
nav ul {
text-align: right;
padding: 0 5% 0 0;
}
.main-title {
text-align: left;
margin: 0 0 15px 15%;
padding: 15px 0 0 0;
color: orange;
font-size: 4em;
}
}
<!DOCTYPE HTML>
<html>
<head>
<title>TEST</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<header>
<h1 class="main-title">This Is a Test</h1>
<nav>
<ul>
<li class="main-nav home-page active">
HOME
</li>
<li class="main-nav">
<span> Content 1 </span>
<ul class="sub-nav">
<li>Page 1
</li>
<li>Page 2
</li>
<li>Page 3
</li>
</ul>
</li>
<li class="main-nav">
<span> Content 2 </span>
<ul class="sub-nav">
<li>Page 4
</li>
<li>Page 5
</li>
<li>Page 6
</li>
</ul>
</li>
<li class="main-nav">
<span> Content 3 </span>
<ul class="sub-nav">
<li>Page 7
</li>
<li>Page 8
</li>
<li>Page 9
</li>
</ul>
</li>
</ul>
</nav>
</header>
<section>
</section>
<footer>
</footer>
</body>
</html>
your UL is constraining the width, % is always relative to the parent.
.sub-nav {
width: 100%;
}
Should fix it:
https://jsfiddle.net/tvjphz51/1/

Menu list items: Full width (border-bottom)?

I have a menu from a list. On small widths I want the border-bottom to use the full width:
HTML:
<div id="wrapper">
<ul id="menu">
<li class="active">Item 1</li>
<li>Item 2 with Text</li>
<li>Item 3 with more Text</li>
<li>Item 4</li>
<li>Item 5 with Text</li>
</ul>
</div>
CSS:
#menu li {
display: inline-block;
}
#menu li:not(.active) {
border-bottom: 2px solid #ccc;
}
#menu li.active {
border-bottom: 2px solid red;
}
#menu li a {
padding: .5em 1em;
box-sizing: border-box;
display: block;
}
Test: Fiddle
Without a background image, you're going to have to get creative. How about this...
var li = $("#menu li");
li.click(function () {
li.removeClass("active");
$(this).addClass("active");
});
body {
background-color: #eee;
}
#wrapper {
width: 300px;
padding: 1em;
background-color: #fff;
}
a {
color:inherit;
text-decoration: none;
}
#menu {
overflow: hidden;
list-style:inline;
padding: 0 0 2px;
-webkit-margin-before: 0;
-webkit-margin-after: 0;
margin: 0 1em 1em;
}
#menu li {
display: inline-block;
margin-top: 2px;
margin-bottom: -2px;
position: relative;
z-index: 2;
}
#menu li:after {
content: " ";
position: absolute;
bottom: -2px;
right: -1000px;
width: 1000px;
height: 0;
border-bottom: 2px solid #ccc;
z-index: 1;
}
#menu li:not(.active) {
border-bottom: 2px solid #ccc;
}
#menu li.active {
border-bottom: 2px solid red;
}
#menu li:not(:last-of-type) {
margin-right: -.275em;
}
#menu li a {
padding: .5em 1em;
box-sizing: border-box;
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="wrapper">
<ul id="menu">
<li class="active">Item 1</li>
<li>Item 2 with Text</li>
<li>Item 3 with more Text</li>
<li>Item 4</li>
<li>Item 5 with Text</li>
</ul>
</div>
flexbox (as mention by Luis P.A.) can help here by making the li fill with full width of the ul and wrapping as required.
Flexbox Support is IE10 and up.
var li = $("#menu li");
li.click(function() {
li.removeClass("active");
$(this).addClass("active");
});
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
background-color: #eee;
}
#wrapper {
width: 300px;
padding: 1em;
background-color: #fff;
}
a {
color: inherit;
text-decoration: none;
}
#menu {
list-style: none;
padding: 0;
-webkit-margin-before: 0;
-webkit-margin-after: 0;
margin: 0 1em 1em;
overflow: hidden;
display: flex;
flex-wrap: wrap;
}
#menu li {
flex: 1 1 auto;
position: relative;
}
#menu li:not(.active) {
border-bottom: 2px solid #ccc;
}
#menu li.active {
border-bottom: 2px solid red;
}
#menu li:not(:last-of-type) {} #menu li a {
padding: .5em 0;
box-sizing: border-box;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
<ul id="menu">
<li class="active">Item 1
</li>
<li>Item 2 with Text
</li>
<li>Item 3 with more Text
</li>
<li>Item 4
</li>
<li>Item 5 with Text
</li>
</ul>
</div>

:hover selector being detected even when behind another element

So, on a page I'm making, I have a menu bar and a black box that changes color on hover. When I try to open the menu, and move the mouse over the black box (still in the boundaries of the dropdown), the drop down disappears and the black box :hover selector is called.
Basically, :hover is being detected when it's behind an element. I tried searching, but the only results I'm getting are how to force it to detect behind an element. I want to prevent that.
http://jsfiddle.net/yFD2E/13/
Important HTML:
<div class="menuBar">
<ul>
<li>Home
<ul>
<li>Item 1</li>
<li>Item 1</li>
<li>Item 1</li>
<li>Item 1</li>
</ul>
</li>
<li>Home 2
<ul>
<li>Item 1</li>
</ul>
</li>
</ul>
</div> <!--End menu bar-->
<div class="center">
<div class="buttonGroup">
<div class="buttonMain">
<p class="hover">Plot</p>
<p class="show title">Plot</p>
<a class="show inFront" href="#">Test</a>
</div>
</div>
</div>
menubar.css:
.menuBar {
height: 50px;
width: auto;
background-color: #E9523F;
border-radius: 5px;
box-shadow: 0px 0px 10px #DA281F;
z-index: 10000;
}
.menuBar ul {
padding: 0;
list-style-type: none;
positoin: relative;
}
.menuBar ul li {
float: left;
width: 80px;
height: 50px;
display: inline-block;
text-align: center;
border-radius: 5px;
}
.menuBar ul ul {
visibility: hidden;
position: absolute;
width: 80px;
border-radius: 0pxx 5px;
}
.menuBar ul ul li{
width: 80px;
height: 50px;
float: none;
position: relative;
}
.menuBar ul li a {
text-decoration: none;
color: #FAE12D;
text-align: center;
width: 80px;
line-height: 50px;
margin: 0;
padding: 0;
font-size: 17pt;
height: 50px;
display: block;
font-weight: 800;
}
.menuBar ul li:hover ul {
background-color: #DA281F;
box-shadow: 0px 0px 10px #DA281F;
}
.menuBar ul li:hover {
background-color: #DA281F;
box-shadow: 0px 0px 10px #DA281F;
}
.menuBar ul li:hover ul {
visibility: visible;
}
.menuBar ul li li:hover {
background-color: #FE3845;
}
buttons.css
.buttonMain {
height: 400px;
width: 400px;
background-color: #000;
float: left;
margin-left: 20px;
margin-right: 20px;
line-height: 400px;
}
.buttonGroup {
height: 500px;
}
.buttonMain .hover {
position: absolute;
width: inherit;
height: inherit;
line-height: inherit;
margin: 0;
padding: 0;
text-align: center;
overflow: show;
color: #FFF;
}
.buttonMain .show {
position: absolute;
width: inherit;
height: inherit;
line-height: inherit;
margin: 0;
padding: 0;
text-align: center;
overflow: show;
color: #FFF;
background-color: #AAF;
opacity: 0;
}
.buttonMain:hover .show {
opacity: 1;
}
.buttonMain:hover .hover {
opacity: 0;
}
.inFront {
background-color: transparent !important;
}
.title {
line-height: normal !important;
text-decoration: underline;
font-size: 30pt;
font-weight: 600;
}
z-index will do the trick like following
.menuBar ul {
padding: 0;
list-style-type: none;
position: relative; // Just changed the spelling of position
z-index:1000; // Added
}
http://jsfiddle.net/peqh662a/

Centering Submenu

I've read all the questions concerning centering submenus. But I don't get my problem solved.
I have a simple navigation bar with 2 submenus.
You can find it here: Fiddle.
ul#nav, ul#sub1, ul#sub2 {
list-style-type: none;
}
ul#nav {
position: relative;
}
ul#nav li {
width: 125px;
text-align: center;
float: left;
margin-right: 4px;
}
ul#nav a {
text-decoration: none;
display: block;
width: 125px;
height: 25px;
line-height: 25px;
background-color: #FFF;
border: 1px solid #CCC;
border-radius: 5px;
color: #000;
}
ul#sub1 a, ul#sub2 a {
margin-top: 4px;
}
ul#nav li:hover > a {
background-color: #6E6E6E;
color: #FFF;
}
ul#nav li:hover a:hover {
background-color: #E2E2E2;
color: #000;
}
ul#sub1, ul#sub2 {
display: none;
position: absolute;
left: 0px;
}
ul#nav li:hover ul#sub1 {
display: block;
}
ul#sub1 li:hover ul#sub2 {
display: block;
}
<nav>
<ul id="nav">
<li>Reisen
<ul id="sub1">
<li>Europa</li>
<li>Amerika</li>
<li>Asien
<ul id="sub2">
<li>Thailand</li>
<li>Bhutan</li>
<li>China</li>
<li>Vietnam</li>
<li>Japan</li>
</ul>
</li>
<li>Afrika</li>
<li>Australien</li>
</ul>
</li>
<li>Magazin</li>
<li>Karriere
<ul id="sub1">
<li>Thema 1</li>
<li>Thema 2</li>
<li>Thema 3</li>
</ul>
</li>
<li>Kontakt</li>
</ul>
</nav>
I want the submenu centered. When I hover over "Reisen" the submenu gets the same width like the main menu.
When I hover over "Karriere", I want the submenu centered under "Karriere" and not positioned left under "Reisen".
I was thinking of a span-element to the button "Karriere" but I couldn't solve it.
Thanks for your help.
I don't really now if this is what you're looking for or not, but maybe something like this?
Note: I made a few changes to your CSS and HTML, mainly changing everything to use classes instead of IDs
JS Fiddle Example
HTML
<nav>
<ul id="nav">
<li>Reisen
<ul class="sub">
<li>Europa</li>
<li>Amerika</li>
<li>Asien
<ul class="sub-second">
<li>Thailand</li>
<li>Bhutan</li>
<li>China</li>
<li>Vietnam</li>
<li>Japan</li>
</ul>
</li>
<li>Afrika</li>
<li>Australien</li>
</ul>
</li>
<li>Magazin</li>
<li>Karriere
<ul class="sub">
<li>Thema 1</li>
<li>Thema 2</li>
<li>Thema 3</li>
</ul>
</li>
<li>Kontakt</li>
</ul>
CSS
ul#nav, ul.sub {
list-style-type: none;
}
ul#nav {
position: relative;
}
ul#nav li {
width: 125px;
text-align: center;
float: left;
margin-right: 4px;
position: relative;
}
ul#nav a {
text-decoration: none;
display: block;
width: 125px;
height: 25px;
line-height: 25px;
background-color: #FFF;
border: 1px solid #CCC;
border-radius: 5px;
color: #000;
}
ul.sub a {
margin-top: 4px;
}
ul#nav li:hover > a {
background-color: #6E6E6E;
color: #FFF;
}
ul#nav li:hover a:hover {
background-color: #E2E2E2;
color: #000;
}
ul.sub {
display: none;
position: absolute;
left: 0px;
padding-left: 0;
}
ul.sub-second {
display: none;
list-style: none;
left:100px;
top: 0;
position: absolute;
}
ul#nav li:hover ul.sub {
display: block;
}
ul#nav li:hover ul.sub li:hover ul.sub-second {
display:block;
}
}

Linkcolor on submenus inherits the :hover one

I'm trying to create a menu were the linkcolor should be white with a blue background. Problem is when you hover a submenu that has submenus. They "loose" the default black color and inherits (?) the white one making it all white.
http://jsfiddle.net/lasseedsvik/TzMxG/2/
Products + Category2 + .. in the fiddle...
What's missing to make all submenus links, that aren't hover, have a black color?
HTML
<ul class="topmenu">
<li class="topmenu-root-node">
Welcome
</li>
<li class="topmenu-root-node nonempty">
About
<ul class="topmenu-submenu-container">
<li class="topmenu-sub-item">
<div class='item'>A
</div>
</li>
<li class="topmenu-sub-item">
<div class='item'>B
</div>
</li>
</ul>
</li>
<li class="topmenu-root-node selected nonempty">
Products
<ul class="topmenu-submenu-container">
<li class="topmenu-sub-item">
<div class='item'>Category1
</div>
</li>
<li class="topmenu-sub-item nonempty">
<div class='item has-submenu'>Category2
</div>
<ul class="topmenu-submenu-container">
<li class="topmenu-sub-item">
<div class='item'>Product1
</div>
</li>
<li class="topmenu-sub-item">
<div class='item'>Product2
</div>
</li>
</ul>
</li>
<li class="topmenu-sub-item">
<div class='item'>Category3
</div>
</li>
</ul>
</li>
</ul>
CSS:
/* Top menu */
ul.topmenu {
position: relative;
z-index: 1200;
margin: 0;
height: 35px;
padding: 0 18px 4px 19px;
text-transform: uppercase;
}
ul.topmenu a {
text-decoration: none;
font-size: 14px;
}
ul.topmenu li {
z-index: 1200;
float: left;
list-style: none;
padding: 12px 10px 5px;
}
ul.topmenu li a:active {
}
ul.topmenu a:hover {
color: #333333;
}
ul.topmenu li a {
display: block;
color: #000;
}
ul.topmenu li.hover, ul.topmenu li:hover {
position: relative;
z-index: 1200;
}
.topmenu-root-node:hover {
background: url(css/images/menubg_hover.gif) repeat-x bottom;
}
/* LEVEL TWO */
.topmenu-sub-item li a {
}
ul.topmenu ul {
width: 220px;
display: none;
position: absolute;
top: 45px;
left: 0;
z-index: 1200;
margin: 0;
padding: 0;
border: #000 1px solid;
}
ul.topmenu > li > ul {
border-top: none;
}
ul.topmenu ul li {
padding: 4px 6px 4px 13px;
float: none;
z-index: 1200;
/*background: url(css/images/menu_separator.png) bottom center no-repeat;*/
}
ul.topmenu ul li a {
color: #000;
border-right: none;
display: inline-block;
z-index: 1200;
font-size: 12px;
font-weight: normal;
}
ul.topmenu li:hover a {
color: #000;
}
ul.topmenu ul li:hover {
background: #0098c3;
color: #fff;
}
ul.topmenu ul li ul li a {
color: #000;
}
ul.topmenu ul li:hover a {
color: #fff;
}
.item a {
color: #000;
}
.topmenu-submenu-container {
background: #fff;
}
ul.topmenu ul ul {
left: 100%;
z-index: 1200;
top: 0;
}
ul.topmenu div {
cursor: pointer;
}
ul.topmenu div.has-submenu {
padding-right: 15px;
/*background: url(css/images/menuarrow.png) no-repeat right;
background-position: right;*/
}
.topmenu-root-node.selected {
font-weight: 700;
/*background: url(css/images/menubg_selected.jpg) repeat-x;*/
height: 28px;
}
.topmenu-root-node {
/*background: url(css/images/menubg.jpg) repeat-x;*/
height: 28px;
}
UPDATE:
http://jsfiddle.net/lasseedsvik/TzMxG/4/
Fixed thanks to Jonas! :)
There's no need for all that javascript.
http://jsfiddle.net/jonigiuro/TzMxG/3/
You can use the ">" selector which means pick the one that comes right after it
.element > a
will not include all the a's in your .element, avoiding the style overwriting
ul.topmenu ul li:hover > a {
color: #fff;
}

Resources