Truly centering the menu in Navbar? #36486
-
I am trying to create a navbar with the brand-name aligned left, menu items centred and possibly, eventually, another item, such as search input, right-aligned. The examples I looked at online use "justify-content-center" (see after button tag below) on the menu items to center them. But, the result is not truly centred. The "centred" menu is a little off to the right because it is affected by the width of the brand name/logo content on the left. To make the menu truly centred, I removed the brand name/log from the flow with "position-absolute". However, this makes the Navbar less responsive as now the menu overlaps with the brand name when the viewport shrinks as the menu div now ignores the brand-name/logo content. What would be recommended to TRULY center the menu while preventing it from overlapping with the brand name/logo when the viewport shrinks--before I remove the brand name with d-none (see below) for mobile viewports?
|
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 1 reply
-
I found this excellent tutorial, which uses using The author explains the central issue, "The “centered” child element is only horizontally centered within the remaining space in the parent element, and not the “true center” of the parent element. In this case the width of the centered element is equal to the width of the parent element minus any sibling element(s). The centered element can only be “truly centered” if the width of child elements on both the left and right of it are equal." The tutorial solution boils down to this:
The "solution" applied to my nav looks like the following below. Hopefully overriding CSS
HTML
|
Beta Was this translation helpful? Give feedback.
-
Posting this as a suggestion with our grid and utilities: #36523. |
Beta Was this translation helpful? Give feedback.
-
Looking more around the internet for flex box approaches for displaying left/true-center/right-align items in the same row, I came across this simple approach: https://stackoverflow.com/questions/32378953/keep-the-middle-item-centered-when-side-items-have-different-widths HTML version (The centerline class adds a central divider to check the alignment of the center div):
I'll likely update this thread with the above approach applied to the navbar. |
Beta Was this translation helpful? Give feedback.
-
Here is the alternative flex-1 approach for a truly centered navbar I discussed in my previous post applied to a Bootstrap navbar. This approach does not require specifying specific col spans. However, due to the limitation of navbar-collapse for the toggler requiring its own div, I am not able to avoid collapsing the Navbar brand with everything else. Again, I've included a fixed red center line div to see that the navbar menu remains center.
|
Beta Was this translation helpful? Give feedback.
-
I just noticed that the bootstrap website itself has a well-designed responsive three-section navbar with what looks like a truly centered search box (https://getbootstrap.com/docs/5.2/). I'll be examining it in detail to learn from it. I like how it collapses down. I suggest that Bootstrap include a simplified version of it as an example in the documentation. With so many scripts and styles going on, I am having difficulty replicating the website's navbar locally. It does look like it is using grid display as well for layout? |
Beta Was this translation helpful? Give feedback.
I found this excellent tutorial, which uses using
display : grid
instead ofdisplay : flex
to center. (https://chrisbracco.com/css-truly-center-a-single-child-element-horizontally-when-siblings-are-present/).The author explains the central issue, "The “centered” child element is only horizontally centered within the remaining space in the parent element, and not the “true center” of the parent element. In this case the width of the centered element is equal to the width of the parent element minus any sibling element(s). The centered element can only be “truly centered” if the width of child elements on both the left and right of it are equal."
The tutorial solution boils down to this: