vertical css dropdown menu in one column? - css

I have created a vertical navigational menu in css with two sub-menus.
But I can't figure out how to position them in one column so that they work properly.
Is this possible?
html
<ul>
<li>works
<ul>
<li>something</li>
<li>something</li>
<li>something</li>
<ul>
<li>Category 1</li>
<li>Category 2</li>
<li>Category 3</li>
<li>Category 4</li>
<li>Category 5</li>
</ul>
<li>something</li>
<li>something</li>
</ul>
</li>
<li>photos
<ul>
<li>something</li>
<li>something</li>
</ul>
</li>
<li>friends</li>
<li>contact</li>
</ul>
</div></html>
css
#menu {
font-size: 14px;
font-family: "Courier New", Courier, monospace;
}
#menu ul {
margin: 0px;
list-style-type: none;
}
#menu ul li {
position: relative;
}
#menu ul li a {
line-height: normal;
color: #000;
text-decoration: none;
}
#menu ul li ul {
display: none;
position: absolute;
top: 0px;
left: 180px;
float: left;
z-index: 99999;
width: 180px;
}
#menu ul li ul li {
min-width: 180px;
}
#menu ul li ul ul {
float: left;
top: 0px;
}
#menu ul li:hover > ul { display:block;
}

First of all your html structure is messy. the clean structure could be something like this:
<div id="menu">
<ul>
<li>
works
<li>
works subcategory
<ul>
<li>something</li>
<li>something</li>
<li>something</li>
</ul>
</li>
<li>Category 1</li>
<li>Category 2</li>
<li>Category 3</li>
<li>Category 4</li>
<li>Category 5</li>
</li>
<li>something</li>
<li>something</li>
<li>
photos
<ul>
<li>something</li>
<li>something</li>
</ul>
</li>
<li>friends</li>
<li>contact</li>
</ul>
</div>
You had mistakes in closing tags,..
And i suggest you to use css resets while making dropdown menus. because user-agent predefined styles get you in trouble (try Normalize.css)
In CSS: you don't need to float the 2nd-level ul blocks and also setting list items position property to relative and using top and left properties for children ul is not a good solution.
I styled your menu a little bit and it looks fine. you can view it here: http://codepen.io/anon/pen/sdomr

Related

css Hover li tag select inside first ul only not in select inside ul

CSS hover li tag inside the first ul select, Not select the first ul inside ul tag.
For example below the code.
ul li:hover ul {display: block;}
ul li ul {
position: absolute;
width: 200px;
display: none;
}
ul li ul li {
background: #555;
display: block;
}
<ul>
<li> ( hover this li)
Dropdown Link
<ul> ( Select this ul only )
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3
<ul> ( not select this ul )
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</li>
</ul>
</li>
</ul>
Your Answer is :
<ul>
<li class="link1"> ( hover this li)
<a href="#" >Dropdown Link</a>
<ul class="first-ul"> ( Select this ul only )
<li>Link 1</li>
<li>Link 2</li>
<li class="link2"><a href="#" >Link 3</a>
<ul class="second-ul" > ( not select this ul )
<li>Link 1</li>
<li>Link 2</li>
<li><a href="#" >Link 3</a></li>
</ul>
</li>
</ul>
</li>
</ul>
CSS
.link1:hover .first-ul{display: block;}
.first-ul {
position: absolute;
width: 200px;
display: none;
}
.link2:hover .second-ul{display: block;}
.second-ul {
position: absolute;
width: 200px;
display: none;
}
.second-ul, .first-ul {
background: #555;
display: none;
}
.secondUL{display:none}
View Code in Codepen: Open

CSS Child selector is confusing me

CSS
#nav > li {
list-style:none;
letter-spacing:3px;
}
<ul id="nav">
<li>Home</li>
<li>About US</li>
<li>Services
<ul>
<li>Web Development</li>
<li>Mobile Development</li>
<li>Consultancy</li>
</ul>
</li>
</ul>
I am using child selector to make the list style none, which only unstyles children list items. But, letter-spacing property is adding spacing to the grandchildren list items. It is confusing me.
The default behaviour of letter-spacing, text-* and font-* are to inherit from the parent. So you have reset on your children:
#nav > li {
list-style: none;
letter-spacing: 3px;
}
#nav > li li {
letter-spacing: normal;
}
<ul id="nav">
<li>Home</li>
<li>About US</li>
<li>Services
<ul>
<li>Web Development</li>
<li>Mobile Development</li>
<li>Consultancy</li>
</ul>
</li>
</ul>
#nav > li {
list-style:none;
letter-spacing:3px;
ul{
list-style:none;
letter-spacing:0;
}
}
or
#nav > li {
list-style:none;
letter-spacing:3px;
ul{
li{
list-style:none;
letter-spacing:0;
}
}
}

Semantically correct separators in list (directly in HTML, not CSS generated)

To achieve this layout of a fully justified menu list, I can not use CSS pseudo-classes to display separators between list items; instead, I have to put the separator directly in the HTML.
Since according to HTML5 standard in an <ul> only <li> and script-supporting elements are allowed, I made the below code. It is valid HTML5 but it seems quirky to me. Any concerns?
ul {
list-style: none;
padding: 0;
margin: 0;
display: flex;
justify-content: space-between;
}
li.home {
padding: 0;
}
li,
script::after {
vertical-align: middle;
padding-top: 10px;
}
nav {
border-bottom: 1px solid black;
border-top: 1px solid black;
height: 40px;
}
script.separator {
display: block;
}
script.separator::after {
content: "*";
}
<nav id="main-menu">
<ul>
<li class="home">
<img src="http://dummyimage.com/40x40/000/fff">
</li>
<script class="separator"></script>
<li class="second">Item 1</li>
<script class="separator"></script>
<li>Item 2</li>
<script class="separator"></script>
<li>One more Item</li>
<script class="separator"></script>
<li>Another Item</li>
<script class="separator"></script>
<li class="last">Contact</li>
</ul>
</nav>
Replace the <script> with another <li> and simply assign a style to it with
ul li:nth-of-type(even) {
display: block;
content: "*";
vertical-align: middle;
padding-top: 10px;
}
This will have the same effect but will look much neater on the code view.
<nav id="main-menu">
<ul>
<li class="home">
<img src="http://dummyimage.com/40x40/000/fff">
</li>
<li></li>
<li class="second">Item 1</li>
<li></li>
<li>Item 2</li>
<li></li>
<li>One more Item</li>
<li></li>
<li>Another Item</li>
<li></li>
<li class="last">Contact</li>
</ul>
</nav>
You may have to tweak the actual CSS in the rule above to suit your look and feel but as a concept I think it's neater and cleaner to have all <li> elements and then use CSS to intelligently select all of the correct ones. This also reduces the number of class=" ... " laying around too.
You can also potentially add further rules so that for example you do not do the seperator CSS on the last of type, so the final li would never be the seperator either:
ul li:nth-of-type(even), ul li :not(:last-of-type) {
display: block;
content: "*";
vertical-align: middle;
padding-top: 10px;
}
I'm not sure this is the exact layout you're after, but can you not use display: table and a border?
ul {
margin: 0;
padding: 0;
list-style: none;
display: table;
width: 100%;
}
li {
display: table-cell;
text-align: center;
}
li:not(:last-child) {
border-right: 1px solid #333;
}
<ul>
<li>Menu 1</li>
<li>Menu 2</li>
<li>Menu 3</li>
<li>Menu 4</li>
<li>Menu 5</li>
</ul>

CSS: Showing an unsorted list in a tile-like style

I have an unsorted list which I want do display in a tile-like style using CSS.
This is my list:
<ul>
<li>Area 1</li>
<ul>
<li>Topic 1</li>
<li>Topic 2</li>
<li>Topic 3</li>
<li>Topic 4</li>
</ul>
<li>Area 2</li>
<ul>
<li>Topic 5</li>
<li>Topic 6</li>
</ul>
</ul>
This is the needed Output:
Unfortunately I cannot post images here (due to my low reputation).
+---------------------------+
| Area 1 |
+---------------------------+
+-------+ +-------+ +-------+
|Topic 1| |Topic 2| |Topic 3|
+-------+ +-------+ +-------+
+-------+
|Topic 4|
+-------+
+---------------------------+
| Area 2 |
+---------------------------+
+-------+ +-------+
|Topic 5| |Topic 6|
+-------+ +-------+
In a first step it would be enough if the list is only these 2 steps deep.
I checked several approaches using CSS, but failed.
Can someone help me please with the solution or an approach?
Best regards,
Stefan
- LIVE DEMO
- RESPONSIVE
Corrected HTML:
<ul>
<li><span>Area 1</span>
<ul>
<li>Topic 1</li>
<li>Topic 2</li>
<li>Topic 3</li>
<li>Topic 4</li>
</ul>
</li>
<li><span>Area 2</span>
<ul>
<li>Topic 5</li>
<li>Topic 6</li>
</ul>
</li>
</ul>
CSS:
ul{
width:300px;
list-style:none;
padding:0;
text-align:center;
overflow:auto;
}
ul > li{
margin-bottom:15px;
}
li span{
display:block;
clear:both;
background:#5A9BD5;
padding:15px 0;
}
ul ul li{
float:left;
width:90px;
margin:15px 15px 0 0;
}
ul ul li:nth-child(3){
margin-right:0;
}
ul li a{
background:#5A9BD5;
display:block;
width:100%;
padding:15px 0;
}
There's a few approaches you could take. One is to use floats, like so:
ul li {
clear: left;
}
ul li ul li {
clear: none;
float: left;
width: 33.33%;
}
Another is display: inline-block. But note you'd need to remove any whitespace and line breaks from between the list items (in the HTML):
ul li li {
display: inline-block;
width: 33.33%;
}
Note IE7 doesn't do inline-block. To make it work there (if necessary) add this:
.ie7 ul li li {
display: inline;
zoom: 1;
}
(I use conditional commments to add the .ie7 class to the HTML element)
First thing. You cannot have a <ul> directly under a parent <ul>.
<ul>
<li>Area 1</li>
<!-- this is wrong -->
<ul>
<li>Topic 1</li>
<li>Topic 2</li>
<li>Topic 3</li>
<li>Topic 4</li>
</ul>
<li>Area 2</li>
<!-- this is wrong -->
<ul>
<li>Topic 5</li>
<li>Topic 6</li>
</ul>
</ul>
So you can change the markup this way:
<ul>
<li>Area 1</li>
<li class="tiles">
<ul>
<li>Topic 1</li>
<li>Topic 2</li>
<li>Topic 3</li>
<li>Topic 4</li>
</ul>
</li>
<li>Area 2</li>
<li class="tiles">
<ul>
<li>Topic 5</li>
<li>Topic 6</li>
</ul>
</li>
</ul>
Now the CSS:
ul, li {display: block; list-style: none; margin: 0; padding: 0;}
ul {background: #fff;}
li {line-height: 50px; width: 100%; text-align: center; background: #66f; margin: 5px;}
li.tiles ul li {width: 33%; float: left; margin: 5px 0; background: none;}
li.tiles ul {overflow: hidden;}
li.tiles {text-align: left; margin: 0;}
a {color: #fff; text-decoration: none; display: block; background: #66f; margin: 5px;}
Fiddle: http://jsfiddle.net/praveenscience/LjCHW/1/
Without the use of class attribute.
HTML
<ul>
<li>Area 1</li>
<li>
<ul>
<li>Topic 1</li>
<li>Topic 2</li>
<li>Topic 3</li>
<li>Topic 4</li>
</ul>
</li>
<li>Area 2</li>
<li>
<ul>
<li>Topic 5</li>
<li>Topic 6</li>
</ul>
</li>
</ul>
CSS
ul, li {display: block; list-style: none; margin: 0; padding: 0;}
ul {background: #fff;}
li {line-height: 50px; width: 100%; text-align: center; background: #66f; margin: 5px;}
li ul li {width: 33%; float: left; margin: 5px 0; background: none;}
li ul {overflow: hidden;}
a {color: #fff; text-decoration: none; display: block; background: #66f; margin: 5px;}
Fiddle: http://jsfiddle.net/praveenscience/LjCHW/2/

CSS display li in special format

If I have an Ul that contains
<UL class="level2">
<li>
<a>Link1</a>
</li>
<li>
<a> Link2</a>
</li>
<li>
<a> Link 3</a>
</li>
<li class="hasChildren">
<a>Navigation</a>
<ul>
<li>Navigation 1 </li>
<li>Navigation 2</li>
<li>Navigation 3 </li>
<li>Navigation 4</li>
<li>Navigation 5</li>
</ul>
</li>
I need to display the li that don't have children in block and I need the li that haschildren to be displayed inline next to it not under it any idea
I need to have :
Link1 Navigation
Link2 Navigation1 Navigation4
Link3 Navigation2 Navigation5
Navigation3
Something like this?
CSS:
ul {
width: 600px;
position: relative;
margin: 0;
padding: 0;
}
li {
display: block;
width: 300px;
background: Red;
}
li.hasChildren {
position: absolute;
top: 0;
right: 0;
background: Green;
}
li.hasChildren ul {
width: auto;
margin: 0;
padding: 0;
}
li.hasChildren ul li {
background: Green;
}
HTML:
<ul>
<li>Item #1</li>
<li>Item #2</li>
<li class="hasChildren">
Item #3
<ul>
<li>Sub-item #1</li>
<li>Sub-item #2</li>
</ul>
</li>
<li>Item #4</li>
<li>Item #5</li>
</ul>

Resources