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>
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.
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...
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;}
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
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 🙂
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
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(){