So I’ve designed a mock website layout for a made up company so I can test my skills at Twitter’s Bootstrap 3 but I’m having problems relaying what I’ve designed on photoshop to code.
I’m trying to get the whole navbar content centered, with links at either side of the brand logo. I’ve checked online, google, stackoverflow and more, however I can’t seem to get it anywhere near what I’ve designed! I’m having a hard time getting my head around it. It seems like it should be simple, and I’ve just overthought it’s complexity! The links need to be center to the image, but also center in the navbar itself. The layout as such:
[link] [link] [LOGO] [link] [link]Here’s an image of the mentioned design:
2
Answers
You can also try flexbox display. In order to implement it, you will have to:
display: flex
property to the parent element of the elements that you need to align (in this case: on the<ul>
element in the navbar, which contains the list of navigation links and the brand image)margin: auto
for the elements you need to align (in this case: on the<li>
elements)Optional: if you don’t want your elements to show up in a row (default bahavior), adjust the value of the
flex-direction
property. E.g. if you want to have the elements listed vertically, addflex-direction: column;
to the<ul>
element.Here is a sample code, with two different image-placements: on-top of the list for mobile devices and in the middle of the row for small devices and above.
To center the floated
navbar-nav
you can use relative position and negativeleft
,right
values on thenavbar-nav
itself and on the list items or child elements.To make sure that the logo is always centered you may think about not using a
nav-justified
but a regularnavbar-nav
with a fixed value for left and rightpadding
on the anchor tags, but with individual values for different screens.To get the vertical alignment working just add the appropriate
line-height
to the anchor tags.I have decided to hide the actual
navbar-brand
and only show it on sx breakpoint.For the centered logo it is the other way around, so we do not have the logo inside the collapsed nav. Just look at the example and i am sure you will get the idea.
Here is the example.