CSS - Show/hide collapse simple navigation when user clicks title - css

I'd like to ask you for a little help. Well, I have to create a very simple navigation but I got a problem because I can't rewrite this code to make nav works without checkbox, it's very important - it shouldn't based on checkbox.
Ok, so first of all here is version based on checkboxes: http://codepen.io/anon/pen/dPaeZE
As you can see when user clicks TEST1 or TEST2 it expands list.
I want to make something similar to that but without checkboxes as I mentioned before, so here is my second codepen: http://codepen.io/anon/pen/azXGEQ
I tried this code:
.menu {
display: none;
}
/* show menu */
.nav-header:focus + .menu{
display: block;
}
But it doesn't work, when I click on TEST1 or TEST2 ul doesn't show. Something here is wrong but I don't know what exactly. :P
Ok, so that's all hope you could check it and help me a bit.

Not a CSS soludtion but a simply jQuery toggle works fine:
Codepen: http://codepen.io/anon/pen/yyZjdE
jQuery(document).ready(function(){
jQuery('.nav-header').click(function(){
jQuery(this).siblings('ul.menu').toggle();
});
});

Basically, a span cannot receive focus.
The optimal solution is to use an element that can receive focus like an anchor link or, for preference an actual button.
For ease, I have use an link here but a button would be preferable because Links are not buttons
/* basic style */
body {
margin-top: 30px;
}
a {
color: #EF9000;
text-decoration: none;
}
.container {
width: 230px;
margin: 0 auto;
}
/* nav */
.nav,
.menu {
padding: 0px;
margin: 0px;
text-align: center;
text-transform: uppercase;
list-style: none;
}
.menu li {
list-style: none;
display: list-item;
padding: 2px 0;
}
.menu li a {
display: block;
padding: 2px 0;
transition: all 0.2s ease-in-out;
}
.menu li a:hover {
background-color: #EF9000;
color: #F1F1F1;
}
/*
* Hide/show nav
*/
.nav-header {
cursor: pointer;
}
.menu {
display: none;
}
/* show menu */
.nav-header:focus + .menu {
display: block;
}
<div class="container">
<h1>Collpase menu NO checkbox</h1>
<ul class="nav">
<li>
Test 1
<ul class="menu">
<li>Something
</li>
<li>Another element
</li>
</ul>
</li>
<!--./test1-->
<li>
Test 2
<ul class="menu">
<li>Element
</li>
</ul>
</li>
<!--./test2-->
</ul>
</div>
<!--/.container-->
Stack Overflow Link to preferred answer.

Related

how can I make the active tab highlighted until I change the tab for another?

I created a simple vue app with router. I managed to get the tab highlighted upon hovering on it but I want it to stay coloured until the user changes for another.
Here is relevant code:
<nav>
<ul>
<li><router-link to="/contact">Contact</router-link></li>
<li><router-link to="/blog">Blog</router-link></li>
<li><router-link to="/ceremonies">Ceremonies</router-link></li>
<li><router-link to="/pregnancy">Pregnancy Yoga</router-link></li>
<li><router-link to="/about">About</router-link></li>
<li><router-link to="/">Home</router-link></li>
</ul>
</nav>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color:#F1D0E5
}
li {
float: right;
}
li a {
font-size: 20px;
display: block;
color: #89a864;
font-weight:900;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color:#985277;
}
li a:active {
background-color:#985277;
}
it does not stay higlighted as soon as I remove the cursor
vue-router automatically adds two classes to the active <router-link> component.
.router-link-active and .router-link-exact-active
You can use those classes to style your links.
This is a comprehensive answer that covers how/when these classes are applied.

How to center the nav

I found this really simple way to make responsive menu on w3schools (article) but I have been struggling for a few days trying to center it horizontally.
html
<ul class="topnav">
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
<li class="icon">
☰
</li>
</ul>
CSS
/* Remove margins and padding from the list, and add a black background color */
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
/* 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: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of links on hover */
ul.topnav li a:hover {background-color: #111;}
/* Hide the list item that contains the link that should open and close the topnav on small screens */
ul.topnav li.icon {display: none;}
I made a fiddle from you linked code: https://jsfiddle.net/gqm7zdf9/
A solid option is to add a wrapper around your UL so that you can move the background-color there. Then you position it inside its wrapper.
HTML
<div class="nav-wrapper">
<ul class="topnav">
...
</ul>
</div>
additional CSS
div.nav-wrapper {
background-color: #333;
}
div.nav-wrapper ul.topnav {
display: inline-block;
position: relative;
left: 50%;
transform: translateX(-50%);
}
 
If you're able (depending on the browsers you need to support) to use display:flex, there's an even way more simple option. You just need to add some CSS:
ul.topnav {
/* ... */
display: flex;
justify-content: center;
}
https://jsfiddle.net/gqm7zdf9/1/
 
I think you'll be able to continue with your media-queries from there...

Default link color still showing up, even after changing with CSS

I have my nav and li with the links, and went in to change the color with my CSS. Usually this works fine for me, but today I just can't seem to figure out the glitch. The link for my nav is still showing up as the default color, not the color I set in CSS.
I tried cleaning up the code, and am still running into the problem. What is odd is that the hover color is working fine, but the display color is not.
Here is the code:
<nav>
<ul>
<li> home </li>
<li> about </li>
<li> photos </li>
<li> contact </li>
<li> prices </li>
</ul>
</nav>
/*Nav*/
nav {
width: 50%;
margin-left: 45%;
margin-right: auto;
}
nav ul li {
position: relative;
display: inline-block;
float: left;
width: 20%;
margin-bottom: 20px;
text-align: center;
font-size: 24px;
}
nav ul li a {
color: #4C4C4C;
text-decoration: none;
}
nav ul li a:hover {
color: #f64751;
text-decoration: none;
}

Css vertical menu: issue with background color on hover mode

I'm having a design issue with my css vertical menu.
It's working but it does not have the effect i would like to have when I do a mouse hover on a category
Below, you will see a simple vertical menu which appears when you hover your mouse over the main category
However I would like to have a small effect :
When the mouse is hover a category, i would like to add a background color (black).
It's working but I would like that the height and the width of the background to stick exactly to the same height and width of the text. Currently, I dont know why; the height of the background is more than the height of my text.
Here is some pictures of how it's right now and how i would like to be be.
How it 's now:
How I would like it to be:
Here is my code Html code
<div id="menu">
<ul id="MenuDeroulant">
<li style="margin-left:-10px;">Main categorie
<ul>
<li><a href="" >Subcat 1</a></li>
<li><a href="" >Subcat 2</a></li>
</ul>
</li>
</ul>
Here is my css code:
#MenuDeroulant
{
margin: 0;
padding: 0
}
#MenuDeroulant li
{
float: left;
list-style: none;
}
#MenuDeroulant li a
{
display: block;
padding: 0px 0px;
text-decoration: none;
color: #000;
white-space: nowrap;
text-align:center;
}
#MenuDeroulant li a:hover
{
background: #000;
color: #FFF;
}
#MenuDeroulant li ul
{ visibility: hidden;
padding: 0px 0px;
}
#MenuDeroulant li ul li
{
float: none;
display: inline;
}
#MenuDeroulant li ul li a
{
width: auto;
padding: 0px 0px;
}
#MenuDeroulant li ul li a:hover
{
background: #0000;
padding: 0px 0px;
}
Thanks in advance for your help and I wish you a very nice day,
Anselme
Use width:100% to all your <li> or li a elements and a fixed width to your <ul>. This will solve your issue.
With that CSS your nested ul is permanently hidden. You'll need something like
#MenuDeroulant li:hover ul {
visibility:visible;
}
to show the nested menu items then maybe display: inline on the #MenuDeroulant li ul li a
You can add a class to your menu hyperlinks giving them a margin-bottom:3px and it should bump up the links in the container.

CSS navigation submenu and seperator

I have created a navigation bar that is centered with CSS which works. Each li item is separated with a border which is a background image. When hovering on the nav items, the separator disappears because the hover changes the background (I guess) but I wonder how I can fix this, padding or margin can't work because it will just shift the li element.
Second problem is that the sub menu items aren't displaying correctly and I have no idea why...
Demonstration: http://jsfiddle.net/Xenios/tfbhh/9/embedded/result/
The code: http://jsfiddle.net/Xenios/tfbhh/9/
I'm trying to get this to work for almost a week, and I'm quite tired of it, so I'm looking here for support.
Separator
As you know the main bar (nav_container) has a background image, which makes up the look of the button. The background for each button is the separator and nothing else (10px on the right). So, when your on hover background shows, because its park of the non-hover background.
In order to fix this you need to put the separator in it's own <li>, with the non-hover background. Then when you hover the elements they can easily change to your current on hover image with.
If you don't want to separate the <li> elements then, you will have to will have to make individual full width images for each button, but looking at the way you've gone about making this menu, I doubt you will want to do this.
Here is your working example (I only did the first few buttons): http://jsfiddle.net/tfbhh/43/
Submenu
As I mentioned above, you have set the container background image, you haven't done this on your submenu items, so thats why they don't have a larger looking button. Use your developer toolbar (F12) to see the styling and this should clear it up.
You can use a left padding equal to the width of the separator on the li and change only the background on the a. Also I noticed you used class="separator" on all but the first list item. You could replace that with the :first-child pseudo selector. Then you would get something like this:
li:first-child { padding-left: 0; background: transparent; }
li { padding-left: 3px; background: url(separator.png) no-repeat; }
li a { line-height: 40px; padding: 0 15px; }
li a:hover { background: url(anchor-hover.png) repeat-x; }
Edit: The CSS above covers the core styling of this solution. Here's a working example (using background colors):
http://jsfiddle.net/haa5X/3/
The complete CSS:
ul { overflow: hidden; background: green; }
li:first-child { padding-left: 0; }
li { padding-left: 3px; float: left; background: red; }
li a { float: left; line-height: 40px; padding: 0 15px; background: yellow; }
li a:hover { background: purple; }
The complete HTML:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
Edit 2: Sorry, missed the part of the submenu:
http://jsfiddle.net/haa5X/4/
The complete CSS:
ul { overflow: hidden; margin: 0; background: green; }
ul > li:first-child { padding-left: 0; }
ul > li { padding-left: 3px; float: left; background: red; }
ul > li a { float: left; line-height: 40px; padding: 0 15px; background: yellow; }
ul > li a:hover { background: purple; }
li ul { display: none; position: absolute; margin-top: 40px; }
li:hover ul { display: block; }
li li { padding-left: 0; float: none; display: block; }
li li a { float: none; display: block; width: 100%; }
The complete HTML:
<ul>
<li>Item 1</li>
<li>Item 2
<ul>
<li>First sub item</li>
<li>Sub item 2</li>
<li>Last sub item</li>
</ul>
</li>
<li>Item 3</li>
</ul>
​

Resources