I have the following code to create a horizontal navigation bar however it’s not filling up the full width of the browser like I want it to. What is wrong with my code? There are no anchor tags because the html is created dynamically with javascript and it has a click event to go to the sections. This is for an assignment so it has to be done that way.
<header class="page__header">
<nav class="navbar__menu">
<!-- Navigation starts as empty UL that will be populated with JS -->
<ul id="navbar__list"></ul>
</nav>
</header>
I would be tempted to temporarily forget the JavaScript insertion and hardcode a couple of <li> elements. Once it is doing what you want it to do then remove the hard coded elements and add the JavaScript.
Is the code you’ve posted given as the assignment?
Unless you want a general discussion about possible ways to distribute a nav bar horizontally:
– Please tell what you are allowed to change in e.g. the CSS you’ve posted.
– Please also give an example of what could populate the nav list.
Using floats is a fairly “old” way of positioning your menu items. It will require you to size them proportionally to the total width.
It would be easier to use something like display table or flex, as these can allocate space evenly per item, or divide the space available proportionally to the size of each item, without having to work any of that out yourself.
Yes, I have 3 tabs that I want to make full width. I don’t know what the border box model is. I tried box-sizing: border-box; but it didn’t change the design. How do I implement flex box?
The 33% doesn’t have to be that accurate as long s its less than a third of the space. Flex will flex to fill any gaps automatically. It could be flex:1 0 0% and still look much the same unless you had menu items with lots more text and then they would be wider etc. They will always fit though.
Course. You should look into those articles I linked you to. They’re helpful. I personally like the one from CSS-Tricks, and generally love that website, cause it explains things with pictures