Li:hover not encompassing entire menu button on screen resize - html

So I seem to be having an issue with the hover feature. When my screen is full size, I can hover my mouse over a menu option and the entire background color changes. However, when I resize my screen, only a portion of the background color changes. I see this upon screen resize:
Screen Resize Issue
Here is the HTML code
<nav>
<ul>
<li>Home</li>
<li>Getting Started</li>
<li>How Do Plants Thrive?</li>
<li>Common Problems</li>
<li>Great Indoor Plants</li>
<li>References</li>
</ul>
<nav>
And the css
nav {
width: 100%;
background: #003300;
border: 1px solid #ccc;
border-right: none;
}
nav ul {
overflow: hidden;
margin: 0;
padding: 0;
}
nav ul li {
list-style: none;
float: left;
text-align: center;
border-left: .5% solid #fff;
border-right: .5% solid #ccc;
width: 16.6667%; /* fallback for non-calc() browsers */
width: calc(100% / 6);
box-sizing: border-box;
}
nav ul li:first-child {
border-left: none;
}
nav ul li a {
display: block;
text-decoration: none;
color: white;
padding: 10px 0;
}
nav ul li a:hover {
background-color: #00b300;
width:100%;
}
.currentlink {
background: #00b300;
}
Thanks in advance for any help you can provide!

By testing your code it appears the issue is that your list items are assigning their height based on their content. As such, when you resize your window, forcing the text in one or more of your list items to wrap, the list items with the wrapped text grow taller than those with less or no wrapped text.
One possible solution, depending on how you wish to style your nav, is to utilize CSS' flex styling. For your CSS, try the following:
nav {
border: 1px solid #ccc;
border-right: none;
}
nav ul {
background: #003300;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-box;
display: -webkit-flex;
display: flex;
justify-content: space-around;
list-style: none;
margin: 0;
padding: 0;
-webkit-flex-flow: row wrap;
}
nav ul li {
border-left: .5% solid #fff;
border-right: .5% solid #ccc;
box-sizing: border-box;
font-weight: bold;
padding: 5px;
text-align: center;
width: 16.6667%;
/* fallback for non-calc() browsers */
width: calc(100% / 6);
}
nav ul li:first-child {
border-left: none;
}
nav ul li:hover {
background-color: #00b300;
}
nav ul li a {
color: white;
text-decoration: none;
}
.currentlink {
background: #00b300;
}
For your HTML, because we can't assign a height to the <a>, move class="currentLink" to the <a>'s parent <li> so it looks like the following:
<li class="currentlink">Home</li>
To see it all in action, check this jsfiddle.

It happens because your third a text wraps on two lines.
As #dmitry suggested, I'd go with flexbox too. Let's make a few modifications.
First thing, we need box-sizing: border-box on our elements and their children
*, *:before, *:after {
box-sizing: inherit;
}
html {
box-sizing: border-box;
}
Next we want the nav and the ul to be wull width and we apply display: flex; to this last one
nav {
width: 100%;
}
nav ul {
display: flex;
align-items: center;
width: 100%;
}
Last thing, we need our a to cover full height;
nav ul li a {
height: 100%;
}
Have a look on this fiddle.

Related

how to make all list items stretch to full width of a container

I' trying to create a nav that's responsive but I can't get then menu items to stretch itself relative to the container.
What's the most effective modern method of making all elements auto fit themselves full width of a container?
nav {
border: solid 1px #000;
width: 700px;
}
ul {
width: 100%;
list-style-type: none;
width: 100%;
}
ul li {
padding: 25px;
display: inline-block;
background: #000;
color: #fff;
}
<nav>
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
<li>f</li>
</ul>
</nav>
I would use CSS tables as follows.
For ul, use display: table and zero out the margin and padding, and set width to 100%.
For ul li, use display: table-cell.
The table cells will adjust themselves to the width of the parent in a reasonable fashion taking into account the width of the link text/labels.
Note: I assumed that you want the links to be inline such that all the links fill up the width, as opposed to a single link taking up 100% of the width. Otherwise, change display: inline-block to display: block for the li elements, but since that is too obvious, I assumed that you wanted a horizontal layout.
nav {
border: solid 1px #000;
width: 700px;
}
ul {
width: 100%;
list-style-type: none;
display: table;
margin: 0;
padding: 0;
}
ul li {
padding: 25px;
display: table-cell;
background: #000;
color: #fff;
}
<nav>
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
<li>f</li>
</ul>
</nav>
let's say that effective is in the eye of the beholder, but flexbox is quite modern:
nav { display: flex; border: solid 1px #000; width: 700px; }
ul { display: flex; flex-grow: 1; width: 100%; padding: 10px;
list-style-type: none; }
ul li { flex-grow: 1; padding: 25px; margin: 10px;
text-align: center; background: #000; color: #fff; }
see fiddle: http://jsfiddle.net/4dxkk5wr/18/
and this resource: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
and have fun!
If you really want the most modern solution, you could try flexbox layout: http://jsfiddle.net/4dxkk5wr/15/
ul { width: 100%; list-style-type: none; display: flex; padding: 0; }
ul li { width: 100%; padding: 25px; box-sizing: border-box; background: #000; color: #fff; flex-wrap: nowrap; }
Use flexbox! More information. Give the container the CSS:
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
and children: flex: 1 0
Example
Here is an updated jsfiddle http://jsfiddle.net/4dxkk5wr/10/
You can use:
box-sizing: border-box;
width:100%;
in the li tag and set the padding to 0 on the ul.
you can use this css
nav {
border: solid 1px black;
width: 100%;
}
ul {
width: 100%;
list-style-type: none;
width:100%;
margin-left: -35px;
}
ul li {
padding: 100px;
display: inline-block;
background: #000;
color: #fff;
}

navbar list items changing size depending on list string length

I made a navbar that supposedly has list items that are evenly distributed along the navbar, although it seems that list items with longer string lengths have a bigger width than others. Is there a way to fix this?
<div id="menu-bar">
<ul>
<li><a href=#>Home</a></li>
<li>Ferrari</li>
<li>Lamborghini</li>
<li>Bugatti</li>
</ul>
</div>
CSS:
* {
margin: 0px;
padding: 0px;
}
#menu-bar {
background-color: #51F069;
width: 100%;
height: 40px;
border-top: 2px solid #8f8f8f;
border-bottom: 2px solid #8f8f8f;
display: table;
}
#menu-bar li {
list-style-type: none;
display: inline-block;
font-family: Ferrari;
display: table-cell;
text-align: center;
}
#menu-bar a {
display: block;
font-size: 25px;
line-height: 40px;
}
#menu-bar ul {
width: 100%;
display: table;
}
This is a default behavior, they're set to display:table-cell. If you look at an HTML table, the cells with longer content will take more space in the table.
Simply set a width to your list-items :
#menu-bar li {
list-style-type: none;
font-family: Ferrari;
display: table-cell;
text-align: center;
width: 25%; /* 100% / 4 li's */
}
Here's a fiddle : http://jsfiddle.net/sa7x258k/1/

Space to the right of each list element

Not sure why there is a space to the right of each li, as you can see here when you mouse over it. Obviously don't want it there and can't figure out how to get rid of it. Any help would be greatly appreciated!
Here is the code:
HTML:
<header>
<div class="nav-container">
<nav class="nav-items" role="navigation">
<ul>
<li>list1</li>
<li>list2</li>
<li>list3</li>
</ul>
</nav>
</div>
</header>
CSS:
header {
position: fixed;
top:0;
background-color:#2C5463;
height:2.3em;
width: 100%;
border-bottom-color: black;
border-bottom-style: solid;
}
header .nav-container {
margin: 0 30px;
height: 100%;
display: block;
padding: 0;
}
.nav-items {
float: left;
margin: 0;
height: 100%;
}
.nav-items ul {
display: inline-block;
margin: 0;
height: 100%;
}
.nav-items ul li {
display: inherit;
border-left: 1px solid #c8c8c8;
height: 100%;
margin: 0;
}
.nav-items ul li a {
display: inherit;
text-decoration: none;
color: #ffffff;
margin: 0 auto;
padding-top: 8px;
white-space: nowrap;
height: 100%; /* Width and height of top-level nav items */
width: 90px;
text-align:center;
vertical-align: middle;
}
.nav-items ul li:hover {
background: #617F8A
}
http://jsfiddle.net/eF83x/
Inline elements are sensitive to white space. Remove the white space and the problem goes away.
Ex:
<ul>
<li>list1</li><li>list2</li><li>list3</li>
</ul>
jsFiddle example
You can remove the spaces between the list items literally, occupy the space with HTML comments (<!-- -->), or float them left.
Just needs to changes on css class here for your solution,
.nav-items ul
{
display: **inline-table**;
margin: 0;
height: 100%;
}
Demostration
What you could also do is make the lis float left and display them as block. This will fix it without messing with the html code.
.nav-items ul li {
float: left;
display: block;
border-left: 1px solid #c8c8c8;
height: 100%;
margin: 0;
}
jsFiddle example

CSS navigation horizontal submenu not directly below parent

I am a little stuck. I am trying to build a horizontal navigation bar, 1024px across, which will allow for a submenu to display below it. But i want the submenu to also be 1024px in width and to display directly below the navigation bar, vertically aligned.
At the moment the submenu appears but fixes its left side to the left side of the current li that you are hovering over. How can I fix this?
Thanks!
EDIT: So on mouse over it would look something like this: http://eventav.biz/site/example.jpg
Link to what I've done so far -
http://www.eventav.biz/site/
ul.top_menu {
position: relative;
margin: 0;
margin-bottom: -1px;
list-style: none;
display: table;
width: 1024px;
border: 1px solid #111111;
border-bottom: 1px solid #000000;
border-radius: 10px 10px 0px 0px;
}
.top_menu li {
display: block;
position: relative;
border-right: 1px solid #111111;
float: left;
margin: 0px;
display: table-cell;
vertical-align: middle;
}
.top_menu li:first-child {
border-left: 1px solid #111111;
}
.top_menu li a {
display: block;
text-decoration: none;
color: #000000;
text-shadow: 3px 3px 8px #3A3A3A;
padding: 15px;
height: 30px;
display: table-cell;
vertical-align: middle;
margin: 0px;
}
#top_menu_item ul {
display: none;
margin: 0px;
}
#top_menu_item:hover ul {
display: block;
position: fixed;
margin: 0;
}
#top_menu_item:hover li {
width: 1024px;
background-color: #666;
text-align: left;
color: #FFF;
font-size: 12px;
margin: 0px;
}
<ul class="top_menu">
<li id="top_menu_item">HOME</li>
<li id="top_menu_item">OUR SERVICES
<ul><li id="top_menu_item">test</li></ul>
</li>
<li id="top_menu_item">EXAMPLES OF OUR WORK
<ul><li id="top_menu_item">test</li></ul>
</li>
<li id="top_menu_item">CONTACT US</li>
</ul>
Remove the fixed positioning from the child ul, and replace it with position:absolute. Add in left:0px, and then remove position:relative from the parent li.
Working jsFiddle example
#top_menu_item:hover ul {
display: block;
position: fixed; /* Change this to position:absolute; */
left:0px; /* Add this */
}
.top_menu li {
display: block;
position: relative; /* Remove this */
}
1) Remove position: relative; from #top_menu_item
2) Set #top_menu_item ul to position: absolute; left: 0; instead
3) Remove left padding on #top_menu with padding-left: 0;
4) Add:
#top_menu_item:first-child {
margin-left: 40px;
}
Essentially, the problem was that you've been positioning your inner ul tag relative to it's parent li. Instead, the solution above positions the secondary navigation absolutely in relation to the primary navigation, and we use left: 0; to make sure it's completely left-aligned.
It's also against the standard to use an id multiple times on a page. Therefore I'd recommend changing #top_menu_item into .top_menu_item and changing the HTML accordingly.
Let me know if you have any problems!

Vertically aligning text inside a link inside an <li> that needs to expand horizontally

I need to make a menu that on mobile has the links expand to full width. Something similar to this wireframe.
My code so far:
HTML
<div id="shortcuts">
<ul>
<li>Categories</li>
<li>Archives</li>
</ul>
</div>
CSS
#header {
width: 100%;
height: 180px;
background-color: #666;
color: #fff;
}
#shortcuts { position: absolute; z-index: 20; top: 0; left: 0; right: 0; }
#shortcuts ul {
display: table;
width:100%;
height: 180px; /* for testing only */
}
#shortcuts ul li {
list-style: none;
display: table;
height: 60px;
width:100%;
display: block;
vertical-align: middle;
background-color: red; /* for testing only */
border: blue 3px solid; /* for testing only */
}
#shortcuts ul li a {
width: 100%;
height: 60px;
text-decoration: none;
display: table-cell;
vertical-align: middle;
border: 2px dashed green; /* for testing only */
}
This is my result so far (colors used only for testing)
If I change the #shortcuts ul li a to display: block;, I can get the desired width. But then the text will not center vertically.
Not a duplicate of CSS Positioning anchor inside li vertically aligned because that is only partial part to vertically align the text. Does not answer how to make the link expand 100% width.
You need to use both "display: block;" and "line-height:60px;" for "#shortcuts ul li a {"
Updated code:
#shortcuts ul li a {
width: 100%;
height: 60px;
text-decoration: none;
display: block;
line-height:60px;
vertical-align: middle;
border: 2px dashed green; /* for testing only */
}
Please refer the fiddle :- http://jsfiddle.net/aasthatuteja/6WEW6/