My problem is probably quite simple, but somehow I can’t get it fixed. In a group of objects with one object hovered over, I’d like the hovered object to remain unchanged, but all others are supposed to change.
To be more specific: I have an unordered list of menu-items. Whenever I hover over one of them, all the other items are supposed to become smaller. When I “unhover” the item they should change back again.
I found this post, but it’s answers didn’t work for me:
Set style for not not hovered elements only
This is what I tried so far:
/*This is the default size*/
#navigation ul li a
{
font-size: 14px;
}
/*When the list is hovered, change font-size (does’nt work)*/
#navigation ul:hover
{
font-size: 13px;
}
/*When the a menu-item is hovered change it’s font-size back to default*/
#navigation ul li a:hover
{
font-size: 14px;
}
This is one of the answers I found in the post I mentioned. It would be great if it could be done that simply with plain CSS. But it’s not working. Did I do something wrong?
I also tried something with jQuery, although I’m not an expert.
for(var i=1; i <= anzahlNavipunkte; i++)
{
var naviId = "navi" + i; // id name for every menu-item
var currentMenuItem = "#navigation li:nth-child(" + i + ") a";
$(currentMenuItem).attr('id', naviId); // gives the current menu-item a specific id
$('#navigation li a').hover(function()
{
var hoveredId = $(this).attr("id"); // get the id of of the hovered element
$('#' + hoveredId).hover(function()
{
console.log(hoveredId);
$('#' + hoveredId).css('font-size', '14px'); // hovered element gets default font-size
$('#navigation ul').css('font-size', '13px'); // other elements change to 13px
}, function()
{
$('#navigation ul').css('font-size', '14px'); // other elements change back
})
});
};
It doesn’t work, either. Probably because it’s the same approach as with the plain CSS-solution. Can anybody help me out?
I hope my explanation is understandable. If there are questions left please ask.
HTML:
CSS:
Try this…