I’m still fairly new to Bootstrap 4 and am having problems with menu styling, though it’s possible that my difficulties are actually caused by an imperfect understanding of CSS. I’m using Bootstrap 4.5.2.
In the example of navbar coding shown below, I can adjust the vertical spacing of the primary menu items (Home, About Us, Contact) by using something like .navbar-nav li a {padding: 10px 0 0 0;} in my CSS. However, all my attempts to adjust the padding of the dropdown menu items (Meetings, Subscriptions, History) have had no effect.
Any suggestions of how to do this would be very welcome.
It would also be useful to know how to adjust the horizontal position of the dropdown menu on wider screens where the full menu is visible at the top of the page. In some cases, this is being truncated on the right hand side by the edge of the browser window and so I want to shift it to the left.
Hi Paul,
Many thanks for putting me right on what I suspect are basic principles of CSS selection rules. I had already tried something almost identical, but with a space after the a. That doesn’t work.
.navbar-nav a .dropdown-item
In contrast, your solution does the job perfectly.
Have you any equally useful suggestions for my second question about shifting the dropdown menu sideways (or preventing it from being truncated in the first place)?
A space is a descendant selector and you don’t want a descendant in that scenario
If you are placing drop downs near the right edge of the viewport then probably best to use the bootstrap built in right alignment of the dropdown rather than trying to magic number guess a width that might fit.
Add the class dropdown-menu-right to the existing dropdown-menu class.
I had previously only looked for guidance about this topic in the BS4 navbar section on w3schools.com/bootstrap4 and hadn’t noticed that they also have a section called BS4 Dropdowns, which is where they mention dropdown-menu-right, etc.
The website I am currently working on has a logo in navbar-brand and I have deliberately made this logo project beyond the bottom of the navbar.
When I shrink the width of the browser window to simulate operation on a mobile phone, the normal horizontal navbar menu is replaced by a vertical menu, which overlays the bottom section of my logo. However, when I fix this as shown below, it also adds padding to the horizontal menu items.
Is there any way to limit my CSS to the vertical version of the menu?