inline-block nowrap resize issue - css

I have being trying to make a nowrap navigation with CSS however when I hover over the parent list for lack of a better description jumps
HTML
html {
overflow-x: hidden;
}
ul li{
list-style:none;
}
nav{
margin: 0 -9999rem;
padding: 0.25rem 9999rem;
height:40px;
background-color:white;
overflow:hidden;
}
nav .user-nav{
position:absolute;
white-space:nowrap;
}
nav .user-nav > li{
display: inline-block;
margin: 0 10px;
}
nav .user-nav li a{
text-decoration:none;
color:black;
}
nav .user-nav li a:hover{
color:red;
}
nav .user-nav > .interact li{
display:none;
background-color:white;
padding:10px;
}
nav .user-nav > .interact:hover li{
display:block;
}
nav .user-nav > .interact li:hover{
color:red;
}
<nav>
<ul class="user-nav">
<li>Reccent reviews</li>
<li>Reccent addtions</li>
<li class="interact">Login/Register
<ul>
<li>Login</li>
<li>Register</li>
</ul>
</li>
</ul>
</nav>
I added it to a fiddle below
https://jsfiddle.net/23e5aLp2/
I initially done it with a float however since reading a bit more I found inline-block is the better option, the problem is as I mentioned the parent list seems to jump when hover over the drop down menu

Make your <ul> inside of .interact positioned absolute, like:
.interact ul {
position: absolute;
}
.interact ul {
position: absolute;
}
html {
overflow-x: hidden;
}
ul li{
list-style:none;
}
nav{
margin: 0 -9999rem;
padding: 0.25rem 9999rem;
height:40px;
background-color:white;
overflow:hidden;
}
nav .user-nav{
position:absolute;
white-space:nowrap;
}
nav .user-nav > li{
display: inline-block;
margin: 0 10px;
}
nav .user-nav li a{
text-decoration:none;
color:black;
}
nav .user-nav li a:hover{
color:red;
}
nav .user-nav > .interact li{
display:none;
background-color:white;
padding:10px;
}
nav .user-nav > .interact:hover li{
display:block;
}
nav .user-nav > .interact li:hover{
color:red;
}
<nav>
<ul class="user-nav">
<li>Reccent reviews</li>
<li>Reccent addtions</li>
<li class="interact">Login/Register
<ul>
<li>Login</li>
<li>Register</li>
</ul>
</li>
</ul>
</nav>
Hope this helps!

I would recommend to use inline-table or inline-block for class nav .user-nav li
Fiddle

Its because the list items are block elements, thus making the container bigger in size when they spawn in. Make the dropdown items absolute and your problem will be solved.
http://prntscr.com/ddn5xe
http://prntscr.com/ddn5gv
As for the nav question; a text-align: justify solution might work, or a flexbox solution. Try googling around for "nav justify text" or "nav flexbox" and you'll find a pletora of solutions.

Actually what is happening is that the inline block works well if you add same content in all inline block elements but as they are block elements their default settings will wrap the div around the content inside it. To make sure the content is aligned correctly in line, you need to specifically assign content alignment properties in the css of the inline-block elements.
The simplest solution in your scenario is just add vertical-align:top; to the nav .user-nav > li class or the element with the display:inline-block; property and the problem will be resolved.
Hope this helps.

Related

Can only get CSS dropdown to appear on hover

I would like to change my current menu to allow for one dropdown: Products. My test page can be found here This is my HTML:
<nav>
<ul class="menu">
<li class="current">Home</li>
<li>About Us</li>
<li class="subNav"><a class="selected">Products</a>
<ul>
<li>Designer Bags
</li>
<li>Cowhides
</li>
<li>Hand-carved Geese
</li>
<li>Antler Chandeliers
</li>
</ul>
<li>Gallery</li>
<li>Shows</li>
<li>Contact</li>
<li>Shop</li>
</ul>
</nav>
My original CSS:
.nav-buttons{text-align:center;padding-bottom:17px;}
#nav{overflow:hidden;display:inline-block}
#nav li{float:left;overflow:hidden;margin:0 10px}
#nav li a{display:block;background:url(../images/pags.png) no-repeat 0 0;
width:19px;height:19px;
line-height:0;font-size:0;
}
#nav li a:hover,#nav li.showPage a{background-position: 0 bottom}
nav{float:right;padding:12px 0 0 0}
.menu {
font-size:0;
line-height:0;
padding:0;
z-index:99;
position:relative;
margin-right:21px;
}
.menu > li {
position:relative;
float:left;
margin-left:11px;
border-radius:5px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
background:url(../images/point.png)
}
.menu li a{
color:#b3adad;
font-size:18px;
line-height:20px;
display:block;
position:relative;
text-decoration:none !important;
padding:7px 12px 9px;
font-family: 'Noto Sans', sans-serif;
}
.menu li.current,
.menu li:hover {
background:#9c6f51;
}
.menu li.current a,
.menu li:hover a{
color:#fff
}
And this bit of CSS I just added today:
nav a {
font-weight: 800;
padding: 5px 10px;
display: block;
}
nav > ul > li.subNav ul {
display: none;
position: absolute;
top: 100%;
left: 0;
white-space: nowrap;
background: #fff;
}
nav ul li.subNav:hover ul {
display: block;
}
It seems to want to work with the exception that I see no sub-menu unless I hover over it. I've come here for help because I'm afraid to mess with the original CSS and thus ruin my navigation throughout the rest of the site. Is there something I can add that will cause the menu to appear within just the "subNav" class, without affecting the rest of the menu or site navigation? (The original CSS came with this template and I do note that font-size:0 is used a few times. Since the menu worked well before I felt I needed to add a dropdown, I have been reluctant to change that, since I would only be experimenting without understanding.)
I've sorted it. I had to change the background color in the "subNav" class, in order for the site menu's overall font color to show up. It's there; I just couldn't see it.

Why does a percentage width in the top li collapse the children li's?

I can't figure out why If I change the top li in a menu from px's to a percentage the child menu items stop displaying normally and fall over each other.
JS fiddle working:
http://jsfiddle.net/mg6vx/
JSfiddle not working (added width:20%; to #nav li):
http://jsfiddle.net/4eyp5/
HTML:
<div id="nav">
<ul>
<li>About Us
<ul>
<li>Ab</li>
<li>out</li>
<li>Us</li>
</ul>
</li>
<li>Stuff
<ul>
<li>Stuffed</li>
<li>stuffs</li>
<li>Stuffeds</li>
</ul>
</li>
<li>Some Such
<ul>
<li>Doohickey</li>
<li>Widgets</li>
<li>Things that amazon sells for one star</li>
</ul>
</li>
<li>Careers
<ul>
<li>Interal postings</li>
<li>External postings</li>
<li>Theoretical postings</li>
</ul>
</li>
</ul>
</div>
CSS:
#nav * {
margin: 0;
padding: 0;
}
#nav li {
position:relative;
float:left;
list-style:none;
background-color:black;
/* if you add this the child li's fall over themselves width:20%; */
}
#nav li a {
width:100px;
height:30px;
display:block;
text-decoration: none;
text-align:center;
line-height:25px;
color: white;
}
#nav li a:hover {
background-color: #009;
text-decoration: underline;
}
#nav ul ul {
position: absolute;
top: 30px;
visibility: hidden;
}
#nav ul li:hover ul {
visibility:visible;
}
As #fizzix points out, each li has a width of 20% of the parent. The nested li elements therefore have a small width because they are 20% of 20%.. etc.
You should use the child combinator, >, in order to prevent styling from effecting nested li elements.
Something like this would work: (example)
#nav > ul > li {
width:20%;
}
#nav > ul > li > ul,
#nav > ul > li > ul > li,
#nav ul li a {
width:100%;
}
It is because you are giving them a specific width (being 20%), therefore it allows the list items to fit next to each other. In easier terms, you have 100% to work with and you are giving every list item 20%. Hence, you could then fit 5 list items on the same line.
If you can explain a little more on what you are trying to achieve exactly, we can help you find an alternative approach.

Chrome reduced the width of the <a> tag on hover

I built a simple dropdown menu, but in Chrome on hover the size of the tag reduced by 3px. (jump effect)
I don't understand where's the problem, here is my css:
#primary {
width:400px;
background:yellow;
height:48px;
margin:0;
padding:0;
}
#primary li {
display:table;
width:120px;
float:left;
border-right:1px solid blue;
position:relative;
}
#primary li a {
display:table-cell;
text-align:center;
height:48px;
vertical-align:middle;
}
#primary #secondary {
display:none;
margin:0;
padding:0;
}
#primary li:hover #secondary {
position:absolute;
top:48px;
left:0;
background:red;
display:block;
}
It seems Chrome adds extra hidden margin/padding.. In FF, IE it's working.
Online demo: http://jsfiddle.net/A3XDR/
Change:
#primary li a {
display:table-cell;
text-align:center;
height:48px;
vertical-align:middle;
}
to
#primary li a {
display:block;
text-align:center;
height:48px;
line-height: 48px;
}
If you want to keep your vertical alignment and table-cell you could add width: 100%:
#primary li a {
display:table-cell;
text-align:center;
height:48px;
vertical-align:middle;
width: 100%;
}
This should do the trick but I am not sure why is it behaving like this. It is surely because of secondary ul but not sure why.
#primary li a {
display:table-cell;
text-align:center;
height:48px;
vertical-align:middle;
width:100%;
}
Fiddle
Found a way to do it but it involves changing your markup, adding a span within the link:
HTML:
<ul id="primary">
<li>
<span>Nav 1 is long and wraps some</span>
<ul id="secondary">
<li>Nav 1-1</li>
<li>Nav 1-2</li>
<li>Nav 1-3</li>
<li>Nav 1-4</li>
</ul>
</li>
<li><span>Nav 2</span></li>
<li><span>Nav 3</span></li>
</ul>
CSS:
#primary li a {
display:table;
height:48px;
width: 100%;
}
#primary li a > span {
display: table-cell;
text-align: center;
vertical-align:middle;
width: 100%;
}
Fiddle | JSBin (for IE8 testing, jsFiddle doesn't work on IE8)
(You can also change #primary li to display: block if that table-in-table bothers you; seems to work: Fiddle | JSBin)
No jumping for me in the older Chrome (~v26) that has issues with just the simple width: 100%, or with the long nav entry that wraps which jumps for me even on current Chrome (~v32). Also seems happy in Firefox, IE8, IE10, Opera, and Midori.
(This solution is only possible because of the direction putvande and Lokesh pointed in. Please show your support for their efforts.)

float and display affection on width and height

What is the effect of inline and block and inline-block and floating to width and height?
For example take look at below css menu :
ul
{
list-style-type:none;border-style:solid;border-width:1px;border-color:Blue;
padding:0px;
display:inline-block;
float:left;
}
ul li{display:inline;}
ul li a
{
/*display:inline-block;
float:left;*/
display:inline-block;
float:left;
background-color:rgb(100,170,110);
color:Yellow;
text-decoration:none;
height:30px;
padding-left:20px;
padding-top:5px;
padding-right:20px;
}
ul li a:hover{background-color:Yellow;color:Red;}
I have corrected that for both IE and Firefox with adding below codes for ul:
display:inline-block;
float:left;
Is it true that for a inline tag the height=0?
Is it true for the left floated tag , it width is the maximum widths of it's children ?
Why block elements (such as menu items) will have some margins with their next items?
You'll get some goofy stuff with inline-block with IE. You might have better luck setting tha a's to block and float the li's. Try the code below
HTML
<nav>
<ul>
<li>Home
<li>About
<li>Contact
</ul>
</nav>
CSS
ul { margin: 0; padding: 0; }
li { float: left; }
a { display: block; padding: 5px; margin: 0 5px; }

css float right space

anyone know how to fix this! on 'Get In Touch button'
it works find in another browser except IE7;
HTML:
<NAV>
<UL>
<LI>Home</LI>
<LI>Branding</LI>
<LI>About</LI>
<LI>Get In Touch</LI>
</UL>
</NAV>
CSS:
nav { float:right;}
nav ul { margin:0px; padding:0px; }
nav ul li { float:left; }
nav ul li { display:block; background:#ccc; padding:5px; margin-left:5px;}
Add white-space:nowrap to the nav ul li CSS rule.
About the white-space property: http://vidasp.net/CSS-white-space-property.html
You may need to explicitly set width on the list and/or nav container.
This is quite common in IE7 and down, where the dumb browser needs to be told there's room allotted for the content.
nav ul li { display:block; background:#ccc; padding:5px; margin-left:5px; width: auto; }
Add in that width: auto; - see if that helps.
Neuro
use
clear: right; float:right;

Resources