pure css nav bar with child links

Started on the old forum in January 2015 · view the original in the archive · old links redirect here

13 replies · 255 views · started 11 years ago · 1 min read

4 participants
Ghost Senior · 366 posts #1

been designing a nav bar with child links with pure css for my new script but also for abit of fun. and thought i would share my workings here incase someone else wanted a pure css solution without js.

heres what i got so far. css

body {background-color:#111111; font-family:Verdana, sans-serif; font-size:0.8em; color:#408080;}
a {color:#408080;}
.nav {background-color:#1a1a1a; border:1px solid #408080; margin:5px; border-radius:7px;}
.nav ul {margin:0; padding:0;}
.nav ul li {display:block; float:left; position:relative;}
.nav ul li:hover {background-color:#0a0a0a;}
.nav ul li:hover > ul {display:block; position:absolute;}
.nav ul li a {text-decoration:none; color:#408080;}
.nav ul li ul {border-radius:7px; border:1px solid #408080; background-color:#1a1a1a; display:none; left:0px; width:250px; margin-top:6px;}
.nav ul li ul li {width:241px;}
.nav ul li ul li:hover {width:241px; background-color:#0a0a0a;}
.nav-first-parent-link {padding:6px 8px 6px; border-right:1px solid #408080; border-radius:7px 0px 0px 7px;}
.nav-middle-parent-link {border-right:1px solid #408080; padding:6px 8px 6px;}
.nav-last-parent-link {padding:6px 8px 6px; border-right:1px solid #408080; border-radius:0px 7px 7px 0px;}
.nav-single-child-link {padding:4px; border-radius:7px 7px 7px 7px;}
.nav-first-child-link {padding:4px; border-radius:7px 7px 0px 0px; border-bottom:1px solid #408080;}
.nav-middle-child-link {border-bottom:1px solid #408080; padding:4px;}
.nav-last-child-link {padding:4px; border-radius:0px 0px 7px 7px;}
.nav-padder {height:0px; clear:both;}

HTML

<div class="nav">
<ul>
<!-- Show use of a parent with no child -->
<li class="nav-first-parent-link">
 <a href="#">Amazing</a>
</li>
<!-- Show use of a parent with a single child -->
<li class="nav-middle-parent-link">
<a href="#">CSS</a>
<ul>
<li class="nav-single-child-link"><a href="#">More CSS Links</a></li>
</ul>
</li>
<!-- Show use of a parent with 2 childs -->
<li class="nav-middle-parent-link">
<a href="#">Dropdown</a>
<ul>
<li class="nav-first-child-link"><a href="#">More Dropdown Links</a></li>
<li class="nav-last-child-link"><a href="#">More Dropdown Links</a></li>
</ul>
</li>
<!-- Show use of a parent with 3 childs or more -->
<li class="nav-middle-parent-link">
<a href="#">Menu</a>
<ul>
<li class="nav-first-child-link"><a href="#">More Menu Links</a></li>
<li class="nav-middle-child-link"><a href="#">More Menu Links</a></li>
<li class="nav-last-child-link"><a href="#">More Menu Links</a></li>
</ul>
</li>
</ul>
 <!-- A Padder To Clear Floats, Nothing More -->
<div class="nav-padder"></div>
</div>

DEMO http://codepen.io/anon/pen/dPvMzy

i would like to attempt to add sub child links also. but atm having issues solving this. if anyone wants to attempt it and solve it before me. please let me know thanks.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Ghost Senior · 366 posts #2

i have updated the pen with new improvements to css so no need to set a class for any of the <li> of parent or child anymore. also added sub levels within sub levels within sub levels. Enjoy...

live demo + code: http://codepen.io/anon/pen/yyMjoJ

HTML

<!-- nav bar with sub levels within sub levels within sub levels etc.. -->
<div class="nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Amazing</a>
<ul>
<li><a href="#">More Amazing Child Links</a></li>
<li><a href="#">More Amazing Child Links</a></li>
<li><a href="#">More Amazing Child Links</a></li>
<li><a href="#">More Amazing Child Links >></a>
<ul>
<li><a href="#">More Amazing Grandchild Links >></a>
<ul>
<li><a href="#">More Amazing Great Grandchild Links</a></li>
<li><a href="#">More Amazing Great Grandchild Links</a></li>
<li><a href="#">More Amazing Great Grandchild Links</a></li>
<li><a href="#">More Amazing Great Grandchild Links</a></li>
</ul>
</li>
<li><a href="#">More Amazing Grandchild Links</a></li>
</ul>
</li>
<li><a href="#">More Amazing Child Links</a></li>
</ul>
</li>
<li><a href="#">CSS</a>
<ul>
<li><a href="#">More CSS Child Links</a></li>
<li><a href="#">More CSS Child Links</a></li>
<li><a href="#">More CSS Child Links</a></li>
</ul>
</li>
<li><a href="#">Dropdown</a>
<ul>
<li><a href="#">More Dropdown Child Links</a></li>
<li><a href="#">More Dropdown Child Links >></a>
<ul>
<li><a href="#">More Dropdown Grandchild Links</a></li>
<li><a href="#">More Dropdown Grandchild Links</a></li>
<li><a href="#">More Dropdown Grandchild Links</a></li>
<li><a href="#">More Dropdown Grandchild Links</a></li>
<li><a href="#">More Dropdown Grandchild Links</a></li>
</ul>
</li>
<li><a href="#">More Dropdown Child Links</a></li>
<li><a href="#">More Dropdown Child Links</a></li>
<li><a href="#">More Dropdown Child Links</a></li>
</ul>
</li>
<li><a href="#">Menu</a></li>
</ul>
<div class="nav-padder"></div>
<div>

CSS

body {background:#111111; font-family:"HelveticaNeue","Helvetica Neue",Helvetica,Arial,sans-serif; font-size:0.8em; color:#408080;}
a {color:#408080;}
/** nav bar with sub levels within sub levels within sub levels etc.. */
.nav {background:#1a1a1a; border:1px solid #408080; margin:5px; border-radius:7px;}
.nav ul {list-style:none; float:left; margin:0; padding:0;}
.nav ul a {display:block; text-decoration:none;}
.nav ul li {position:relative; float:left; margin:0; padding:6px 8px 6px; border-right:1px solid #408080;}
.nav ul li:hover {background:#0a0a0a;}
.nav ul li:hover > ul {display:block;}
.nav ul li:first-of-type {border-radius:7px 0px 0px 7px;}
.nav ul ul {display:none; position:absolute; top:100%; left:0; border:1px solid #408080; background:#1a1a1a; padding:0;}
.nav ul ul li:first-of-type {float:none; width:250px; border-top:none;}
.nav ul ul li {float:none; width:250px; border-top:1px solid #408080; padding:4px; border-right:none;}
.nav ul ul a {text-decoration:none;}
.nav ul ul ul {top:0; left:100%;}
.nav-padder {height:0px; clear:both;}
👍 1
Share
<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Ghost Senior · 366 posts #4

nice man. looks good, im gonna be re-creating the mysqli forums script i started to make before i had to dissapear.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Ghost Senior · 366 posts #6

GumSlone wrote: the floating header has some strange margin/padding

i noticed that aswell. i also recently noticed that the header sometimes sticks in wrong place when resizing window and header gets compressed. it sometimes ends in middle of page stopping you viewing part of site. if i can reproduce it ill post a ss here

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
kevk3v Senior · 332 posts #7

Yeah guys, this is the first time I'm doing that sticky stuff, I used javascript to check if the height from the top passed the element then put it fixed in the same position it's in from getting the width from the left and height, that's why it's weird when you resize. Anyone have any tips how to make this great? the only way I can think of is positioning an invisible element where the content should be sticky, but if there's an easier way, please let me know 🙂

Mobile chat, iphone chat, android chat, chat, rooms http://www.aiochat.com

kevk3v Senior · 332 posts #8

Ghost wrote: nice man. looks good, im gonna be re-creating the mysqli forums script i started to make before i had to dissapear.

That could be fixed if I add a setInterval to the function, but I think i'll go with the invisible element, unless you guys have some tips?

Mobile chat, iphone chat, android chat, chat, rooms http://www.aiochat.com

Ghost Senior · 366 posts #9

this is what happened this time: last time it was centered in middle of screen this time it flopped to top though.

also have you thought about a css solution.? responsive designs can be done using just css also now.

Untitled.png

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Ghost Senior · 366 posts #10

take alook here. http://codepen.io/search?q=sticky+header&limit=all&depth=everything&show_forks=false plenty of examples of css/jquery skicky headers etc.

if choose to use any of these. remember to wrap the jquery properly. the site uses prewrap functions to execute your jquery so users who leave these pens dont include the function wrap

example: $(document).ready(function(){ //jquery here. }

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
kevk3v Senior · 332 posts #11

Ghost wrote: this is what happened this time: last time it was centered in middle of screen this time it flopped to top though.

also have you thought about a css solution.? responsive designs can be done using just css also now.

That's because the function only runs once, I have to run it more than once to update the position like I said lol. I want to do my own! or $(function(){

});

Mobile chat, iphone chat, android chat, chat, rooms http://www.aiochat.com

GumSlone Administrator Principal · 1,690 posts #12

kevk3v you can do it only with css: position:fixed; top:0; margin:0 auto; z-index:1000;

the content below the header should have margin-top: {=height of the header}

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Ghost Senior · 366 posts #14

wapxtech wrote: nice work gum ill use it =) thanks

what you going to use that is gumslones? lol

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close