CSS Submenu item bound position to menus parent element size - html

I created a CSS menu which has menu items and submenu items. These submenu items are actually big divs which gets shown when the user hovers the menu item.
They work pretty well but I have one problem:
If the submenu is wider than the whole menu, it overflows and may break the pages width in the worst case.
When the submenu reaches the right side of the menu, it should "stick" at right:0px.
Is there any possibility to limit the position of an element to it's grandparental's width? Resp. is there a way to achieve my goal in pure CSS? I don't really want to use JavaScript for that.
This is the HTML structure and an excerpt of the important CSS:
.navi {
height:60px;
width:350px;
position:relative;
background-color:#DEDEDE;
}
.navi ul {
list-style:none;
display:inline-block;
margin-left:50px;
}
.navi ul li {
float:left;
height:60px;
cursor:pointer;
position:relative;
}
.navi ul li a {
height:100%;
line-height:60px;
display:inline-block;
}
.navi ul li:hover .submenu {
display:block;
}
.navi ul li .submenu {
width:200px;
height:100px;
display:none;
position:absolute;
background-color:#F3F3F3;
}
<div class="navi">
<ul>
<li>
<a>a navigation link</a>
</li>
<li>
<a>a navigation link</a>
<div class="submenu">
<!-- content -->
</div>
<div class="arrow"></div>
</li>
<div class="clearfix"></div>
</ul>
</div>
Here is a fiddle showing my menu.

Related

Why isnt my last child li elemens padding not be removed?

My navigation anchor tag is the last child element of the navigation I tried removing the padding-left but it wont remove however when I try to add padding it adds it. Any thoughts about this?
<div class="main_nav">
<div class="logo">
<img src="Logo/logo.png">
</div>
<nav>
<ul>
<li>Home</li>
<li>Aboutus</li>
<li>News</li>
<li>Portfolio</li>
<li>Blog</li>
<li>Shop</li>
<li>Contact</li>
</ul>
</nav>
</div
.main_nav{
width:1200px;
margin:0 auto;
height:90px;
background-color:orange;
}
.logo{
clear:both;
float:left;
padding-top:15px;
}
nav ul{
float:right;
padding-top:33px;
}
nav li{
display:inline;
padding-right:40px;
background-color:yellow;
}
nav li:last-child a{
padding-right:0px;
}
From your code you actual want this:
nav li{
display:inline;
padding-right:40px; /* padding applied */
background-color:yellow;
}
nav li:last-child { /* padding on last list item removed */
padding-right:0px;
}
You haven't applied padding to the anchor so it can't be removed with:
nav li:last-child a{
padding-right:0px;
}
You stated:
My navigation anchor tag is the last child element of the navigation.
Actually, it isn't (and it sort of is). In a list such as this every anchor link is the last child of the li as it's also the first and only child.
nth-of statements (which include first and last etc.) always refer the children of a parent element.
So the li:last-child will always be the last child of it's parent ul.

Nav menu "pulled left" using Bootstrap: submenu also pulled left

I am trying to make a navigation bar with drop down menus. I've created the navigation bar and used bootstraps "pull-left" class to move it to the left. But the dropdown menu I have created using jQuery is now also moved the left since the HTML code is contained in the div marked as "pull-left" I have googled and tried out stuff for a few hours, but I couldn't quite find a solution.
The HTML for the Navigation bar
<div id = "nav">
<div class = "container" >
<div class = "pull-left ">
<img class = "logo-image" src = "Logo2.png" />
</div>
<ul class = "pull-left">
<li class "logo">Home</li>
<li>About</li>
<li>
Projects
<ul>
<li>Stealth Game </li>
</ul>
</li>
<li>Gallery</li>
<li>Tutorials</li>
</ul>
<ul class = "pull-right">
<li> Follow me</li>
</ul>
</div>
The CSS
#nav li{
display:inline;
padding-right : 5px;
}
#nav ul ul {
display:none;
position:fixed;
z-index:999;
}
#nav li li {
float: auto;
}
#nav li a {
width:150px;
display: inline-block;
text-align:center;
color:#000;
margin-right:5px;
height:35px;
line-height:35px;
text-decoration:none;
font-size:80%;
border:1px solid #ccc;
}
#nav ul{
list-style-type:none;
margin:0;
padding:0;
}
#nav li li a {
background:#EBE7E6!important;
text-align:left;
height:auto;
line-height:1;
width:150px;
padding:8px 20px 8px 22px;
border:1px solid #D0D0D0;
border-top:none;
margin-right:0;
}
And the JavaScript
$(document).ready(function() {
$("#nav li:has(ul)").hover(function(){
$(this).find("ul").slideDown();
}, function(){
$(this).find("ul").hide();
});
});
So the actual problem is, that the submenu, that is appearing doesn't appear under the menu it is opened from, but is also pulled to the left since I used this bootstrap class to move my navigation bar to the left.
Two things resolve this:
#nav li {
display:inline-block;
...
}
#nav ul ul {
position:absolute;
...
}
Demo
You have:
#nav ul ul {
display:none;
position:fixed;
z-index:999;
}
The position:fixed on the submenu is probably what is giving you positioning problems.
edit:
you want to use position: absolute instead.
another edit, set position: relative on the parent LI, you will also need to set the display to block and because they are no longer inline elements you will want to float them left:
#nav li{
position: relative;
display: block;
float: left;
padding-right: 5px;
}
Fiddle with this working: https://jsfiddle.net/DTcHh/

Dropdown wont show up on hover of list item

For some reason I cant get my pure css drop menu to work. I have tried everything I can think of but to no avail. I know it has to be something small or I am missing a descendant selector or something. Can anyone figure out this problem.
HTML
<div class="section navi">
<div class="row">
<div class="col_06">
<ul id="nav">
<li>Home</li>
<li>About</li>
<li>Practice Areas</li>
<li>Mediation & Arbitration</li>
<li>Attorneys</li>
<li>Offices</li>
<li><a href="/"><span>▼</span>More
<ul class="subNav"></a>
<li><a>News & Accolades</a></li>
<li><a>Careers</a></li>
<li><a>Administration</a></li>
<li><a>Disclaimer</a></li>
<li><a>Community</a></li>
<li><a>The Harmonie Group</a></li>
<li><a>Reported Cases</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
CSS
#nav ul{
background:#fff; <span class="code-comment">/* Adding a background makes the dropdown work properly in IE7+. Make this as close to your page's background as possible (i.e. white page == white background). */</span>
background:rgba(255,255,255,0); <span class="code-comment">/* But! Let's make the background fully transparent where we can, we don't actually want to see it if we can help it... */</span>
list-style:none;
position:absolute;
left:-9999px; <span class="code-comment">/* Hide off-screen when not needed (this is more accessible than display:none;) */</span>
}
#nav ul li{
padding-top:1px; <span class="code-comment">/* Introducing a padding between the li and the a give the illusion spaced items */</span>
float:none;
}
#nav ul a{
white-space:nowrap; <span class="code-comment">/* Stop text wrapping and creating multi-line dropdown items */</span>
}
#nav li:hover ul{ <span class="code-comment">/* Display the dropdown on hover */</span>
left:0; <span class="code-comment">/* Bring back on-screen when needed */</span>
}
#nav li:hover a{ <span class="code-comment">/* These create persistent hover states, meaning the top-most link stays 'hovered' even when your cursor has moved down the list. */</span>
background:#6b0c36;
text-decoration:underline;
}
#nav li:hover ul a{ <span class="code-comment">/* The persistent hover state does however create a global style for links even before they're hovered. Here we undo these effects. */</span>
text-decoration:none;
}
#nav li:hover ul li a:hover{ <span class="code-comment">/* Here we define the most explicit hover states--what happens when you hover each individual link. */</span>
background:#333;
}
All those <span>elements in your CSS are breaking it. HTML elements ruin your CSS. To comment use a /*I am a comment*/ Remove all of the <span>'s and your drop down works great:
#nav ul{
background:#fff;
background:rgba(255,255,255,0);
list-style:none;
position:absolute;
left:-9999px;
}
#nav ul li{
padding-top:1px;
float:none;
}
#nav ul a{
white-space:nowrap;
}
#nav li:hover ul{
left:0;
}
#nav li:hover a{
background:#6b0c36;
text-decoration:underline;
}
#nav li:hover ul a{
text-decoration:none;
}
#nav li:hover ul li a:hover{
background:#333;
}
JSFIddle Demo
You have HTML code in your CSS. Remove the tags.

How do I make a horizontal navbar using CSS, that supports hover and respects the edges of the container?

I am trying to make a CSS based nav bar that converts a UL/LI list (including sub UL/LI lists that display on hover) into a horizontal nav bar and a sub horizontal nav bar. My current implementation (see picture) works except the sub list is using position:absolute to get its position below the hovered element. I believe that is the source of my problem, as the absolute positioned sub menu doesnt respect the edges of the container, so if the browser gets too small it bleeds off the edge of the container while the top level menu wraps. The other problem i'm having is that it doesnt expand the page vertically when the top level menu wraps, so even though its taking up more space the paragraph following doesnt move down and gets overlapped.
Does anybody have any CSS tips or know of any good examples of a menu like the one I'm trying to make?
Link to image:
http://i47.tinypic.com/288tbn7.png
To this day, the article "Son of Suckerfish Dropdowns" by HTML Dog is still a classic on clean CSS menus with hover.
http://www.htmldog.com/articles/suckerfish/dropdowns/
Hey now create this menu easily used to ul li simply as like this
HTML
<ul class="menu">
<li>Home</li>
<li>Home2</li>
<li>Home3
<ul class="submenu">
<li>Submenu</li>
<li>Submenu</li>
<li>Submenu</li>
</ul>
</li>
<li>Home4</li>
<li>Home5</li>
</ul>
Css
.menu{
display:block;
list-style:none;
border-bottom:solid 1px red;
float:left;
}
.menu > li{
float:left;
position:relative;
}
.menu > li + li{
border-left:solid 1px red;
margin-left:10px;
}
.menu > li > a{
display:block;
margin-left:10px;
}
.submenu{
display:none;
list-style:none;
position:absolute;
top:20px;
left:-39px;
white-space:nowrap;
}
.menu > li:hover .submenu{
display:block;
}
.submenu li{
display:inline;
}
.submenu li + li {
border-left:1px solid green;
margin-left:10px;
}
.submenu a{
display:inline-block;
vertical-align:top;
margin-left:10px;
}
Live demo
and now change css according to your layout this is basic step

Center Nav Bar Elements

<div id="wrapper" class="hfeed">
<div id="access">
<div id="menu">
<ul>
<li class="page_item page-item-2"><a title="About" href="/?page_id=2">About</a></li>
<li class="page_item page-item-20"><a title="Support" href="/?page_id=20">Support</a></li>
<li class="page_item page-item-22"><a title="Links" href="/?page_id=22">Links</a></li>
<li class="page_item page-item-47"><a title="About" href="/?page_id=47">About</a></li>
</ul>
</div>
</div><!-- #access -->
</div>
My current CSS:
div#menu {
background:#000;
height:1.5em;
margin:1em 0;
}
div#menu ul,div#menu ul ul {
line-height:1;
list-style:none;
margin:0;
padding:0;
}
div#menu ul a {
display:block;
margin-right:1em;
padding:0.2em 0.5em;
text-decoration:none;
}
div#menu ul ul ul a {
font-style:italic;
}
div#menu ul li ul {
left:-999em;
position:absolute;
}
div#menu ul li:hover ul {
left:auto;
}
Is my menu however I'm not sure how to centre it in the middle of the page.
Like what hitautodestruct said there are two ways, but you need to describe what you want.
Do you want the whole navigation block center aligned?
If so you could do this in the css (change the width to what ever you neeed it to be):
div#menu {
width:500px;
margin: 0px auto;
}
Do you want the content within the naviagtion to be centered?
If so then add this to your css:
ul {
text-align:center;
}
If these aren't what you are looking for then can you describe in more detail please! Cheers
try to use this css attribute: text-align and vertical-align
http://www.w3schools.com/css/pr_pos_vertical-align.asp
http://www.w3schools.com/CSS/pr_text_text-align.asp
I dont sure is your html is entry page or not so I can not edit the css for you, try to do that by yourself if possible.
If you want to center it on the x grid you would use the simple technique of auto margins:
Set your body so that it aligns text to the center:
body{text-align:center;}
Set your container with auto margin left and right and also so it aligns all text back to the left:
#wrapper{margin:0 auto;text-align:left;}
On the vertical side it's a bit trickier follow this article:
http://phrogz.net/css/vertical-align/index.html