positioning my css menu [closed] - html

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to update on of my company's website menu and i've managed to do everything except when the menu's extend their width on mouse hover, level 2 and level 3 lists stack up on top of each other. I understand its because i made the positioning of absolute rather than relative to avoid level 1 menus from moving up and down on hover. Anyone has any suggestion on how i can manage to show submenus and keep the main menu not move around on mouse hove?
here's the my jsfiddle code, http://jsfiddle.net/#&togetherjs=hyRjkxLPGH.
thanks!

There were a number of issues with your Fiddle. First of all, the only element that should be inside a <ul> tag is an <li> tag, not text. Secondly, you were using absolute positioning nearly correctly, but you need to give each li a relative position.
I've included a basic (somewhat stripped down) solution for you below. You can also find this at this fiddle.
You can reinsert your styles pretty easily I'm sure, perhaps use more classes and less CSS selectors (have you ever used SCSS?)
HTML
<ul class="menubar">
<li>menu1
<ul>
<li>submenu1
<ul>
<li>submenu11</li>
<li>submenu12</li>
<li>submenu13</li>
</ul>
</li>
<li>submenu2
<ul>
<li>submenu21</li>
<li>submenu22</li>
<li>submenu23</li>
</ul>
</li>
<li>submenu3
<ul>
<li>submenu31</li>
<li>submenu32</li>
<li>submenu33</li>
</ul>
</li>
</ul>
</li>
<li>menu2
<ul>
<li>submenu1
<ul>
<li>submenu11</li>
<li>submenu12</li>
<li>submenu13</li>
</ul>
</li>
<li>submenu2
<ul>
<li>submenu21</li>
<li>submenu22</li>
<li>submenu23</li>
</ul>
</li>
<li>submenu3
<ul>
<li>submenu31</li>
<li>submenu32</li>
<li>submenu33</li>
</ul>
</li>
</ul>
</li>
</ul>
CSS
.menubar, .menubar ul {
list-style: none;
margin: 0;
padding: 0;
}
.menubar > li {
display: inline-block;
position: relative;
}
.menubar li ul {
display: none;
}
.menubar li:hover > ul {
display: block;
position: absolute;
width: 100px;
}
.menubar li:hover > ul > li {
position: relative;
}
.menubar > li > ul > li ul {
position: absolute;
top: 0;
left: 100px;
}

Related

How do I select the third nested list and every other of its sibling lists in css [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
Is there a better way to write this:
#container ul ul ul
I need to target the third nested list and every other one after that?
You can do it in several ways. If you simply want to assign a css property from the third ul element upwards (i.e.: 3 ul, 4 ul , n ul) the easiest way would be to use an asterisk *.
#container ul ul > * {
font-style: italic
}
I have used several selectors in the following example. Which one you actually use is up to you.
#container ul ul ul {
color: green;
}
div ul > ul > ul {
font-size: 30px;
}
.third {
text-decoration: underline;
}
#container ul ul > * {
font-style: italic;
font-weight: bold;
}
blockquote {
color: gray;
}
<blockquote>I need to target the third nested list and every other one after that?
</blockquote>
<div id="container">
<ul>
<li>1 ul</li>
<ul>
<li>2 ul</li>
<ul class="third">
<li>3 ul</li>
<ul class="third">
<li>4 ul</li>
<ul>5 ul</ul>
</ul>
</ul>
</ul>
</ul>
</div>
Maybe use the > selector in css or assign the element a className, but I don't think there is an easier way to do that rather than these two ways.
By using >, you will directly specify the child element (e.g. A) of the parent element (e.g.B) and won't select the appended child element of A.
Check more here: What is the difference between '>' and a space in CSS selectors?
You are trying to get the nested ul, so things like nth-of-child or nth-of-type will not work for you.
#ul>ul>ul {
background: red
}
<ul id='ul'>
<ul>
<ul>
Yes
</ul>
</ul>
</ul>
Confused with difference between space and > in css?
Check this as example:
#ul>ul>ul {
background: red
}
<ul id='ul'>
<ul>
<ul>
This will be considered
</ul>
<div>
<ul>This will not be considered</ul>
</div>
</ul>
</ul>
#ul ul ul {
background: red
}
<ul id='ul'>
<ul>
<ul>
This will be considered
</ul>
<div>
<ul>This will be considered as well</ul>
</div>
</ul>
</ul>
the other answers seem to have ignored your other criterion, which was: 'and every other one after that'
this answers that question
#content ul ul ul:first-of-type,
#content ul ul ul ~ ul:nth-of-type(odd) { color: green;}
<div id="content">
<ul>
<li>one</li>
<ul>
<li>two</li>
<ul>
<li>three.1</li>
</ul>
<ul>
<li>three.2</li>
</ul>
<ul>
<li>three.3</li>
</ul>
<ul>
<li>three.4</li>
</ul>
<ul>
<li>three.5</li>
</ul>
</ul>
</ul>
</div>
yeap it's messy-looking css (not really, IMHO, but to each their own) but if you don't have access to add classes to the markup, this is the way.

CSS nested hover events for a menu system [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Here is my CodePen: http://codepen.io/ScottBeeson/pen/rxquJ
So basically when you hover over an entity, a menu slides up. I'm trying to create a submenu for each menu item that basically mirrors the menu functionality, but slides down from the bottom of the menu. Here is an image of what it should look like:
And here is my current HTML:
<div class="entity">
<span class="menu"><div>A</div><div>B</div><div>C</div></span>
</div>
I can think of a couple ways to do this with JQuery, but I'm wondering if it's possible to do with CSS. Obviously, populating the menu will be via javascript, but I'm trying to use CSS as much as possible. So to put it in question form: If I put a static div with a class of "submenu" inside my entity, is there any way with CSS/LESS to trigger it when I hover over a div inside the menu?
I don't use LESS, so I can't help you with that.
However, I made you this code, which displays the menu on hover, and the submenu when you hover the menu items. You could set up the structure for a entity like this:
<div class="entity">
<ul>
<li>A
<ul>
<li>1</li>
<li>2</li>
</ul>
</li>
<li>B
<ul>
<li>1</li>
<li>2</li>
</ul>
</li>
<li>C</li>
</ul>
</div>
And combine it with this CSS:
.entity {
margin: 5px;
position: relative;
display: inline-block;
width: 260px;
height: 200px;
background-color: lightblue;
}
.entity ul {
display: none;
list-style: none;
margin: 0;
padding: 0;
position: absolute;
width: 100%;
background-color: rgb(0,0,0); /*fallback*/
background-color: rgba(0,0,0,.5);
bottom: 0px;
}
.entity li:hover {
background: black;
color: white;
}
.entity:hover > ul { /* only display direct ul child of .entity */
display: block;
}
.entity li {
display: inline-block;
padding: 10px;
}
.entity li > ul {
background: black;
}
.entity li:hover > ul {
display: block;
left: 0;
bottom: -100%;
}
I hope you can add the smooth effects yourself. Good luck.
Ow, and a DEMO
[EDIT]
Made a (bit sloppy though) animation using transitions, check the updated Fiddle.

How to only show a nested UL and hide its parent?

I'm trying to show a nested (sub) list, but hide the parent ULs and LIs through an "active" class so that the sub list looks like the parent list.
The list with the "active" class isn't visible because it inherits display: none from its parent.
Code:
<ul>
<li>
Hidden
<ul>
<li class="active">Visible</li>
</ul>
</li>
</ul>
CSS:
li {
display: none;
}
li.active {
display: block;
}
Fiddle: http://jsfiddle.net/2C8qs
Any ideas?
If you can add span around the hidden text (http://jsfiddle.net/vittore/2C8qs/3/) :
<ul>
<li>
<span>Hidden</span>
<ul>
<li class="active">Visible</li>
</ul>
</li>
</ul>
li span, li li {
display: none;
}
li li.active {
display: block;
}
display: none hides the element and all of its children, that is final and adding display: block to a child won't make it visible again.
This will hide all children, except for the .active element:
ul.parent > li {
display: none;
}
ul.parent > li.active {
display: block;
}
EDIT: Oops, I misread the question. You can do something similar to the above though, if you wrap the other contents in an element.
An ugly CSS trick : http://jsfiddle.net/2C8qs/4/
Instead of using display none/block, I used text-indent, like that :
li {
text-indent: -99999em
}
li.active {
text-indent: 0
}
Note that can only work on inline / text elements.
I know this is very late to this question, but I've found what I would consider a nice solution and thought I'd post it here for whoever might need it in the future.
First of all, wrap all the <li>'s children with <p> (or <div> or anything, it doesn't matter really), but not any sub-<ul>'s. Then, to the child <ul> you want to be visible, add a class called showing. Example (we only want to show the SubSubThing list):
<ul>
<li>
<p>Item</p>
<ul>
<li>
<p>SubItem</p>
<ul>
<li>
<p>SubSubItem</p>
</li>
</ul>
</li>
</ul>
</li>
<li>
<p>Thing</p>
<ul>
<li>
<p>SubThing</p>
<ul class="showing">
<li>
<p>SubSubThing1</p>
</li>
<li>
<p>SubSubThing2</p>
</li>
<li>
<p>SubSubThing3</p>
</li>
</ul>
</li>
</ul>
</li>
</ul>
Then apply this CSS:
ul>li {
list-style:none;
}
ul>li>p {
display: none;
}
ul.showing>li>p {
display:block;
}
/* Without removing padding and margin,
the sublists appear way over to the right */
ul {
margin-left: 0px;
padding-left: 0px;
}
li {
margin-left: 0px;
padding-left: 0px;
}
Now, only the <li>'s who are direct descendants of ul's with a showing class will display at all. The other items in the list will use no space.
To get the sublists to show bullet points would be easy via CSS, and to show different sublists it is simple to just use jQuery to set showing on the appropriate ul.
Hope that helps.
Obligatory JSFiddle
So the reason you can't simply hide the first li and reveal the second is because the second is contained by the first — you can't reveal and element that is contained by a hidden one.
Therefore, if you put the li element within a span that you'd like to hide, it becomes easy. I've created a class-free version for you here: http://jsfiddle.net/rgpnr6mh/3/
<ul>
<li><span>Hidden</span>
<ul>
<li>Visible</li>
</ul>
</li>
</ul>
I'm assuming you don't want to display the bullets:
ul {
list-style-type:none
}
li span{
display: none;
}
li li {
display: block;
}

HTML Submenu disappearing when trying to hover over it

i created an HTML menu that has a submenu, however when i hover over the main menu and it shows the submenu, i cant get access to it. It disappears before i can move my mouse down to navigate to it. I dont know what i am doing wrong, maybe with my code someone can help out. thanks
#header ul { position: absolute; top: 88px; left: 0; }
#header ul li { display: inline;}
#header ul li a { font-size: 11px; text-decoration: none; text-transform: uppercase; margin-right: 20px; color: #fff; line-height: 2em;}
#header ul li ul{ position: static;display: none; z-index: 999;top:150%;}
#header ul li:hover a { font-weight:bold; color: #000000}
#header ul li:hover ul { display: block; position:absolute;}
Edited to show HTML
<ul>
<li>Home
</li>
<li>
HRMS
<ul>
<li>
Position
</li>
<li>
COA
</li>
<li>
Employee
</li>
<li>
Estate
</li>
</ul>
</li>
<li>
Employee Maintenance
</li>
<li>
Payroll
</li>
<li>
Data Transfer
</li>
<li>
Reports
</li>
<li>
Administration
</li>
<li>
Help
</li>
</ul>
The problem is a disconnect between the main and the children <ul>.
In your style ul li ul { position: static;display: none; z-index: 999;top:150%;}
The top:150%; creates the disconnect (so you hover over nothing instead of hitting the submenu's :hover logic when you hover from ul li to ul li ul). You can try using padding-top instead:
ul li ul{ position: static;display: none; z-index: 999; padding-top:10px;}
Edit: Here is a working nav menu example you can look at.
Edit 2: Looks like it works in Firefox and Chrome, but not IE.
You will need a direct child selector, somewhere along the line of:
li:hover > ul { display: block; (...) }
This means that the child <ul> in the list will be displayed when it's direct parent <li> has been hovered upon.
I wrote one article about Dropdown menu in Portuguese, but I guess it will help you with google translate or something like that.
Check it out: http://www.linhadecodigo.com.br/artigo/3474/menu-em-css-menu-dropdown-horizontal-com-html5-e-css3.aspx

Simple CSS fly out list, not so simple?

I had in my mind that it would not be hard to add an anchor tag that, when hovered or clicked, would cause a CSS flyout with more links in it to appear.
As it is now, a set of normal anchor tags are inside of a span which is inside of an li. I want to add this hover flyout link to be in the same location, the same as one of the links but instead of being a normal link, do the flyout. I found all kinds of code online but none of it seems to work in this location:
<li>
Introduction to Something
<span>
<a target="_blank" href="http://http...file.html">Watch Slideshow</a>
View File
<a target="_blank" href="http://file....pdf">Print</a>
FLY OUT MENU ITEM
</span>
</li>
I've posted a demo, that's not hugely different to #jeffkee's answer, over at jsbin, to show how deep it's possible to go with flyout menus and how simple they can be.
The (x)html is copied below (with notes):
<ul>
<li>home</li>
<li>products
<ul>
<li>CPUs
<ul>
<li><a href="#">AMD<a>
<ul>
<li>AM2</li>
<li>AM3</li>
</ul>
</li>
<li>Intel</li>
</ul>
</li>
<li>Motherboards</li>
<li>PSUs</li>
<li>Hard drives
<ul>
<li>HDD</li>
<li>SSD</li>
</ul>
</li>
</ul>
</li>
<li>Tracking</li>
</ul>
The CSS is as below:
ul {list-style-type: none; width: 8em; border: 1px solid #000; padding: 0;}
/* just to set the base-line for the ul, but note the width. It's important. */
ul li {position: relative; border-top: 1px solid #000; margin: 0; padding: 0; }
/* the position: relative is used to allow its child elements to be absolutely positioned */
ul li:first-child {border-top: 0 none transparent; }
/* to avoid a two-pixel border on the first li (1px li-border + 1px ul-border) */
ul li:hover {background-color: #f90; }
/* just to aid visually */
ul ul {position: absolute; top: -1px; left: 8em; display: none; }
/* sets up all ul elements beneath the parent ul, the -px is to counter the movement forced by the border, bear in mind that the li:first-child doesn't *have* a border, so adjust to taste */
ul > li:hover > ul {display: block; }
/* makes the nested list appear if the parent-li is hovered */
I don't really see how the fly out is structured. Flyouts are generally set within a link so that when the mother link is hovered, the child shows up..
Check out the most recent flyout menu I did on http://www.feelfabulous.ca/oldindex.php and break down the HTML/CSS. You can do it without any javascript etc. Here's hte HTML structure I have (simplified):
<ul id="menu">
<li>Home</li>
<li>About
<div class="submenu_container">
<ul>
<li>our story</li>
<li>meet our team</li>
<!--<li>press</li>-->
</ul>
</div>
</li>
<li>Spa Menu</li>
<li>Party Packages</li>
<li>Beauty Kits</li>
<li>Goody Bags</li>
</ul>
So .submenu_container is set to display:none, and then #menu li:hover .submenu_container is set to display:block. that's the basic idea of a flyout tyep fo menu. And of course the .submenu_container is absolute positioned so it doesn't affect the shape and form of the page when it pops up.