I have created a navigation bar that is centered with CSS which works. Each li item is separated with a border which is a background image. When hovering on the nav items, the separator disappears because the hover changes the background (I guess) but I wonder how I can fix this, padding or margin can't work because it will just shift the li element.
Second problem is that the sub menu items aren't displaying correctly and I have no idea why...
Demonstration: http://jsfiddle.net/Xenios/tfbhh/9/embedded/result/
The code: http://jsfiddle.net/Xenios/tfbhh/9/
I'm trying to get this to work for almost a week, and I'm quite tired of it, so I'm looking here for support.
Separator
As you know the main bar (nav_container) has a background image, which makes up the look of the button. The background for each button is the separator and nothing else (10px on the right). So, when your on hover background shows, because its park of the non-hover background.
In order to fix this you need to put the separator in it's own <li>, with the non-hover background. Then when you hover the elements they can easily change to your current on hover image with.
If you don't want to separate the <li> elements then, you will have to will have to make individual full width images for each button, but looking at the way you've gone about making this menu, I doubt you will want to do this.
Here is your working example (I only did the first few buttons): http://jsfiddle.net/tfbhh/43/
Submenu
As I mentioned above, you have set the container background image, you haven't done this on your submenu items, so thats why they don't have a larger looking button. Use your developer toolbar (F12) to see the styling and this should clear it up.
You can use a left padding equal to the width of the separator on the li and change only the background on the a. Also I noticed you used class="separator" on all but the first list item. You could replace that with the :first-child pseudo selector. Then you would get something like this:
li:first-child { padding-left: 0; background: transparent; }
li { padding-left: 3px; background: url(separator.png) no-repeat; }
li a { line-height: 40px; padding: 0 15px; }
li a:hover { background: url(anchor-hover.png) repeat-x; }
Edit: The CSS above covers the core styling of this solution. Here's a working example (using background colors):
http://jsfiddle.net/haa5X/3/
The complete CSS:
ul { overflow: hidden; background: green; }
li:first-child { padding-left: 0; }
li { padding-left: 3px; float: left; background: red; }
li a { float: left; line-height: 40px; padding: 0 15px; background: yellow; }
li a:hover { background: purple; }
The complete HTML:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
Edit 2: Sorry, missed the part of the submenu:
http://jsfiddle.net/haa5X/4/
The complete CSS:
ul { overflow: hidden; margin: 0; background: green; }
ul > li:first-child { padding-left: 0; }
ul > li { padding-left: 3px; float: left; background: red; }
ul > li a { float: left; line-height: 40px; padding: 0 15px; background: yellow; }
ul > li a:hover { background: purple; }
li ul { display: none; position: absolute; margin-top: 40px; }
li:hover ul { display: block; }
li li { padding-left: 0; float: none; display: block; }
li li a { float: none; display: block; width: 100%; }
The complete HTML:
<ul>
<li>Item 1</li>
<li>Item 2
<ul>
<li>First sub item</li>
<li>Sub item 2</li>
<li>Last sub item</li>
</ul>
</li>
<li>Item 3</li>
</ul>
Related
I am having trouble with a pop-out menu behaving different in Chrome than in IE or Firefox.
Here is the HTML
<body>
<ul>
<li>Level One
<ul>
<li>Level Two Item One</li>
<li>Level Two Item Two
<ul>
<li>Level Three</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
Here is the css
ul {
padding: 0px;
list-style-type: none;
}
li {
background-color: blue;
}
a:link {
text-decoration:none;
color: #0000ff;
margin: 5px 0px 5px 0px;
background-color: cyan;
display: inline-block;
width: 200px;
}
li {
position: relative;
}
li > ul {
display: none;
}
li:hover > ul, li.sfhover > ul {
left 100%;
top 0;
position: absolute;
display: inline-block;
}
li:hover > ul li, li.sfhover > ul li {
background-color: #33ff33;
width: 200px;
position: relative;
}
In Firefox and IE Level Three pops out to the right of Level Two Item Two. In Chrome Level Three pops out below Level Two Item Two.
I know it has something to do with making the link a block element, but I want the link to display as an inline-block. Also, I cannot change the HTML. It is coming from a CMS (I have simplified it to illustrate the problem)
Any suggestions would be appreciated.
You've got a couple of colons missing from your CSS, specifically this rule: li:hover > ul, li.sfhover > ul
left 100%; should beleft:100%; and top 0; should be top:0;
I have a code like this. But the background, that changes, doesn't touches the left border of the div. How can I do this without changing the position of the text ('Element 1')?
CSS:
li{
list-style-type: none;
}
li:hover{
background: green;
}
HTML:
<div style="border: 1px solid black; width: 200px;">
<ul>
<li>Element 1</li>
<li>Element 2</li>
<li>Element 3</li>
</ul>
</div>
http://jsfiddle.net/F698P/
Set padding of ul to 0px.
<style>
ul {
padding: 0;
}
li {
list-style-type: none;
padding-left: 65px;
}
li:hover {
background: green;
}
</style>
By removing the margin of the li and adding some padding to it.
li{
list-style-type: none;
margin-left: 0; /* assuming it had 10px margin first */
padding-left: 10px; /* more padding */
}
li:hover{
background: green;
}
The only thing that was not allowing it to have background color at the left side was the extra margin. You can also check for the ul properties for padding too. It is just an extra spacing.
ul was the problem
Ul was also having margin as I said,
so here is the updated fiddle: http://jsfiddle.net/afzaal_ahmad_zeeshan/F698P/1/
The CSS added was
ul {
padding: 0;
}
You can add more padding for the li element to make the text go back to where it had to go.
I have an evenly distributed menu like :
HTML
<nav>
<ul>
<li>Home
</li>
<li>About
</li>
<li>Contact
</li>
<li>Blog
</li>
</ul>
</nav>
CSS
nav ul {
padding:0;
margin:0;
text-align: justify;
text-transform: uppercase;
background-color: #000;
}
nav ul:after {
content:'';
width: 100%;
display: inline-block;
height: 0;
line-height: 0;
font-size: 0px;
}
nav ul li {
display: inline-block;
}
nav ul li a {
color: #fff;
}
This works great to spread the menu items accross the whole width of the bar as can be seen in this fiddle: http://jsfiddle.net/SjDEX/.
However, you can also see that a result of the ::after element the height of the ul is increased making extra space below the menu items.
Is there a way to get rid of this by making the after element not take up vertical space?
Setting its height to 0 does nothing and changing its display to block or something else breaks the layout.
It is the ul itself that has that height, not the :after, so just add
nav ul {
height: 20px;
}
Fiddle
And this code can be reduced to this:
nav ul:after {
content:'';
width: 100%;
display: inline-block;
}
I'm having a design issue with my css vertical menu.
It's working but it does not have the effect i would like to have when I do a mouse hover on a category
Below, you will see a simple vertical menu which appears when you hover your mouse over the main category
However I would like to have a small effect :
When the mouse is hover a category, i would like to add a background color (black).
It's working but I would like that the height and the width of the background to stick exactly to the same height and width of the text. Currently, I dont know why; the height of the background is more than the height of my text.
Here is some pictures of how it's right now and how i would like to be be.
How it 's now:
How I would like it to be:
Here is my code Html code
<div id="menu">
<ul id="MenuDeroulant">
<li style="margin-left:-10px;">Main categorie
<ul>
<li><a href="" >Subcat 1</a></li>
<li><a href="" >Subcat 2</a></li>
</ul>
</li>
</ul>
Here is my css code:
#MenuDeroulant
{
margin: 0;
padding: 0
}
#MenuDeroulant li
{
float: left;
list-style: none;
}
#MenuDeroulant li a
{
display: block;
padding: 0px 0px;
text-decoration: none;
color: #000;
white-space: nowrap;
text-align:center;
}
#MenuDeroulant li a:hover
{
background: #000;
color: #FFF;
}
#MenuDeroulant li ul
{ visibility: hidden;
padding: 0px 0px;
}
#MenuDeroulant li ul li
{
float: none;
display: inline;
}
#MenuDeroulant li ul li a
{
width: auto;
padding: 0px 0px;
}
#MenuDeroulant li ul li a:hover
{
background: #0000;
padding: 0px 0px;
}
Thanks in advance for your help and I wish you a very nice day,
Anselme
Use width:100% to all your <li> or li a elements and a fixed width to your <ul>. This will solve your issue.
With that CSS your nested ul is permanently hidden. You'll need something like
#MenuDeroulant li:hover ul {
visibility:visible;
}
to show the nested menu items then maybe display: inline on the #MenuDeroulant li ul li a
You can add a class to your menu hyperlinks giving them a margin-bottom:3px and it should bump up the links in the container.
I have an unordered inline horizontal list menu, when my mobile media query kicks in I want the css to change so that it has a parent item called 'menu' that when clicked displays the menu child links in a vertical list.
What is the best way to achieve this?
Should I create both types of menu and use css to switch their visibility or is there a way I can make the first menu become the new second menu?
Any help would be really appreciated. I am sure switching between hidden css property on each would work but I wasn't sure how semantic friendly this would be etc?
Cheers
Righto, it took a bit of work but I've created a working example of what you're after here
HTML
Open / Close
<ul id="navigation">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>
Item 5
<ul>
<li>Sub Item 1</li>
<li>Sub Item 2</li>
<li>Sub Item 3</li>
</ul>
</li>
<li>Item 6</li>
<li>Item 7</li>
</ul>
CSS
/* FOR ANYTHING GREATER THAN MOBILE RESOLUTION */
#media screen and (min-width: 480px) {
#nav-status {
display: none;
}
ul {
width: 100%;
min-height: 25px;
color:#fff;
background:#CCC;
overflow: visible;
}
ul li {
color:#000;
border-right: 1px solid #333;
width: 96px;
height: 21px;
padding:2px;
display: block;
float: left;
position: relative;
}
ul li:last-child {
border-right: none;
}
ul li ul {
display: none;
width: 100px;
color:#fff;
background:#666;
position: absolute;
top: 25px;
left: 0px;
overflow: hidden;
}
ul li:hover ul {
display: block;
}
}
/* FOR MOBILE RESOLUTIONS */
#media screen and (max-width: 480px) {
#nav-status {
display: block;
width: 100%;
height: 21px;
padding: 2px;
background: #000;
color: #FFF;
}
ul {
display: none;
width: 100%;
color:#fff;
background:#CCC;
overflow: visible;
}
ul li {
color:#000;
border-bottom: 1px solid #333;
width: 100%;
min-height: 21px;
padding:2px;
display: block;
position: relative;
}
ul li:last-child {
border-bottom: none;
}
ul li ul {
display: block;
width: 100%;
color:#fff;
background:#666;
overflow: hidden;
position: relative;
}
}
JAVASCRIPT
$(function() {
$('#nav-status').click(function(e) {
e.preventDefault();
$('#navigation').toggle();
});
});
In summary, any time the resolution drops under 480px wide, the mobile styling will kick in. This'll basically stack the menu items vertically and allow you to collapse/expand the nav with the open/close link. Anything larger than 480px will use the default styling which will order the menu items horizontally.
Hopefully it all makes sense :-)
You can keep the html markup the same, but set your media queries to intercept your chosen resolution and style the elements differently.
For example: http://jsfiddle.net/rYVz4/
HTML
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
CSS
#media screen and (min-width: 600px) {
ul { display: block; width: 100%; overflow: hidden; }
ul li { display: inline-block; padding: 2px 5px 2px 5px; width: 100px; float: left; }
}
#media screen and (max-width: 600px) {
ul { display: block; width: 110px; overflow: hidden; }
ul li { display: inline-block; padding: 2px 5px 2px 5px; width: 100px; }
}
As you can see, whenever the resolution is greater than 600px wide, the elements will be styled to float left and take up 100% of the width of the screen.
On any smaller resolution, the elements will stack vertically.
You can read more about media queries here
If you need more advanced behaviour, you can tie this in with javascript. This is one way of doing it:
function checkResolution() {
// Resolution width > 600px
if ($(window).innerWidth() > 600) {
// implement styling for these devices
}
}
$(function () {
$(window).resize(function () {
checkResolution();
});
checkResolution();
});
This code will tie into the window.resize event which'll run the relevant code if your browser is resized.
If the menu has the same items in both cases, I cannot see any reasons to make two copies of it. I would just make two different css-files, one for "regular" browsers and one for mobile media.
In the "regular" one, the "menu" parent item is hidden, and the menu is horizontal. In the mobile media one, the menu parent item is visible, the menu is a hidden vertical list that shows up (through css or javascript) when the menu parent item is clicked.