- This topic has 15 replies, 2 voices, and was last updated 5 years ago by David.
-
AuthorPosts
-
April 5, 2019 at 9:19 am #860744Florian
Hey,
I want to add a underline to selected menu items.
This code I found online works:
@media (min-width:769px) {
.main-navigation .main-nav ul li[class*=”current-menu-“] > a {
border-bottom: 2px solid #ffffff;
}
}but it is much wider than the menu item because it doesnt respect the padding. Can you tell me how to create an underline for selected menu items that are only as wide as the menu item itself?
April 5, 2019 at 9:58 am #860762DavidStaffCustomer SupportHi there,
we can create a pseudo after element like so:
@media (min-width:769px) { .main-navigation .main-nav ul li[class*="current-menu-"]:not(.dropdown)>a { position: relative; } .main-navigation .main-nav ul li[class*="current-menu-"]:not(.dropdown)>a:after { content: ''; position: absolute; bottom: -5px; left: 12px; right: 12px; height: 2px; background-color: #000000; pointer-events: none; } .is_stuck.main-navigation .main-nav ul li[class*="current-menu-"]:not(.dropdown)>a:after { bottom: 0; } }
Bottom -5px pushes the line a little below the text, left and right positons are the same as horizontal padding, Height determines it thickness and background-color is its color.
April 5, 2019 at 10:02 am #860764FlorianThank you, that works!
April 5, 2019 at 10:31 am #860783DavidStaffCustomer SupportGlad to be of help.
April 5, 2019 at 12:05 pm #860841FlorianSorry, there is one problem. I use the sticky menu (always visible) and the underline is always placed under the bottom of the menu. It is like that no matter how large I set the sticky menu items’ height.
April 5, 2019 at 12:24 pm #860850DavidStaffCustomer SupportI edited the code above to include a variation for the sticky navigation – it will now sit on the bottom of the nav bar when stuck.
April 5, 2019 at 12:31 pm #860855FlorianThank you, great! The only thing that doesn’t look perfect is that the sticky menu has a sort of “expand animation” when it appears, and the bottom line has a bit of a delay to that. But if there is no easy fix for that I can live with it.
April 5, 2019 at 12:32 pm #860857FlorianOh, and is it possible to remove that line for a menu item that is a drop down?
April 5, 2019 at 12:36 pm #860862FlorianNevermind, there are other problems like the line disappearing in drop-down menus when I hover over the item below the selected one.
I think I will leave it for now since this seems all a bit messy.Thanks anyways! I think this will work perfect for menus without drop downs!
April 5, 2019 at 12:49 pm #860876DavidStaffCustomer SupportNo problems. I edited the code above to exclude dropdowns in case you want to revisit in the future.
April 5, 2019 at 1:04 pm #860889FlorianOk, so then only 1 thing is left: The line also moves up and down for drop down items, but they never change their size. Is this the correct way to fix it:
.main-navigation .main-nav .dropdown ul li[class*=”current-menu-“]>a:after {
bottom: 0;
}April 5, 2019 at 1:34 pm #860906DavidStaffCustomer SupportDo you think it would be better if the submenus didn’t have the underlines? considering they’re not always visible?
April 5, 2019 at 1:36 pm #860909FlorianI am not sure what is the best practice here. To me right now it looks correct when they ARE underlined. But I am not sure. Do you think different?
The code you posted above removes the underline for the dropdown parent item, but they are still visible on the sub items.
The code I added stops them from moving up and down with the sticky menu.April 5, 2019 at 1:53 pm #860919DavidStaffCustomer SupportI think what you have works really well – if you want the line in the submenus to fit the menu item width then you can add a
display: inline-block;
property inside this rule:.main-navigation .main-nav ul li[class*="current-menu-"]:not(.dropdown)>a { position: relative; }
ie.
.main-navigation .main-nav ul li[class*="current-menu-"]:not(.dropdown)>a { position: relative; display: inline-block; }
April 5, 2019 at 2:31 pm #860939FlorianNow everything seems to be perfect, thank you very much!
-
AuthorPosts
- You must be logged in to reply to this topic.