I have spent some time trying to figure out why it is so difficult to center a nav bar. I searched the web and downloaded some examples that claimed to be centered. After some checking, everyone I found was not centered as advertised. So, I decided to see if I could figure it out on my own and made good progress.
So, if someone can let me know where to post this info - which BTW, is simply a codepen link, I will do so.
Thanks for the quick response. Here is the codepen link.
The only thing that needs tweaking is how to evenly space each menu item within the nav bar. This will become abundantly clear once the borders are removed because most menu items have a different char count.
Hopefully, someone with more experience than I can figure this out.
Thatās a pretty common way. Using margin:0 auto to center things (not just menus) have been around for ā¦ever since CSS was made. 15ish years maybe? 10? Hard to say when it was introduced (auto margins) since I wasnāt around.
Thatās a neat demo though so thank you for posting it.
Glad youāve taken some time to experiment as you learn a lot that way
However, the approach you have taken is slightly flawed because you have set fixed widths for the nav items which means should a user resize their text it will no longer fit into the width you specified.
An easier approach is to use padding as shown in Ralphs demo (although I would have used display:table-cell instead of inline-block to allow text to wrap within cells if needed and to keep equal cells).
Whenever you find yourself making complicated calculations then most of the time that is the wrong approach. For example what happens when the client says he needs one more menu item or one less menu item you would then need to change all your calculations.
Much better to let the menu flow by itself without fixing the widths on each item.
Iām not actually sure it is, as Iāve noticed it has a slight display problem at one end in some browsers. It was just a quick experiment, but @PaulOB will know a better solution.
Edit: hah, heās already spoken in post 7.
Yes, I did that at first, but the spacing was a bit weird. Instead of investigating further, I copped out and used inline-block, but Iām not sure it works too well. I suspect display: flex would work well, but havenāt time to figure it out right now.
I have to agree with all that you have said. As a rookie coder, I know I have a long ways to go. This was done simply as a learning experience for me. That said, with all the help I have received on this forum, my skill level is improving substantially.
There are often many ways to do the same thing as shown by the examples above but generally it depends on context as to which is the best method to use.
In your example you seemed to want a menu that wasnāt full width but perhaps only 70% wide and then centred which you could have done easily by using display:table and setting a 70% width and margin:auto
Iām not a fan of bare anchors next to each other but this is a feature of html5 and does make things easier for examples like this.
The table-cell algorithm is in fact more clever than just providing equal space as it distributes the items across the menu but allows more room for longer items which is more pleasing to the eye than having a fixed amount of space on each item no matter how long the text is.
The table-cell algorithm also means that no widths are needed on the cells and will accommodate the text size differences between browsers without upsetting anything.
Good point. I only rejected it because it didnāt do what was asked, but it does look better that way imho.
Iād say nav is a feature, but from what I read it doesnāt help make bare links any more readable for screen readers ⦠unless something has changed.
Even though you can just put bare links next to each other, as they are a list of links, shouldnāt they still be in a unordered list? Unless Nav implies an implicit list of links which I donāt think it does?
The specs are a little ambiguous on this as they first say:
But then goes on to say without explaining the difference from the first term:
I believe that ānavā describes navigation items rather than a list of things. In my mind either the list is superfluos or the nav is superfluos. I find it a little pointless to add a new html5 element only to nest superfluous elements inside.
Notwithstanding the above the demo I created cannot have list items holding the anchors because that breaks the table-cell structure (if you always want cells of equal height). It is just impossible to do without the structure I used. If you set the list to display:table-cell and then the anchor to display:block you will find that when text wraps to a new line the other links do not fill the whole cell because anchors are no longer display:table-cell (the list elementis display:table-cell but it cant be a link as well).
Itās one of those occasions when yiu need to bend the rules a little to get what you want
I believe that Mallory mentioned that modern screen readers have no problems with this. It used to be that they (screen readers) would read links next to each other as a sentence without pausing. It would be easier enough to put a pipe character between each anchor and just move the pipe off screen if this was a real issue.
I see what you mean, but in my mind, it generically indicates to a chunk of information which is related to navigation. So an agent could look for the nav block and analyse it for navigational information instead of scanning a whole document and making some sort of guess on what is related to navigation. A bit like <div id="navigationBlock"> but where everyone knows the id terminology that will be used to identify the navigation block.
Iām sure you have a reason why this doesnāt work, as you know far more about CSS than me, but it works ok in Chrome!
Actually that version isnāt quite right, because the anchors dont fill the list elements, and the colours are wrong. I edited the code accordingly though so my post is how I meant it to be.