Display affected by drop down using CSS - html

I've got a nav bar configured to hide drop down elements (using display:none) until hovered over when it will show as a drop down (using display:block at a:hover). It all works great except the "divs" below it are pushed down when drop down displays. How can I get the "divs" to remain in place regardless of the display of the drop down items?
Thanks!

I hope it helps you
<h1>Simple Pure CSS Drop Down Menu</h1>
<ul class="nav">
<li>Menu1
<ul>
<li>Sub menu1</li>
<li>Sub menu2</li>
<li>Sub menu3</li>
<li>Sub menu4
<ul>
<li>Deep menu1</li>
<li>Deep menu2
<ul>
<li>Sub deep menu1</li>
<li>Sub deep menu2</li>
<li>Sub deep menu3</li>
</ul>
</li>
<li>Deep menu3</li>
</ul>
</li>
<li>Sub menu5</li>
</ul>
</li>
<li>Menu2
<ul>
<li>Sub menu1</li>
<li>Sub menu2</li>
<li>Sub menu3</li>
</ul>
</li>
<li>Menu3
<ul>
<li>Sub menu1</li>
<li>Sub menu2
<ul>
<li>Category1</li>
<li>Category2</li>
<li>Category3</li>
<li>Category4</li>
<li>Category5</li>
</ul>
</li>
<li>Sub menu3</li>
<li>Sub menu4</li>
<li>Sub menu5</li>
</ul>
</li>
</ul>
.nav {
margin: 15px 0 0;
}
.nav li {
display: inline-block;
width: 150px;
position: relative;
text-align: center;
}
.nav li:hover {
background: #f6f6f6;
}
.nav li a {
display: block;
padding: 10px 15px;
}
.nav ul {
display: none;
position: absolute;
}
.nav ul li {
display: block;
}
.nav li:hover ul {
display: block;
}
.nav li:hover ul li ul {
display: none;
}
.nav li ul li:hover ul {
display: block;
left: 100%;
top: 0;
}
.nav li ul li:hover ul li ul {
display: none;
}
.nav li ul li ul li:hover ul {
display: block;
}
fiddle

The <div> elements which contain the dropdown parts have to have position: absolute. That way they don't occupy any space in the surrounding containers. Tune their position n relation to their parent elements with the top and left (or right) settings.
The parent elements of those DIVs need to have position: relative to enable the absolute positioning of their children elements.
By the way, position: fixed wouldn't work that well, since this positions an element absolutely to the window, which in most cases you don't want (exception: if you have a fixed navbar).

Related

Collapsible category listing without jQuery

I'm working on an ebay shop. I need to make categories collapsible after I click the root item (if that is even possible) following ebay's guidelines.
Rules:
Ebay doesn't want us to use active content, that means no JavaScript, flash.
It only allows us to use CSS3.
We can't even change the output HTML for categories. So only option available is CSS.
HTML:
<div id="org-categories">
<div class="ttl">Shop home</div>
<ul class="lev1">
<li>CAT A</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT B</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT C</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
</ul>
</div>
Nice question! Because of this, I just discovered the magic of :focus-within.
<div id="org-categories">
<div class="ttl">Shop home</div>
<ul class="lev1">
<li>CAT A</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT B</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT C</li>
<ul class="lev2">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
</ul>
</div>
Then here comes the magic:
li + .lev2 {
display: none;
}
li:focus-within + .lev2 {
display: block;
}
Check it out in a fiddle
So after digging into this subject, your request seems impossible with this markup. BUT with minor adjustments to the html you provide, you can achieve it with this minimal style:
<style>
.lev1 li { display:block; }
.lev1 ul { display:none; }
.lev1 ul:target { display:block; }
</style>
<div id="org-categories">
<div class="ttl">Shop home</div>
<ul class="lev1">
<li>CAT A</li>
<ul id="lev2a">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT B</li>
<ul id="lev2b">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
<li>CAT C</li>
<ul id="lev2c">
<li>sub cat 1</li>
<li>sub cat 2</li>
</ul>
</ul>
</div>
*Minor adjustments = change ul's classes to an unique id's. That way the :target pseudo element can affect it.
Hope it solve it, and good luck!
Here is one example on how to do this. Whilst this shows expanding menus, it also has some visual effects. Try it out and adapt yourself. The code itself is from http://www.dynamicdrive.com/
<style>
.bgslidemenu {
font: bold 16px 'Bitter', sans-serif; /* use google font */
position: relative;
width: 100%;
}
.bgslidemenu:after { /* clear menu */
content: '';
clear: both;
display: block;
}
/* Top Level Menu */
.bgslidemenu ul {
z-index: 100;
margin: 0;
padding: 0;
position: relative;
list-style: none;
float: right; /* change to "left" to left align menu */
}
/* Top level list items */
.bgslidemenu ul li {
position: relative;
display: inline;
margin-right: 20px; /* spacing between each top level menu item */
float: left;
}
/* Top level menu items link style */
.bgslidemenu ul li a {
display: block;
position: relative;
color: white;
padding: 14px 10px;
color: black;
text-decoration: none;
}
.bgslidemenu ul li a:link, .bgslidemenu ul li a:visited {
color: black;
}
/* Top level menu items link style on hover */
.bgslidemenu ul li:hover > a {
color: purple !important;
}
/* LIs links with a sub UL style */
.bgslidemenu ul li > a {
/* add padding to accomodate arrow inside LIs */
padding-right: 25px;
}
/* LIs links with NO sub UL style */
.bgslidemenu ul li > a:only-child {
/* undo padding for non submenu LIs */
padding-right: 10px;
}
/* LIs links with a sub UL pseudo class (Add down arrow) */
.bgslidemenu ul li > a:after {
/* add arrow inside LIs */
content: "";
position: absolute;
height: 0;
width: 0;
border: 5px solid transparent;
border-top-color: black;
top: 50%;
transform: translateY(-20%);
right: 8px;
}
/* LIs links with NO sub UL pseudo class */
.bgslidemenu ul li > a:only-child:after {
/* undo arrow for non submenu LIs */
display: none;
}
/* Sub ULs style */
.bgslidemenu ul li ul {
position: absolute;
left: -5000px;
top: auto;
opacity: 0;
width: 200px; /* width of drop down menus */
visibility: hidden;
padding-top: 80px; /* Add large top padding to drop down menu */
z-index: -1;
background: #F3F3F3;
transform: translateY(100px);
-webkit-transition: opacity .3s, transform .5s, visibility 0s .3s, left 0s .3s;
transition: opacity .3s, transform .5s, visibility 0s .3s, left 0s .3s;
}
/* Sub UL style on hover */
.bgslidemenu ul li:hover > ul {
visibility: visible;
left: -30px;
transform: translateY(-80px); /* move drop down menu upwards (should be smaller than padding-top value above) */
opacity: 1;
-webkit-transition: opacity 1s, transform .5s;
transition: opacity 1s, transform .5s;
}
/* Sub level Menu list items (alters style from Top level List Items) */
.bgslidemenu ul li ul li {
display: list-item;
float: none;
overflow: hidden;
}
/* Add animated line to sub menu item on Mouseover */
.bgslidemenu ul li ul li:after {
content: '';
position: absolute;
width: 30px;
height: 5px;
background: purple;
left: 0;
top: 50%;
transform: translate3d(-100%, -50%, 0);
transition: transform .3s;
}
.bgslidemenu ul ul li:hover:after {
transform: translate3d(0, -50%, 0);
}
/* Sub Levels link style on hover and when active */
.bgslidemenu ul ul li:hover > a {
color: purple !important;
}
/* Sub level menu links style */
.bgslidemenu ul li ul li a {
font: normal 16px 'Bitter', sans-serif;
padding: 10px;
padding-left: 40px; /* Add left padding to sub menu links to accommodate animated line */
position: relative;
margin: 0;
}
/* ####### responsive layout CSS ####### */
#media (max-width: 700px) {
/*
For mobile and screen browser windows
Get Sub ULs to expand entire width of document and drop down
Changes menu reveal type from "visibility" to "display" to overcome bug. See comments below
*/
.bgslidemenu ul {
float: none;
}
.bgslidemenu ul li {
position: relative;
display: block;
width: 100%;
}
.bgslidemenu ul li ul {
width: 100%;
position: relative;
border-top: 2px solid #eee;
/* change menu reveal type from "visibility" to "display". Former seems to cause browser to jump to top of page sometimes when menu header is tapped on */
display: none;
}
.bgslidemenu ul li:hover > ul {
display: block;
padding-top: 0;
transform: none;
z-index: 10000;
left: 0 !important;
top: auto;
}
.bgslidemenu ul ul li:hover > ul {
left: 0 !important;
top: auto;
}
}
</style>
<div class="bgslidemenu">
<ul>
<li>Item 1</li>
<li>Item 2
<ul>
<li>Sub Item 1.1</li>
<li>Sub Item 1.2</li>
<li>Sub Item 1.3</li>
<li>Sub Item 1.4</li>
<li>Sub Item 1.2</li>
<li>Sub Item 1.3</li>
<li>Sub Item 1.4</li>
</ul>
</li>
<li>Item 3</li>
<li>Item 4
<ul>
<li>Sub Item 3.1</li>
<li>Sub Item 3.2</li>
</ul>
</li>
<li>Home</li>
</ul>
</div>

Toggle dropdown menu on ul:focus

I'm trying to create dropdown navigation with pure CSS and I want to show dropdown menu on ul click. Problem is that simple ul:focus > ul doesn't work even though there is anchor in it. Selectors :hover and :active are working without problems.
HTML
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4
</ul>
</li>
CSS
#primary_nav_wrap ul li:hover > ul
{
display:block
}
Here's the code:
https://codepen.io/anon/pen/vgpQWV
Your selector needs to be #primary_nav_wrap ul li > a:focus + ul.
Breaking it down:
When the <a> directly inside the <li> has focus, apply these styles to the immediately adjacent <ul>.
Fixed example:
#primary_nav_wrap {
margin-top: 15px
}
#primary_nav_wrap ul {
list-style: none;
position: relative;
float: left;
margin: 0;
padding: 0
}
#primary_nav_wrap ul a {
display: block;
color: #333;
text-decoration: none;
font-weight: 700;
font-size: 12px;
line-height: 32px;
padding: 0 15px;
font-family: "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif
}
#primary_nav_wrap ul li {
position: relative;
float: left;
margin: 0;
padding: 0
}
#primary_nav_wrap ul li.current-menu-item {
background: #ddd
}
#primary_nav_wrap ul li:hover {
background: #f6f6f6
}
#primary_nav_wrap ul ul {
display: none;
position: absolute;
top: 100%;
left: 0;
background: #fff;
padding: 0
}
#primary_nav_wrap ul ul li {
float: none;
width: 200px
}
#primary_nav_wrap ul ul a {
line-height: 120%;
padding: 10px 15px
}
#primary_nav_wrap ul ul ul {
top: 0;
left: 100%
}
#primary_nav_wrap ul li > a:focus + ul {
display: block
}
<h1>Testing menu</h1>
<nav id="primary_nav_wrap">
<ul>
<li class="current-menu-item">Home
</li>
<li>Menu 1
<ul>
<li>Sub Menu 1
</li>
<li>Sub Menu 2
</li>
<li>Sub Menu 3
</li>
<li>Sub Menu 4
<ul>
<li>Deep Menu 1
<ul>
<li>Sub Deep 1
</li>
<li>Sub Deep 2
</li>
<li>Sub Deep 3
</li>
<li>Sub Deep 4
</li>
</ul>
</li>
<li>Deep Menu 2
</li>
</ul>
</li>
<li>Sub Menu 5
</li>
</ul>
</li>
<li>Menu 2
<ul>
<li>Sub Menu 1
</li>
<li>Sub Menu 2
</li>
<li>Sub Menu 3
</li>
</ul>
</li>
<li>Menu 3
<ul>
<li class="dir">Sub Menu 1
</li>
<li class="dir">Sub Menu 2 THIS IS SO LONG IT MIGHT CAUSE AN ISSEUE BUT MAYBE NOT?
<ul>
<li>Category 1
</li>
<li>Category 2
</li>
<li>Category 3
</li>
<li>Category 4
</li>
<li>Category 5
</li>
</ul>
</li>
<li>Sub Menu 3
</li>
<li>Sub Menu 4
</li>
<li>Sub Menu 5
</li>
</ul>
</li>
<li>Menu 4
</li>
<li>Menu 5
</li>
<li>Menu 6
</li>
<li>Contact Us
</li>
</ul>
</nav>

CSS Drop-Down Menu issues

I'm making a wordpress theme and I have some problems with the menu codification. My menu has sub-menus but they are displaying in the wrong way, And I don't know what to do to make them look like a Drop-Down menu. Here's the link to my site.
Would you mind giving me a CSS code (only) for a really simple dropdown menu? In my website, the menu with sub-categories is 'TV Shows' and the Subcategories are 'Pretty Little Liars', 'Resurrection', and 'Chasing Life'. I need a CSS to make them drop-down from 'Tv Shows'.
This is my CSS Code for the links
#menu {
height:55px;
background-color: #000;
width:100%;
top:0px;
left:0px;
z-index:101;
text-align:center;
text-transform:uppercase;
position:relative;
}
.menulinks {
float:right;
}
#menucontainer {
margin: 0 auto;
width:900px;
font-family: 'Open Sans', sans-serif;
}
#menucontainer a {
color:#fff;
}
#menucontainer a:hover {
color:#fff;
}
#menucontainer ul {
list-style: none;
padding:7px;
color:#A4A4A4;
}
#menucontainer ul a {
color:#848484;
}
#menucontainer li a {
color:#848484;
}
#menucontainer li {
display: inline;
margin-right:3px;
margin-left:3px;
padding:3px;
color:#848484;
}
Try This. fiddle here
ul {
text-align: left;
display: inline;
margin: 0;
padding: 15px 4px 17px 0;
list-style: none;
border: 1px solid;
}
ul li {
display: inline-block;
margin-right: -4px;
position: relative;
padding: 15px 20px;
background: #fff;
cursor: pointer;
}
ul li:hover {
background: #262222;
color: #fff;
}
ul li ul {
padding: 0;
position: absolute;
top: 48px;
left: 0;
width: 150px;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
display: none;
opacity: 0;
visibility: hidden;
}
ul li ul li {
background: #262222;
display: block;
color: #fff;
text-shadow: 0 -1px 0 #000;
}
ul li ul li:hover {
background: #a1a1a1;
}
ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
<ul>
<li>Home</li>
<li>Menu1</li>
<li>Menu2
<ul>
<li>Sub Menu</li>
<li>Another Sub Menu</li>
<li>And Anthor Sub Menu</li>
</ul>
</li>
<li>Menu3</li>
<li>Menu4</li>
</ul>
The answer is more than just CSS. You have to make sure the HTMl is built to accept both. You have to have a ul tag within a ul tag to cause the secondary drop down.
Here is a codepen link for exactly what your looking for I think:
<h1>Simple Pure CSS Drop Down Menu</h1>
<nav id="primary_nav_wrap">
<ul>
<li class="current-menu-item">Home</li>
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4
<ul>
<li>Deep Menu 1
<ul>
<li>Sub Deep 1</li>
<li>Sub Deep 2</li>
<li>Sub Deep 3</li>
<li>Sub Deep 4</li>
</ul>
</li>
<li>Deep Menu 2</li>
</ul>
</li>
<li>Sub Menu 5</li>
</ul>
</li>
<li>Menu 2
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
</ul>
</li>
</ul>
</nav>
Good luck!

How to position absolute element in a container?

I am trying to make a menu with sub-menu under an element by giving the main menu's il relative position and the ul inside it absolute position. Why does the sub-menu treats the to ul as it's container and not it's parent li?
the HTML:
<nav>
<ul>
<li>Menu Item 1</li>
<li>Menu Item 2
<ul>
<li>Submenu Item 1</li>
<li>Submenu Item 2</li>
<li>Submenu Item 3</li>
</ul>
</li>
<li>Menu Item 3</li>
<li>Menu Item 4</li>
<li>Menu Item 5</li>
</ul>
</nav>​
the CSS:
nav a {
float: left;
padding: 10px;
border: solid 1px #C0C0C0;
margin-left: -1px;
}
nav li {
position: relative;
}
nav li ul {
position: absolute;
}
​
http://jsfiddle.net/6pgHa/
It may be because it is your anchor tags that are floated left and therefore defining the position, not the li elements.
Change the nav a selector to nav li and this fixes it. The li elements don't really have a position in your current code.
I've updated your fiddle to make the menu work the way I guess you want it to: http://jsfiddle.net/6pgHa/3/
nav {
width: 600px;
margin: 50px auto 0;
}
nav > ul li {
float: left;
position: relative;
}
nav > ul li a {
float: left;
padding: 10px;
border: solid 1px #C0C0C0;
display:inline-block;
}
nav > ul li ul {
position: absolute;
top:50px;
left:-9999px;
}
nav > ul li:hover ul {
left:0;
}

pure css vertical menu with submenu

I did my research and was able to replicate what I'm looking for, well kind of--I just need help with a more specific vertical, pure CSS, menu.
I want my sub-menu popup to appear 10px to the left of the a not the li attribute like most example found on the internet. I'm also looking for the most simple, pure CSS, type of menu--Nothing fancy.
Here's what I've done so far:
<div id="nav">
<ul class="top-level">
<li>This is a long text
<ul class="sub-level">
<li>Sub Menu Item 1</li>
<li>Sub Menu Item 2</li>
<li>Sub Menu Item 3</li>
<li>Sub Menu Item 3</li>
</ul>
</li>
<li>About</li>
<li>Contact me here</li>
<li>Help
<ul class="sub-level">
<li>Sub Menu Item 1</li>
<li>Sub Menu Item 2</li>
<li>Sub Menu Item 3</li>
</ul>
</li>
</ul>
my css:
#nav {border:1px solid cyan;}
/* top level menu */
#nav ul.top-level {border:1px solid red;}
#nav ul.top-level li {position:relative;}
/* sub level menu */
#nav ul.sub-level {border:1px solid yellow;}
#nav ul.sub-level {display:none;} /* hide */
/* hover the sub menu*/
#nav ul.top-level li:hover .sub-level {display: block; position:absolute; top:5px;}
How do I make it so the sub level menu pops up when I hover the a HTML anchor, not the li, and 10px to the left of the clicked a anchor?
Thanks.
Try this one and I think it will help
HTML
<div id="nav">
<ul class="top-level">
<li>This is a long text
<ul class="sub-level">
<li>Sub Menu Item 1</li>
<li>Sub Menu Item 2</li>
<li>Sub Menu Item 3</li>
<li>Sub Menu Item 3</li>
</ul>
</li>
<li>About</li>
<li>Contact me here</li>
<li>Help
<ul class="sub-level">
<li>Sub Menu Item 1</li>
<li>Sub Menu Item 2</li>
<li>Sub Menu Item 3</li>
</ul>
</li>
</ul>
</div>
​
CSS
#nav {font-size:0.75em; width:150px;}
#nav ul {margin:0px; padding:0px;}
#nav li {list-style: none;}
ul.top-level {background:#FFFFFF;}
ul.top-level li {
border: #FF0000 solid;
border-width: 1px;
}
#nav ul.sub-level {border:1px solid yellow;}
#nav a {
color: #000000;
cursor: pointer;
display:block;
height:25px;
line-height: 25px;
text-indent: 10px;
text-decoration:none;
width:100%;
}
#nav a:hover{
text-decoration:underline;
}
#nav li:hover {
background: #f90;
position: relative;
}
ul.sub-level {
display: none;
}
li:hover .sub-level {
background: #999;
border: #fff solid;
border-width: 1px;
display: block;
position: absolute;
left: 75px;
top: 5px;
}
ul.sub-level li {
border: none;
float:left;
width:150px;
}
#nav .sub-level {
background: #FFFFFF;
}