CSS Dropdown Menu, Child Exceed Parent Width - css

I'm trying to build a centrally-aligned dropdown menu, using HTML5 and CSS3. I could use jQuery to assist, if I must. This must work in IE8+. At the moment, my menu is centrally-aligned, and the drop-downs work, but the width of the dropped-down item (div) is the same width as the li above.
<div class="menu">
<ul class="level0">
<li class="level0">
<a href=" http://domain.com/">
<span>home</span>
</a>
<div class="sub">
<div class="sub-column">
<a href="http://domain.com/customer-service">
<span>Customer Service</span>
</a>
</div>
<div class="sub-column">
<a href="http://domain.com/privacy-policy">
<span>Privacy Policy</span>
</a>
</div>
</div>
</li>
<li class="level0">
<a href=" http://domain.com/about">
<span>About Us</span>
</a>
<div class="sub">
<div class="sub-column">
<div>
<p>Blah blah blah, some random text goes here.</p>
</div>
</div>
</div>
</li>
</ul>
</div>
And the CSS (tried to compact it for easy viewing).
.menu {
clear:both; display:block; max-width:100%; margin:0 auto; padding:0;
border:1px solid #ddd; border-bottom:7px solid #323131; text-align:center;
}
.menu ul {
display:block; max-width:100%; height:3.2em; margin:0 auto;
padding:0; list-style-type:none; text-align:center;
}
.menu ul li {display:inline-block; margin:0; padding:0; height:3.2em;
line-height:3.2em; position:relative;
}
.menu ul li > div {display:none; visibility:hidden; position:absolute;
background:#f60; z-index:999; transition:display 0.25s ease;
}
.menu ul li:hover > div {display:inline-block; visibility:visible;
max-width:100%;
}
.menu ul li a {display:block; margin:0; padding:0 2em; font-size:90%;
font-weight:700; text-transform:uppercase; transition:background-color 0.25s ease;
}
.menu ul li a:hover {background-color:#e4e4e4; color:#323131;}
.menu ul li.switcher {display:none;}
How can I get the width of the first line div (.menu ul li > div) to adopt the width of the grandparent element (.menu)? Or at least stretch to fit the contents in using max-width?
Any help is appreciated. Thanks.
~ edit ~
Here's the fiddle: http://jsfiddle.net/Xp6yG/

I think I understand your issue correctly, try the following:
fiddle demo
CSS:
.sub {left:0px;}

As well as using James suggestion of making the .menu position: relative I think you will also need to remove the position: relative on .menu ul li

Thanks to some helpful suggestions, this is as close to working as I've got it: http://jsfiddle.net/Xp6yG/3/.
I changed the display on .menu ul li:hover > div to display:table; and added whitespace:nowrap; and right:50%; to the .menu ul li > div element.
The dropdown appears where I want it and stretches to fit the containing element(s). Thanks very much all!

Related

Absolutely positioned anchor tag inside relatively positioned li not working

I am trying to create a menu as follows. When hover the menu item, it should be animate the height from bottom to top. So I positioned anchor tag absolute and gave bottom 0. When anchor tag is positioned absolute, It does not show menu properly.
css is as follows.
#navigation
{
position:relative;
float:right;
margin-top:55px;
padding-right:45px;
}
#navigation ul
{
text-decoration:none;
list-style:none;
display:inline;
position:relative;
padding:0px;
height:30px;
margin:0px;
}
#navigation ul li
{
position:relative;
float:left;
}
#navigation ul li a
{
position:absolute;
bottom:0px;
padding:10px 5px;
width:79px;
display:block;
text-decoration:none;
background-color:#1c1c1c;
color:rgb(255,255,255);
/*margin:2px;
margin-bottom:0px;*/
text-align:center;
font-family:Tahoma;
/*position:relative;*/
font-size:15px;
}
html is as follows.
<div id="navigation">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Products</li>
<li>Contact Us</li>
</ul>
</div>
http://jsfiddle.net/90up4hz2/
demo - http://jsfiddle.net/victor_007/90up4hz2/1/
the issue is because of position:absolute for the li and position:relative the width becomes 0 try adding fixed with for li
#navigation ul li {
position:relative;
float:left;
height:30px;
width:89px;
}
http://jsfiddle.net/L0r493ag/2/
I created this for you, dont use ul li or just add the follow css to them or to any object
.boxhead
{
color: #afaeae;
text-decoration: none;
font-size: 17px;
font-family: 'Open Sans', sans-serif;
font-weight: 400;
background:#000000;
padding-left:10px;
padding-right:10px;
height:50px;
}
.boxhead:hover
{
color: #7d7d7d;
}
and then
<div id="navigation">
<a class="boxhead" href="index.php">Home</a>
<a class="boxhead" href="about-us.php">About Us</a>
<a class="boxhead" href="products.php">Products</a>
<a class="boxhead" href="contact-us.php">Contact Us</a>
</div>

Float on <li> is not working on absolute position

I want to display (blue) sub menu in one line but somehow it is coming in multiple lines. It works fine if I add fixed width but it should be flexible as this menu will by dynamic and could be change as per user's request.
Please check Fiddle
http://jsfiddle.net/awaises/meXB9/
HTML :
<div class="left-bar">
<ul class="menu">
<li>
For Sale
<ul class="sub-menu">
<li>Residential</li>
<li>Commercial</li>
<li>Industrial</li>
<li>Agriculture</li>
<li>Land</li>
</ul>
</li>
<li>Rent To Own</li>
<li>Key Money</li>
<li>For Sale at Auction</li>
</ul>
</div>
CSS :
body{font:normal 12px/24px arial;}
.left-bar{background:#262626; width:150px; text-align:center; text-decoration}
ul.menu, ul.sub-menu, ul.sub-menu2{margin:0; padding:0; list-style:none;}
ul.menu li{ position:relative; }
ul.menu li a{
color:#adadad;
font-weight:bold;
text-decoration:none;
border-bottom:1px solid #171717;
display:block;
}
ul.sub-menu{
position:absolute;
background:#1ea3d7;
top:2px; left:150px;
}
ul.sub-menu li{float:left;}
ul.sub-menu li a{color:#fff;border:none;padding:0 10px;}
Updated Fiddle
make top:0px; in ul.sub-menu
Update:
use display:inline-flex;
Use following.
.sub-menu {
display: table;
}
.sub-menu li {
/* float: left; */
display: table-cell;
}

CSS .Active background

i'm currentley trying to make a website nav bar, when the page is active i want the navbar background to be the same as the body.
However, when i do this the background only wraps around the text rather than the full height of the navbar.
My HTML
<div id="navbar">
<ul>
<li> Home </li>
<li>Purchase </li>
<li>Contact </li>
<li>Portfolio </li>
</ul>
</div>
My CSS
#navbar ul{
list-style:none;
margin:0;
background-image:url('Images/black.png');
text-align:center;
height:60px;
}
#navbar ul{
list-style-type:none;
text-align:center;
color:#fff;
}
#navbar ul li{
display:inline;
}
#navbar ul li a{
color:#36b6f4;
text-decoration:none;
font-size:30px;
margin:30px;
}
#navbar ul li a.active{
height:60px;
background-image:url('Images/background.jpg');
}
I have create for you a fiddle:
http://jsfiddle.net/RKRHE/1/
I have changed your background images with colors (because I haven't your images).
The problem was display: inline;. You can put float: left;
You can copy my code from fiddle and change background color with you images.
Hope this will help you!

css nav menu dropdown background displays wider than set width ONLY in ie8

This is my first post and I've been researching this issue for two days now but cannot find anything similar and I'm at my wits end with it.
Everything works in all my testing except for IE8. There, my css dropdown menu has a background (set in the main menu as an image) that's wider than the set 128px width of the main nav menu. Each link has its own background, this is a background on the whole dropdown. Even moving it over -40px to line up with the above links still gives me that extra width. I've tried many fixes but nothing seems to work. I'm hoping the combined expertise here can figure this out! I cannot post a picture as I don't have enough reputation here, sorry. Thanks in advance....
Here is the code in my stylesheet followed by HTML5 Code:
#menu{list-style:none; font-size:12px; font-weight:600; float:left; width:100%;
margin:0px auto;position:relative; z-index:5;text-align:center;}
#menu li{float:left; margin-right:2px; position:relative;}
#menu a {display:block; background:#202020 url('images/bgmen.jpg') repeat-x;
width:128px; height:30px; line-height:30px; color:#fff; text-decoration:none;}
#menu a:hover{background:#202020 url('images/bgmenlt.jpg') repeat-x; color:#000;}
/* dropdown*/
#menu ul{background:#000 url('images/bgmen.jpg') repeat-x; background:rgba(255,255,255,0);
list-style:none; position:absolute; left:-9999px;}
#menu ul li{padding-top:1px; float:none;}
#menu ul a{white-space:nowrap;}
#menu li:hover ul{left:-20px;}
#menu li:hover a{background:#202020 url('images/bgmenlt.jpg') repeat-x;}
#menu li:hover ul a{background:#202020 url('images/bgmen.jpg') repeat-x;}
#menu li:hover ul li a:hover{background:#000 url('images/bgmenlt.jpg') repeat-x;}
<ul id="menu">
<li>HOME</li>
<li>BIO</li>
<li>NEWS</li>
<li>GALLERY</li>
<li>TEST
<ul>
<li>test</li>
<li>test</li>
</ul>
</li>
<li>CONNECT
<ul>
<li> <img src="images/fb.jpg" width="20" height="20" alt="" title="" /> Facebook</li>
<li> <img src="images/twitter.jpg" width="20" height="20" alt="" title="" /> Twitter</li>
<li>Contact</li>
</ul>
</li>
<li>CHAR</li>
</ul>
adding margin:0 padding:0 to the #menu ul solved the issue, and changed the position of the dropdown by altering the #menu li:hover ul as below:
#menu ul{
background:#000 url('images/bgmen.jpg') repeat-x;
background:rgba(255,255,255,0);
list-style:none; position:absolute;
left:-9999px;
margin:0;
padding:0;}
#menu li:hover ul{left:20px;}

Inline elements CSS

I want a navigation like structure in the header of my page, so I decided I'd use an inline list however the result it is producing is not what I imagined. It is placing the elements on 3 separate lines.
Element 1 Element 2 Element 3
The above is what I am aiming to achieve, however currently I get,
Element 1
Element 2
Element 3
This is my CSS and HTML snippets:
CSS
#nav li {
display: inline;
}
HTML
<ul id="nav">
<li><button id="left_nav"><</button></li>
<li><div id="day">Monday</div></li>
<li><button id="right_nav">></button></li>
</ul>
Thanks.
Use inline-block instead of inline, you can also use this by float...
By Inline-block,
#nav li {
display: inline-block;
}
By Float,
#nav {
overflow:hidden;
}
#nav li {
float:left; margin-right:10px;
}
Try using the following:
#nav li {
list-style: none;
float: left;
margin-right: 5px;
}
Add this to the CSS for your LI's...
li{ display:inline; }
Use
#nav li {
display: inline-block;
}
Demo: Fiddle
use float:left or float:right aligns side by side
#nav li {
float:left;
}
You have buttons and div inside li give width to div and button that will work.
Because div is block level element and its use all the width of page if you not assign fixed width.
OR
apply float: left; to li that will work too.
The structure is different for different users.
Check the link: http://jsfiddle.net/wWyMW/1/
<div id="nav">
<ul>
<li>
Element <span>1</span>
<div class="clr"></div>
</li>
<li>
Element <span>2</span>
<div class="clr"></div>
</li>
<li>
Element <span>3</span>
<div class="clr"></div>
</li>
</ul>
<div class="clr"></div>
</div>
And The CSS:
*{ matgin:0; padding:0}
ul, li{ list-style:none}
a{ text-decoration:none}
.clr{ clear:both;}
#nav{margin:auto; width:960px;}
#nav ul li{ float:left; margin-left:10px}
#nav ul li:first-child{ margin-left:0}
#nav ul li a{ display:block; background:#f00; color:#0F0; line-height:30px; text-align:center; width:70px; padding:0px 10px; font-weight:bold;}
#nav ul li a span{ display:block; float:right;}
Add this CSS:
#navigation ul
{margin:0px; padding:0px;}
#navigation ul li
{display:inline; height:30px; float:left; list-style:none; margin-left:15px;}

Resources