Using Flexbox to make a navbar with a dropdown menu [duplicate] - html

We all have hard times with positioning absolute divs :S
In my case its horizontal sub-menus with this css:
ul.children{
display:none;
position:absolute;
}
ul.children li{
position:relative;
height:60px;
float:none;
}
li.page_item_has_children:hover > ul.children{
display:inline;
}
As you can see on the picture whole submenu moves to the left for 50% of the parents width...
I tried everything and created just a bigger mess xD
So if anyone can help me out with this I will be very thankful :)
HTML:
<li class="page_item page-item-2 page_item_has_children">
About
<ul class='children'>
<li class="page_item page-item-39">
About</li>
<li class="page_item page-item-41">
Contact us</li>
</ul>
</li>
I can't change html cause its wordpress theme :S

Try something like this:
ul {
list-style-type: none;
padding: 0;
}
.page_item_has_children {
position: relative;
}
ul.children {
display: none;
position: absolute;
top: 100%;
left: 0;
padding: 0;
min-width: 200px; /* or whatever makes sense */
}
ul.children li {
height: 60px;
}
.page_item_has_children:hover > ul.children {
display: block;
}
The important piece here is to make sure the 'page_item_has_children' element is relatively positioned and that the child 'ul' is absolutely positioned.
JS Fiddle

Update
ul.children {
display: none;
}
ul.children li {
position: relative;
height: 60px;
float: none;
}
li.page_item_has_children:hover > ul.children {
display: inline;
}
li {
float: left;
list-style-type: none;
width: 5em;
}
<ul>
<li class="page_item page-item-2 page_item_has_children">
About
<ul class='children'>
<li class="page_item page-item-39">
About</li>
<li class="page_item page-item-41">
Contact us</li>
</ul>
</li>
<li class="page_item page-item-2 page_item_has_children">
More info
<ul class='children'>
<li class="page_item page-item-39">
Item 1</li>
<li class="page_item page-item-41">
Item 2</li>
<li class="page_item page-item-41">
Item 3</li>
</ul>
</li>
</ul>

Related

how to stretch a UL across the browser window

I have an UL that acts as my navigation bar on my web page. currently it is all the way to the left of the screen. I would like it to be centered and have the background color stretch all the way to the left and right, Like below. what would I need to change to stretch the UL across the page like the top example image?
Here is what it looks like currently:
Here is my code:
.parent {
display: block;
position: relative;
float: left;
line-height: 30px;
background-color: #1D3567;
}
.parent a {
margin: 10px;
color: #FFFFFF;
text-decoration: none;
}
.parent:hover>ul {
display: block;
position: absolute;
}
.child {
display: none;
}
.child li {
background-color: #1D3567;
line-height: 30px;
border-bottom: #CCC 1px solid;
border-right: #CCC 1px solid;
width: 100%;
}
.child li a {
color: #FFFFFF;
}
ul {
list-style-type: none;
margin: 0;
padding: 0px;
min-width: 15em;
}
ul ul ul {
left: 100%;
top: 0;
margin-left: 1px;
}
li:hover {
background-color: #95B4CA;
}
.parent li:hover {
background-color: #95B4CA;
}
<ul id="nav_ul">
<li class="parent">Home</li>
<li class="parent">Outlook Web</li>
<li class="parent">Production
<ul class="child">
<li class="parent">Hennig South</li>
<li class="parent">Hennig Enclosure Systems</li>
<li class="parent">Hennig South</li>
<li class="parent">Factory Andon</li>
<li class="parent">Web Docs</li>
</ul>
</li>
<li class="parent">IT
<ul class="child">
<li>Knowledge Base</li>
<li>Submit a Ticket</li>
<li class="parent">Archived Links</li>
</ul>
</li>
<li class="parent">Office Directories
<ul class="child">
<li>Hennig</li>
<li>AME</li>
</ul>
</li>
<li class="parent">Hennig Parts</li>
<li class="parent">Factory Andon</li>
<li class="parent">Business Cards</li>
<li class="parent">Reports
<ul class="child">
<li class="parent">Global Shop<span class="expand">»</span>
<ul class="child">
<li>Inventory Report</li>
<li>Sales Report</li>
<li>Quotes Report</li>
<li>Work Order Report</li>
<li>Part Where Used Report</li>
</ul>
</li>
<li class="parent">Ndustrios<span class="expand">»</span>
</li>
</ul>
</li>
</ul>
Make your ul a flex container in which you align the children centered, and apply the background-color to this element instead of the li children by adding this rule:
#nav_ul {
width: 100%;
display: flex;
justify-content: center;
background-color: #1D3567;
}
.parent {
position: relative;
float: left;
line-height: 30px;
background-color: #1D3567;
}
.parent a {
margin: 10px;
color: #FFFFFF;
text-decoration: none;
}
.parent:hover>ul {
display: block;
position: absolute;
}
.child {
display: none;
}
.child li {
line-height: 30px;
border-bottom: #CCC 1px solid;
border-right: #CCC 1px solid;
width: 100%;
}
.child li a {
color: #FFFFFF;
}
ul {
list-style-type: none;
margin: 0;
padding: 0px;
min-width: 15em;
}
#nav_ul {
width: 100%;
display: flex;
justify-content: center;
background-color: #1D3567;
}
ul ul ul {
left: 100%;
top: 0;
margin-left: 1px;
}
li:hover {
background-color: #95B4CA;
}
.parent li:hover {
background-color: #95B4CA;
}
<ul id="nav_ul">
<li class="parent">Home</li>
<li class="parent">Outlook Web</li>
<li class="parent">Production
<ul class="child">
<li class="parent">Hennig South</li>
<li class="parent">Hennig Enclosure Systems</li>
<li class="parent">Hennig South</li>
<li class="parent">Factory Andon</li>
<li class="parent">Web Docs</li>
</ul>
</li>
<li class="parent">IT
<ul class="child">
<li>Knowledge Base</li>
<li>Submit a Ticket</li>
<li class="parent">Archived Links</li>
</ul>
</li>
<li class="parent">Office Directories
<ul class="child">
<li>Hennig</li>
<li>AME</li>
</ul>
</li>
<li class="parent">Hennig Parts</li>
<li class="parent">Factory Andon</li>
<li class="parent">Business Cards</li>
<li class="parent">Reports
<ul class="child">
<li class="parent">Global Shop<span class="expand">»</span>
<ul class="child">
<li>Inventory Report</li>
<li>Sales Report</li>
<li>Quotes Report</li>
<li>Work Order Report</li>
<li>Part Where Used Report</li>
</ul>
</li>
<li class="parent">Ndustrios<span class="expand">»</span>
</li>
</ul>
</li>
</ul>

How to get a nav menu bar to scroll horizontally

I've got a nav bar with a few li tags that fall under each other when on a smaller screen size. I want the nav bar to remain the same height and scroll horizontally when on a smaller device screen but everything I'm trying is not working. Can someone shine a light on what is wrong here please!
HMTL:
<nav role="navigation" class="site-navigation main-navigation">
<div class="menu">
<ul>
<li class="page_item page-item-40">Buy</li>
<li class="page_item page-item-41">Rent</li>
<li class="page_item page-item-7">Sell</li>
<li class="page_item page-item-9 current_page_item">Valuations</li>
<li class="page_item page-item-15">Property Management</li>
<li class="page_item page-item-11">About Us</li>
<li class="page_item page-item-13">Contact Us</li>
</ul>
</div>
</nav>
CSS:
#media screen and (max-width : 480px){
div.menu ul {
display: inline-block;
max-height: 34px;
overflow: auto;
white-space: nowrap;
}
div.menu a {
display: inline-block;
text-align: center;
}
}
But this currently is not working.
horizontal scroll menu mobile
<style type="text/css">
#media screen and (max-width : 480px){
div.menu ul {
display: inline-block;
width: 85%;
display: inline-block;
padding-bottom:20px;
white-space: nowrap;
overflow-y: hidden;
overflow-x: scroll;
-webkit-overflow-scrolling: touch;
-ms-overflow-style: -ms-autohiding-scrollbar;
}
div.menu li {
display: inline-block;
}
div.menu li a {
text-align: center;
}
}
</style>
<nav role="navigation" class="site-navigation main-navigation">
<div class="menu">
<ul>
<li class="page_item page-item-40">Buy</li>
<li class="page_item page-item-41">Rent</li>
<li class="page_item page-item-7">Sell</li>
<li class="page_item page-item-9 current_page_item">Valuations</li>
<li class="page_item page-item-15">Property Management</li>
<li class="page_item page-item-11">About Us</li>
<li class="page_item page-item-13">Contact Us</li>
</ul>
</div>
</nav>

Absolute and relative positioning in menu

We all have hard times with positioning absolute divs :S
In my case its horizontal sub-menus with this css:
ul.children{
display:none;
position:absolute;
}
ul.children li{
position:relative;
height:60px;
float:none;
}
li.page_item_has_children:hover > ul.children{
display:inline;
}
As you can see on the picture whole submenu moves to the left for 50% of the parents width...
I tried everything and created just a bigger mess xD
So if anyone can help me out with this I will be very thankful :)
HTML:
<li class="page_item page-item-2 page_item_has_children">
About
<ul class='children'>
<li class="page_item page-item-39">
About</li>
<li class="page_item page-item-41">
Contact us</li>
</ul>
</li>
I can't change html cause its wordpress theme :S
Try something like this:
ul {
list-style-type: none;
padding: 0;
}
.page_item_has_children {
position: relative;
}
ul.children {
display: none;
position: absolute;
top: 100%;
left: 0;
padding: 0;
min-width: 200px; /* or whatever makes sense */
}
ul.children li {
height: 60px;
}
.page_item_has_children:hover > ul.children {
display: block;
}
The important piece here is to make sure the 'page_item_has_children' element is relatively positioned and that the child 'ul' is absolutely positioned.
JS Fiddle
Update
ul.children {
display: none;
}
ul.children li {
position: relative;
height: 60px;
float: none;
}
li.page_item_has_children:hover > ul.children {
display: inline;
}
li {
float: left;
list-style-type: none;
width: 5em;
}
<ul>
<li class="page_item page-item-2 page_item_has_children">
About
<ul class='children'>
<li class="page_item page-item-39">
About</li>
<li class="page_item page-item-41">
Contact us</li>
</ul>
</li>
<li class="page_item page-item-2 page_item_has_children">
More info
<ul class='children'>
<li class="page_item page-item-39">
Item 1</li>
<li class="page_item page-item-41">
Item 2</li>
<li class="page_item page-item-41">
Item 3</li>
</ul>
</li>
</ul>

100% height doesn't work on dropdown menu

Check live- http://uposonghar.com/testsite/
When i add more items to the menus - the menu container does not expand and items get outside the menu.
Screenshot-
Code-
<LI id=navPreretire><A title="Pre Retirement Procedures" href="http://pencproc/" target=_blank><SPAN class=singleLine>Pre Retirement Procedures</SPAN> </A>
<!--Mega menu drop-down part1, the div have to stay like that-->
<div class="dropdown">
<!--until here-->
<Ul>
<div class="dropdown_3rd_lvl1"><li >Plan Benifit Payment Procedures
<ul>
<li >Communications</li>
<li >Manage Payments</li>
<li >Manage Pension Benefits</li>
<li >Reports</li>
</UL></li></div>
<div class="dropdown_3rd_lvl2"><li >Pension Services Procedures
<ul>
<li >Communications</li>
<li >Manage Group Benefits</li>
<li >Manage Payments</li>
<li >Manage Pension Benefits</li>
<li >Reports</li>
<li >Reports</li>
<li >Reports</li>
<li >Reports</li>
<li >Reports</li>
<li >Reports</li>
<li >Reports</li>
<li >Reports</li>
</UL></li></div>
</ul>
<!--Menu ends-->
</div>
<!--Menu ends-->
</LI>
CSS-
.dropdown{
display: none;
BACKGROUND-COLOR: #886d53;
clear: both;
}
.dropdown>ul>li{
clear: both;
float: left;
}
#menu li:hover .dropdown{
position: absolute;
display: block;
height:100%;
min-width: 430px;
margin: 38px auto;
padding-top: 5px;
}
.dropdown ul li{
clear: both;
}
#menu .dropdown ul li:hover{
clear: both;
}
Remove unnecessary height, position:absolute.
#menu li:hover .dropdown {
/*height: 200px;*/
}
#menu ul ul {
height: auto;
}
.dropdown_3rd_lvl1 {
/*position: absolute;*/
/*left: 0*/
/*margin-left: -30px;*/
float: left;
}
.dropdown_3rd_lvl2 {
/*position: absolute;*/
/*margin-left: 170px;*/
float: left;
}
The problem is, at least partly, due to your #menu ul, which applies a height: 38px to the bar, but also gets applied to the ul descendant a few layers deeper.
The entire menu bar seems a bit too complex for what you are trying to achieve, and could be done in a much simpler way.

How to position a drop down list nav bar inside header in html css

I am having trouble stacking up the elements inside the nav bar next to each other.
Here's how it looks:
I want the nav elements to be side by side of each other.
Here's HTML code
HTML:
<div id="navbar">
<ul>
<li>COMPANY
<ul>
<li>OVERVIEW</li>
<li>CEO MESSAGE</li>
<li>LEADERSHIP</li>
<li>INVESTORS</li>
<li>AFFILIATION</li>
<li>CONTACT</li>
</ul>
</li>
<li>PRODUCTS
<ul>
<li>OVERVIEW</li>
<li>PRODUCTS</li>
<li>APPLICATION</li>
<li>USAGE</li>
</ul>
</li>
<li>PARTNER PROGRAM
<ul>
<li>PARTNER PROGRAM</li>
<li>BECOME A PARTNER</li>
</ul>
</li>
<li>LEARN ABOUT WGIG
<ul>
<li>OVERVIEW</li>
<li>VIDEOS</li>
<li>INDUSTRY ARTICLES</li>
<li>WHITE PAPERS</li>
</ul>
</li>
<li>MEDIA CENTER
<ul>
<li>LATEST NEWS</li>
<li>PRESS RELEASES</li>
<li>MEDIA COVERAGE</li>
<li>EVENTS</li>
<li>AFFILIATION</li>
<li>MEDIA KIT</li>
</ul>
</li>
</ul>
</div>
Here's the CSS
CSS:
#navbar {
position: relative;
margin-left: 25%;
border: 5px solid green ;
width: 55%;
height: 100%;
}
#navbar ul {
display: inline-block;
border-radius: 10px;
position: absolute;
}
#navbar li {
list-style: none;
position: relative;
display: inline-block;
float: left;
}
#navbar ul li a {
text-decoration: none;
text-align: center;
color:lime;
height:30px;
width:40px;
display: block;
}
#navbar ul ul {
display:none;
position:absolute;
}
#navbar ul li:hover ul {
display:block;
}
The first <ul> should be relatively positioned. Setting your <a> at a fixed 40px wide isn't such a great idea (nor is it necessary) as most of the text within is more than 40px. Also: your menu items will wrap (stack) unless you set a min-width on their container.
See here: http://jsfiddle.net/RKRWQ/1/