Navbar li goes to next line upon resize - html

I have a navbar on my website, which is made up of a ul and some lis inside. The last li, Log Out, has to be right aligned. The problem is, when the screen is resized too small, it goes to a line below to prevent running into the other lis.
Here's the HTML for the navbar:
<div class="navbar">
<ul>
<li>Home</li>
<?php if ($user->logged_in) { ?>
<li>Page 1</li>
<li>Page 2</li>
<li>Page 3</li>
<li style="margin-right: 10px; float: right;">Log Out</li>
<?php } ?>
</ul>
and the CSS:
/* Navbar */
.navbar {
background: #DDDDDD;
overflow: hidden;
width: 100%;
}
.navbar ul {
border: solid 1px #BBBBBB;
margin: 0;
min-width: 600px;
overflow: hidden;
}
.navbar ul li {
border: solid transparent;
border-width: 0 1px 0 1px;
cursor: pointer;
display: inline-block;
float: left;
padding: 16px;
}
.navbar ul li:hover {
background-image: url("../images/patterns/background.png");
border-color: #369643;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
color: #FFFFFF;
text-shadow: 0 1px 0 #167F39;
}
.navbar ul li a {
color: inherit;
display: block;
text-decoration: none;
width: 100%;
}

Your question isn't too clear but I'm assuming you don't want the "logout" element to go below the other nav elements. The problem is in your css where your setting a "min-width" to your 'navbar ul' element. Therefore when the screen is reduced below that width it is being hidden.
I say hidden because after executing your code the "logout" element doesnt go below other nav elements it is just completely hidden. (Using Safari).
Deleting the 'min-width' fixed that problem for me. Hope it does for you too.

It works well in jsfiddle as well as in local browser.. Overflow is already added.
/* Navbar */
.navbar {
background: #DDDDDD;
overflow: hidden;
width: 100%;
}
.navbar ul {
border: solid 1px #BBBBBB;
margin: 0;
min-width: 600px;
}

Related

How to make the a href fill the entire tab menu without losing the original size and shape?

I want to have the entire tab menu to be clickable. In order to do that, I need the "a href" to fill the entire tab but doing so I lose the exact size and shape I want. I'm trying to accomplish by adding the :not(#sub_menu ul il) to the main ul, il{} css styles but it throws the portions out of whack.
So how can I accomplish this in HTML & CSS? Also, the bottom border is still viewable, so how can I fix this as well? Thank you
Adding :not(#sub_menu ul li) to the main p, il settings.
Then increasing the padding by 5px to "10px 20px" in #sub_nav ul li #selected.
Home Page
<body>
<main>
<h3>Home</h3>
<nav id="sub_nav">
<ul>
<li id="selected">Home</li>
<li>Away</li>
</ul>
</nav>
</body>
Away Page
<body>
<main>
<h3>Away</h3>
<nav id="sub_nav">
<ul>
<li>Home</li>
<li id="selected">Away</li>
</ul>
</nav>
</body>
CSS
#sub_nav {
float: left;
width: 100%;
font-weight: bold;
padding-bottom: 5px;
border-bottom: 1px solid;
}
#sub_nav ul {
list-style: none;
padding: 0;
margin: 0;
}
#sub_nav ul li {
display: inline;
border: 1px solid black;
}
#sub_nav ul li a {
padding: 5px 15px;
color: inherit;
text-decoration: none;
border-width: 1px 1px 0px 1px;
}
#sub_nav ul li#selected {
border-bottom: 1px solid white;
}
main {
margin-bottom: 1px;
padding: 1px;
height: 4000px;
width: 80%;
border: 1px solid black;
float: left;
}
main ul {
margin: 10px;
padding: 10px;
}
main p, li {
margin: 5px;
padding: 5px;
}

How to remove whitespace from a div/container

I have a navigation menu link that has extra whitespace at the bottom of the div tag with the id of nav. It is not because margin or padding, but there is some sort of whitespace that is not allowing the ul tag to touch the bottom of the div with the id of nav. How do I get it to do so. Here is the link
* {
padding: 0;
margin: 0;
}
#nav {
border: 1px solid black;
text-align: center;
min-width: 300px;
}
#nav ul {
padding: 10px 0;
display: inline-block;
}
#nav li {
float: left;
list-style: none;
margin-left: 50px;
}
#nav a {
text-decoration: none;
color: black;
padding: 15px 10px;
}
#nav a:hover {
color: white;
background: black;
}
<div id="nav">
<ul>
<li>link
</li>
<li>link
</li>
<li>link
</li>
<li>link
</li>
</ul>
</div>
The gap is reserved space given to descender text elements (e.g. j, y, g). Remove it by adding vertical-align:top to your <ul>
* {
padding: 0;
margin: 0;
}
#nav {
border: 1px solid black;
text-align: center;
min-width: 300px;
}
#nav ul {
padding: 10px 0;
display: inline-block;
vertical-align: top;
}
#nav li {
float: left;
list-style: none;
margin-left: 50px;
}
#nav a {
text-decoration: none;
color: black;
padding: 15px 10px;
}
#nav a:hover {
color: white;
background: black;
}
<div id="nav">
<ul>
<li>link
</li>
<li>link
</li>
<li>link
</li>
<li>link
</li>
</ul>
</div>
Note that the list items poke out below the div because of the padding you applied to #nav a which can be adjusted.
To fix your problem do this:
Change #nav ul to this:
#nav ul {
padding: 10px 0;
}
Change #nav li to this:
#nav li {
display: inline-block;
list-style: none;
margin-left: 50px;
}
remove margin-left: 50px; from your #nav li.Its creating unwanted white space on your menu.The width of menu will depend on the lenth of text
Something to do with the inline-block it seems. There's no space with inline-flex or display: table;
#nav ul {
padding: 10px 0;
display: inline-flex;
background-color: black;
}
inline-block's biggest problem was it's handling of fonts, it adds a ghost 'padding' of 4 to 5px after each element, depending on browser.
Here's a rewrite that uses the font-size: 0 method to negate the effects.
* {
padding: 0;
margin: 0;
box-sizing: border-box; /* allow percentages to be calculated without border and padding messing things up */
}
#nav {
border: 1px solid black;
min-width: 300px;
}
#nav ul {
list-style-type: none;
font-size: 0; /* font-size: 0; is a method to remove the ghost padding added after inline-blocks, one of the many reasons display: flex is becoming so popular */
}
#nav li {
display: inline-block;
width: 25%; /* control width here */
text-align: center;
}
#nav a {
display: block; /* allow element to expand to match parent size by changing from <a> default display: inline to block */
text-decoration: none;
color: black;
font-size: 15px; /* reset font-size here */
line-height: 30px; /* control element height here */
}
#nav a:hover {
color: white;
background: black;
}
<div id="nav">
<ul>
<li>link</li>
<li>link</li>
<li>link</li>
<li>link</li>
</ul>
</div>
fiddle
https://jsfiddle.net/Hastig/wfrxgxjm/

Why is my UL sub-menu taking the background of it's grandparent?

I'm working on a menu and on small screens I have it using gradients for backgrounds. There are 4 levels to this sub-menu, and each have their own color backgrounds. But for some reason my 4th level sub-menu is taking the background color of the 2nd sub-menu (it's grandparent) instead of the color it should be, which is pure white. It's so weird, this problem skips the 3rd sub-menu. I've tried as many work-arounds as I can think of, and even adding !important to the 4th level css hasn't fixed it. Below is the jsFiddle and the code. Am I missing something?
Here is the jsFiddle
CSS (for small screens)
#navbar {
background-color: #29568F !important;
}
.nav-tabs {
display: inline-block;
position: relative;
width: 98.5%;
background-color: #29568F;
margin: 0px 0px;
padding: 0px;
list-style-type: none;
color: white;
text-decoration: none;
text-shadow: 2px 2px #000000;
font: 18px arial, verdana, sans-serif;
border: 3px solid #29568F;
}
#menu-icon {
width: 200px;
display: block;
float: left;
text-align: left;
border: 0px;
border-bottom: 0px !important;
}
.nav-tabs > li:first-child span { /* This is #menu-icon */
border: 0px !important;
background: #29568F !important;
}
.menu-item {
display: none;
}
.nav-tabs li {
width: 100%;;
border-right: 0px;
clear: both;
list-style-type: none;
}
.nav-tabs li span {
display: block;
border: 0px !important;
padding: 10px 2.5% !important;
width: 95% !important;
position: relative !important;
top: 0px;
left: 0px;
}
.nav-tabs li span {
text-align: left;
border-bottom: 1px solid lightgrey !important;
background: linear-gradient(#29568F, #3399CC);
}
.nav-tabs > li > span:hover,
.nav-tabs > li .open {
background: linear-gradient(#3399CC, #29568F);
}
/* -- Arrows -- */
.nav-tabs .arrow-open {
content: url("/images/arrow-up.png");
}
.nav-tabs li .arrow-closed{
content: url("/images/arrow-down.png") !important;
}
/* --- 2nd level submenu -- */
.sub-menu {
position: relative;
top: 0px;
left: 0px;
width: 100%;
clear: both;
}
.sub-menu > li {
position: relative;
left: 0px;
}
.sub-menu > li span {
left: 0px;
text-align: center;
background: linear-gradient(#99EEFF, #3399CC);
}
.sub-menu > li > span:hover,
.sub-menu > li .open {
background: linear-gradient(#3399CC, #99EEFF);
}
/* -- 3rd level submenu -- */
.drop-menu {
display: none;
clear: both;
width: 100%;
position: relative;
top: 0px;
left: 0px;
}
.drop-menu li span {
border: 0px;
}
.drop-menu > li > span {
border: 0px !important;
left: 0px;
text-align: left;
background: linear-gradient(#C9EAF3, #ffffff);
}
.drop-menu > li > span:hover,
.drop-menu > li .open {
background: linear-gradient(#ffffff, #c9EAF3);
}
/* -- 4th level submenu -- */
.slide-menu {
display: none;
background: #ffffff !important;
border: 0px;
width: 100%;
clear: both;
position: relative;
top: 0px;
left: 0px;
z-index: 300px;
}
.slide-menu li {
position: relative;
left: 0px !important;
width: 98%;
border: 0px !important;
text-align: center;
}
.slide-menu li span {
display: block !important;
position: relative;
left: 0px;
width: 100%;
border: 0px !important;
}
HTML (sample code to view structure)
<div id="navbar">
<ul class="nav-tabs">
<li id="menu-icon"><span><img src="/images/menu-icon.png">Menu</span></li>
<li class="menu-item"><span>Dogs <div class="arrow-closed"></div></span>
<ul class="sub-menu">
<li><span>Meet the Breeds<div class="arrow-closed"></div></span>
<ul class="drop-menu">
<li><span>Sort A - Z <div class="arrow-closed"></div></span>
<ul class="slide-menu">
<li>Breeds A - F</li>
<li>Breeds G - L</li>
<li>Breeds M - R</li>
<li>Breeds S - Z</li>
</ul> <!-- close slide-menu -->
</li> <!-- close Drop-menu item -->
</ul> <!-- close drop-menu -->
</li> <!-- close sub-menu item -->
</ul> <!-- close sub-menu -->
</li> <!-- close main menu item -->
</ul> <!-- close main-menu -->
</div>
NOTE!
While creating the jsFiddle for this question, I realized that when I only included the css for small screens, the 4th level menu's background was white as it was supposed to be. It wasn't until I included the WHOLE CSS code that it stopped working. Which still doesn't make sense because the 4th level background works in it's normal, large screen state. I don't understand why it would be doing this so if someone would be kind enough to comb through this for me I'd be ever so grateful.
UPDATE!
I've been messing with the jsFiddle, and I removed all of the large-screen CSS to try and de-bug it. 4th level menu had a white background. Ok. So I added the 1st level CSS back in, and the 4th level background messed up. Removed it, and added the 2nd level CSS in, and the 4th level background messed up again. But if I added in the 3rd or 4th level CSS for large screens, the 4th level background wasn't affected at all. So the problem is somewhere BOTH in the 1st level and 2nd level CSS. But I don't understand why. I don't have anything inheriting it's parent's css, and none of the code calls for any gradient at all. So weird.
Spent a few minutes looking over your code, and i think i found the problem. In your 2nd and 3rd sub-menu you're applying the background to the span tags. But on your 4th menu ( .slidemenu ) you're applying it directly to the ul.
Remove background from:
Line: 416
.slide-menu
Add background to:
Line: 432
.slide-menu li
Working fiddle: jsfiddle

Extra pixel in firefox on dropdown hover

I'm using top: 100% to push the sub-menu dropdown below the main navigation bar on hover but in Firefox the menu is pushed below the border of the menu causing a 1px gap. In webkit browsers it works the way I'm wanting it to, where it shows the sub-menu with no gap in between. I'm using 100% to avoid hard coding in a value.
I created a codepen for testing. I set the border around the menu to red to make it easier to see the issue. The second nav is just showing the hover state.
update
Without display: table; on the .nav-main element, the 1px gap goes away, but I'm using it so I can add display: table-cell; to the nav list items to spread them out and fill the entire nav. Any ideas on how to get around that?
html
<nav role="navigation">
<ul class="nav-main">
<li>Link One</li>
<li>
Link Two
<ul class="sub-menu">
<li>Dropdown Link One</li>
<li>Dropdown Link Two</li>
<li>Dropdown Link Three</li>
</ul>
</li>
<li>Link Three</li>
<li>Link Four</li>
</ul>
</nav>
scss
$pink: #ed2490;
a {
text-decoration: none;
}
.nav-main {
position: relative;
display: table;
margin: 0;
padding: 0;
width: 100%;
border: 1px solid lighten(black, 22%);
border-radius: 4px;
background: lighten(black, 8%);
#include background(linear-gradient(bottom, lighten(black, 8%), lighten(black, 36%)));
font-weight: 500;
line-height: 1;
> li {
display: table-cell;
text-align: center;
position: relative;
&:hover {
.sub-menu {
top: 100%;
}
a {
background: lighten(black, 18%);
background: rgba(black, 0.25);
}
}
}
a {
display: block;
color: white;
padding: 15px 10px;
&:hover,
&:focus {
background: lighten(black, 18%);
background: rgba(black, 0.25);
}
}
.sub-menu { // dropdown
position: absolute;
top: -999px;
z-index: 10;
margin: 0;
padding: 0;
width: 200px;
background: lighten(black, 4%);
li {
border-top: 1px solid lighten(black, 18%);
&:first-child {
border-top: 0;
}
}
a:hover {
background: $pink;
}
}
}
// For example only
.styleguide-dropdown {
padding: 40px 20px 130px;
}
.nav-main {
border-color: red;
.psuedo-hover {
a {
background: lighten(black, 18%);
background: rgba(black, 0.25);
}
.sub-menu {
top: 100%;
.psuedo-hover {
background: $pink;
}
}
}
}
shudder, anything with display: table-* is inherently going to be very hard to style consistently across browsers.
But, that said, its really not your issue. The following fixes both work:
.submenu { display:none;}
:hover > .submenu { display: block;}
or
.submenu { height: 0; overflow:hidden }
:hover > .submenu { height: auto; }
And either of those should be better for reflow/repaint/re-render than crazy shifts in top. Also they will be more robust in case you ever wanted to re-use that nav lower on a page, without just "hiding" things by sending them up with a negative top.
Cheers.
Removing the 1px border around ul solves the problem; then you can add top and bottom borders to each li. Though the border-radius won't be calculated the same way.
I believe the reason is that the height of ul is more than the height of li with this 2px of border and so 100% of the height of li isn't enough... but I'm really not sure of this one.
Here's a working demo: http://codepen.io/anon/pen/jsBEt

HTML+css dropdown centering

I have this menu:
#navbar {
text-align: center;
margin: auto;
padding: 0;
height: 1em;
}
#navbar li {
list-style: none;
float:left; }
#navbar li a:hover{
background-color: #CCC;
}
#navbar li a {
border: 1px solid #000;
display: block;
margin-right: 18px;
margin-left: 18px;
padding: 3px 8px;
background-color: #FFF;
color: #000;
text-decoration: none; }
#navbar li ul {
display: none;
width: 10em; /* Width to help Opera out */
}
#navbar li:hover ul {
display: block;
position: absolute;
margin: 0;
padding: 0; }
#navbar li:hover li {
float: none; }
#navbar li:hover li a {
background-color: #FFF;
border-bottom: 1px solid #000;
color: #000; }
#navbar li li a:hover {
background-color: #CCC; }
<ul id="navbar">
<li>Start</li>
<li>Vad?</li>
<li>Kom igång!</li>
<li>Läringsartikler<ul>
<li>Subitem One</li>
<li>Second Subitem</li>
<li>Numero Tres</li></ul>
</li>
<li>Läringsfilmer<ul>
<li>Subitem One</li>
<li>Second Subitem</li>
<li>Numero Tres</li></ul>
</li>
</ul>
as you can see in navbar { i tried to use text-align: center or margin:auto but it still wont center the whole menu..
why?
when i change the navbar li to float center instead of float left then it make the whole menu stupid big
You need to specify a width on your navbar ul.
#navbar {
text-align: center;
margin: auto;
padding: 0;
height: 1em;
width: 400px;
}
There is NO center value for 'float' style attribute
-- Oops dint see that comment
As mentioned, there is no Float:center. In order to center using margin-left and margin-right auto, you either need to set a width (as mentioned above) or change it to display:block.
If you don't want to set a width or can't, there's a CSS hack called Shrink Wrapping that is easy to setup.