Nav bar align: Logo on left, links on right - css

I'm having trouble getting the alignments right on a nav bar. I'm trying to figure out how to get the logo part to stay on the left of the nav bar, but put the links on the right side. I've tried using float: right but I can't seem to get it to work on just the links. How can I do this?
https://jsfiddle.net/t46bcayd/1/
<nav>
<ul>
<li>Logo</li>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</nav>

Flexbox is perfect here...no need to change the structure, unless you want to.
body {
margin: 0;
padding: 0;
font-size: 15px;
}
nav {
background-color: black;
margin: 0;
overflow: hidden;
}
nav ul {
margin: 0;
padding: 0;
display: flex;
}
nav ul li {
display: inline-block;
list-style-type: none;
}
nav ul li a {
color: white;
background-color: red;
display: block;
line-height: 3em;
padding: 1em 3em;
text-decoration: none;
}
nav ul li:first-child {
margin-right: auto;
}
nav ul li a.logo {
background-color: green;
}
nav ul li a:hover {
background-color: blue;
}
<nav>
<ul>
<li>Logo
</li>
<li>One
</li>
<li>Two
</li>
<li>Three
</li>
</ul>
</nav>

If you remove the inline-block rule for the list items you can float the first one left and the others right:
li {
float: right;
}
li:first-child {
float: left;
}
jsFiddle example
You'd also need to re-order the list items that are floated right to:
<li>Logo</li>
<li>Three</li>
<li>Two</li>
<li>One</li>

You could use flexbox for this.
<style>
nav{
display:flex;
justify-content: space-between;
align-items: center;
}
</style>
<nav>
Logo
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</nav>
Remember prefixes ... works in IE > 9

Use the float:left property
body {
margin: 0;
padding: 0;
font-size: 15px;
}
nav {
background-color: black;
margin: 0;
overflow: hidden;
}
nav ul {
margin: 0;
padding: 0;
}
nav ul li:not(:first-child) {
float: right;
}
nav ul li {
display: inline-block;
list-style-type: none;
}
nav ul li a {
color: white;
background-color: red;
display: block;
line-height: 3em;
padding: 1em 3em;
text-decoration: none;
}
nav ul li a.logo {
background-color: green;
}
nav ul li a:hover {
background-color: blue;
}
<nav>
<ul>
<li>Logo</li>
<li>Three</li>
<li>Two</li>
<li>One</li>
</ul>
</nav>
I did have to change the order so they showed up right

Related

Multi-level Collapsable Menu - Cannot Reach Item Positioned After Submenu

I am unable to reach the list element that is positioned at level 2, right after the level 3 submenu because when I hover out of the level 3 submenu the whole menu collapses to level 1.
The item that I am unable to reach is titled "Unreachable" in the code.
N.B.: I am able to reach the item if I bypass the level 2 menu, i.e. approach it without triggering level 2. But most of the time, the end user will hover over level 2 before approaching that item and will not be able to reac it.
.side-nav-bar-menu {
position: absolute;
height: 100%;
overflow: hidden;
white-space: nowrap;
width: 19.692em;
padding-top: 0.385em;
}
.side-nav-bar-menu ul {
list-style: none;
padding: 0;
margin: 0;
background-color: red;
}
.side-nav-bar-menu li:hover {
background-color: blue
}
.side-nav-bar-menu ul li ul {
display: none;
}
.side-nav-bar-menu ul li:hover > ul {
display: block;
}
.side-nav-bar-menu ul li ul li {
margin-left: 15px;
}
.side-nav-bar-menu ul li ul li ul li {
margin-left: 30px;
}
<div class="side-nav-bar-menu">
<ul>
<li>
<a>L1</a>
<ul>
<li><a>L2</a></li>
<li>
<a>L2</a>
<ul>
<li><a>L3</a></li>
<li><a>L3</a></li>
</ul>
</li>
<li><a>Unreachable</a></li>
</ul>
</li>
<li>
<a>L1</a>
<ul>
<li><a>L2</a></li>
</ul>
</li>
</ul>
</div>
Updated with new fix
Try this:
.side-nav-bar-menu {
position: absolute;
height: 100%;
overflow: hidden;
white-space: nowrap;
width: 19.692em;
padding-top: 0.385em;
}
.side-nav-bar-menu ul {
list-style: none;
padding: 0;
margin: 0;
background-color: red;
}
.side-nav-bar-menu li:hover {
background-color: blue
}
.side-nav-bar-menu ul li ul {
display: none;
}
.side-nav-bar-menu ul li:hover ul{
display: block;
}
.side-nav-bar-menu ul li ul li {
margin-left: 15px;
}
.side-nav-bar-menu ul li ul li ul li {
margin-left: 30px;
}
<div class="side-nav-bar-menu">
<ul>
<li>
<a>L1</a>
<ul>
<li><a>L2</a></li>
<li>
<a>L2</a>
<ul>
<li><a>L3</a></li>
<li><a>L3</a></li>
</ul>
</li>
<li><a>Reachable</a></li>
</ul>
</li>
<li>
<a>L1</a>
<ul>
<li><a>L2</a></li>
</ul>
</li>
</ul>
</div>

Can't get navigation bar to center

Have tried a lot of different things but can not figure out why it won't center. Any help would be awesome!
nav {
font-size: 18px;
}
ul {
list-style-type: none;
display: inline-block;
}
li {
display: inline-block;
}
li a {
color: #F55F5F;
padding: 10px 15px;
text-decoration: none;
text-align: center;
}
<nav>
<ul>
<li>Home
</li>
<li>Services
<li>About Us
</li>
<li>Contact Us
</ul>
</nav>
You can do two things to center the ul:
Add text-align: center to the nav to center the ul inside it.
Reset the default left padding of ul
See demo below:
nav {
font-size: 18px;
text-align: center;
}
ul {
padding: 0;
list-style-type: none;
display: inline-block;
}
li {
display: inline-block;
}
li a {
color: #F55F5F;
padding: 10px 15px;
text-decoration: none;
text-align: center;
}
<nav>
<ul>
<li>Home
</li>
<li>Services
<li>About Us
</li>
<li>Contact Us
</ul>
</nav>
nav{
text-align: center;
background: green;
}
ul{
padding: 0;
}
just put text align center to nav
nav {
text-align: center;
}

My submenu is not showing when hovering over navigation bar elements (CSS)

I need the submenu to show when hovering over the corresponding navigation element on the navigation bar. For the second level navigation list, I set display to none as follows
nav ul ul {
position:absolute; top: 100%;
background-color: #2b0306;
display: none;
}
and set the the first navigation list to display as inline-block as follows:
nav ul li a {
display: inline-block;
color: #fff;
padding: 10px 20px;
text-decoration: none;
width: 250px;
position: relative;
}
nav ul li a:visited { color: #fff; }
nav ul li a:hover { background-color: #6d0911; }
nav ul ul { position:absolute; top: 100%; background-color: #2b0306; display: none; }
nav ul ul li { position: relative; }
nav ul ul ul { left: 100%; top: 0px; }
As for the nav position, I set it to absolute:
nav {
background-color: rbga(0,0,0,.65);
position: absolute;
top: 0px; left: 0px;
padding: 50px 0 0 0;
width: 100%;
}
nav::after { content: ' '; display: block; clear: both; }
nav ul { list-style: none; margin: 0; padding: 0px; }
nav ul li: hover { background-color: #2b0306; }
nav ul li: hover > ul { display: block; }
/* top-level */
nav > ul { padding-left: 300px; }
nav > ul > li { float: left; }
nav > ul > li > a {
width: auto;
padding: 10px 20px 15px 20px;
}
And this is the HTML code:
<nav>
<ul><!--first level navigation-->
<li><a title="About Us" href="aboutATMC.php" >About Us</a></li>
<li>
<a title="Services" href="#" aria-haspopup ="true">Services</a>
<ul><!--Second level navigation-->
<li><a title="Consultancy" href="#">Consultancy</a></li>
<li>
<a title="Learning & Development Solutions" href="#" aria-haspopup ="true">Learning & Development Solutions</a>
<ul><!--Third level navigation-->
<li><a title="Training & Coaching" href="#">Training & Coaching</a></li>
<li><a title="Learning Material" href="#">Learning Material</a></li>
</ul><!--End of third level-->
</li>
</ul><!--end of second level-->
</li>
<li><a onclick="toggleNavPanel ('contact_panel')" id = "contactus" href="#" >Contact Us <span id="navarrow"> ▾</span></a></li>
</ul><!--End of first level-->
</nav>
Any advice please?
Try this:
/* top menu */
nav ul {
list-style: none;
padding: 0;
margin: 0;
}
nav li a {
padding: 10px;
display: inline-block;
}
nav > ul > li {
display: inline-block;
}
/* sub menu */
nav > ul > li > ul {
display: none;
position: absolute;
background-color: #ccc;
}
nav ul li:hover {
background-color: #ccc;
}
nav ul li:hover ul {
display: block;
}
<nav>
<ul>
<li>
Menu A
<ul>
<li>Submenu 1</li>
<li>Submenu 2</li>
<li>Submenu 3</li>
</ul>
</li>
<li>
Menu B
</li>
</ul>
</nav>
Here I added some HTML code
<nav>
<ul>
<li>
<a href='#'>Test</a>
<ul id='submenu'>
<li>Submenu Test</li>
<li>Submenu Test</li>
<li>Submenu Test</li>
</ul>
</li>
<li><a href='#'>Test</a></li>
<li><a href='#'>Test</a></li>
</ul>
</nav>
<style>
nav {
background-color: rbga(0,0,0,.65);
position: absolute;
top: 0px; left: 0px;
padding: 50px 0 0 0;
width: 100%;
}
nav ul ul {
position:absolute;
top: 100%;
background-color: #2b0306;
display: none;
}
nav ul li:hover > #submenu {
display: block;
}
nav ul li a {
display: inline-block;
padding: 10px 20px;
text-decoration: none;
width: 250px;
position: relative;
}
</style>
If you ever see the box appears as you hover, great!

How to make vertical submenu expands to the right

I'm creating a web with a vertical navigation menu on the left. In this menu, I have several submenus which I would like to be expanded to the right when the mouse hovers the parent item.
Here is the html
<div id="leftmenu">
<ul>
<li>Item</li>
<li>item
<ul>
<li>SubItem</li>
<li>SubItem</li>
</ul>
</li>
<li>Item
<ul>
<li>SubItem</li>
<li>SubItem</li>
<li>SubItem</li>
</ul>
</li>
<li>item
<ul>
<li>SubItem</li>
<li>SubItme</li>
<li>SubItem</li>
</ul>
</li>
<li>item</li>
<li>Item</li>
</ul>
</div>
And here is my CSS
#leftmenu {
float: left;
margin: 30px;
font-weight: bold;
background: linear-gradient(#ffeb99, #ffe066);
border: 0;
border-radius: 10px;
}
#leftmenu ul {
position: relative;
border-radius: 10px;
display: inline-block;
}
#leftmenu ul ul {
display: none;
background: #e7c702;
}
#leftmenu ul li:hover > ul {
display: inline-block;
width: 100%;
height: 100%;
position: absolute;
left: 100%;
}
#leftmenu ul li a {
padding: 15px 30px;
display: block;
color: #757575;
text-decoration: none;
font-size: 15px;
text-transform: uppercase;
}
My problem is when I hover the mouse to an item that has subitems, the subitems appear but it down not in the same line as its parent item. Instead, it appears in the same line of the item the item I'm hovering. I'm learning CSS so I don't want to use any JavaScript in this case.
Please help me with this.
Sorry if I format something wrong since this is my first post here.
Thank you
You must write the <ul> tags before the <a> tags. Since, the <ul> is later going to be positioned as absolute, it is not going to affect the position of <a> tag...
I've edited the css a little bit to make it better, (hope you don't mind):
#leftmenu {
float: left;
margin: 30px;
font-weight: bold;
background: linear-gradient(#ffeb99, #ffe066);
border: 0;
border-radius: 10px;
}
#leftmenu ul {
position: relative;
border-radius: 10px;
display: inline-block;
list-style: none;
padding: 10px;
}
#leftmenu ul ul {
display: none;
background: #e7c702;
position: absolute;
top: 0;
left: 100%;
}
#leftmenu ul li{
position: relative;
}
#leftmenu ul li:hover > ul {
display: block;
}
#leftmenu ul li a {
padding: 15px 30px;
display: block;
color: #757575;
text-decoration: none;
font-size: 15px;
text-transform: uppercase;
}
This should do the trick.
When an absolutely positioned element is styled inside an element,
that has a position which is not 'static', the 'top', 'left', 'bottom'
and 'right' css properties are relative to the parent container.
So here's what I did:
I changed the position of <li> tags to 'relative' and the submenu
<ul> top value as 'top : 0px;'. That's it.

How can I keep CSS style select for menu item clicked

This is my HTML Code for menu
<div style="visibility:visible; color: #FFF; top:125px; width:100%; height:40px; position:absolute; display:block;background:#2B63B3;">
<ul id="trans-nav">
<li>Home</li>
<li>About us</li>
<li>Academic
<ul>
<li> Sub Menu 1 </li>
<li> Sub Menu 2 </li>
<li> Sub Menu 3 </li>
</ul>
</li>
<li>Administration</li>
<li>Miscellanous
<ul>
<li> Sub Menu 1 </li>
<li> Sub Menu 2 </li>
</ul>
</li>
</ul>
</div>
This the CSS code for menu
#trans-nav { list-style-type: none; height: 40px; padding: 0px 15px; margin: 0px; }
#trans-nav li { float: left; width:110px; position: relative; padding: 0px; line-height: 40px; background: #2B63B3; text-align:center; }
#trans-nav li a { display: block; padding: 0 15px; color: #fff; text-decoration: none; }
#trans-nav li a:hover { color: #97B7E6; }
#trans-nav li ul { opacity: 0; position: absolute; left: 0; width: 8em; background: #2B63B3; list-style-type: none; padding: 0; margin: 0; z-index:1;}
#trans-nav li:hover ul { opacity: 1; }
#trans-nav li ul li { float: none; position: static; height: 0; line-height: 0; background: none; }
#trans-nav li:hover ul li { height: 30px; line-height: 30px; }
#trans-nav li ul li a { background: #2B63B3; }
#trans-nav li ul li a:hover { background: #2B63B3; }
When I hover on any menu item color gets changed and when I click on item it navigates to the page but the the color of the text revert back to white. I want to keep the changed color till any other menu item is not clicked.
Any advice, suggestion or guidance is appreciable.
Thanking you all in advance
You can add a '.current' class to the anchor tag, via javascript. And give it those styles.
(Or if your nav is seperate on all pages just add it to the current pages nav, no need for js.)

Resources