Zurb Foundation's top-bar is extremely useful. It works great as a main navigation for a site/app, and collapses to a mobile-friendly format on smaller devices.
Its one major shortcoming is the ability to make the top-bar full-width with evenly spaced nav items. Is there a way to make the top-bar full-width and the nav items evenly spaced?
Example
If the top-bar has 6 nav items (width varying length titles) and we're using the default width of 1000px for .rows (with 15px gutters) the 6 nav items should evenly space themselves across the 970px top-bar. The first and last nav items should be left and right justified respectively.
As the screen size reduces the nav items should shrink in width to maintain their even spacing until the $topbar-breakpoint causes the top-bar to collapse to the mobile format.
Requirements
The solution should be CSS-based.
The solution should match Foundation 5's compatibility chart. Namely this means it needs to support IE9+.
Beneath the $topbar-breakpoint the top-bar should work as normal.
Here's a jsFiddle with the Foundation 5 resources already loaded.
Here is another solution. It is based on flexbox which hasn't been supported by browser for very long and it is still only a candidate recommendation: CSS Flexible Box Layout Module
jsFiddle
If you provide a good fallback, like the original Foundation CSS it can be used.
Update
You could also use this jQuery solution as a fallback as I haven't found any polyfills for flexbox: http://jsfiddle.net/borglinm/x6jvS/14/
.top-bar-section > ul {
display: -webkit-flex;
display: -moz-flex;
display: flex;
-webkit-flex-direction: row;
-moz-flex-direction: row;
flex-direction: row;
}
.top-bar-section > ul > li {
float: none;
-webkit-flex: 1;
-moz-flex: 1;
flex: 1;
}
.top-bar-section > ul > li > a {
white-space: nowrap;
text-overflow: ellipsis;
text-align: center;
overflow: hidden;
}
Here's a solution that might need a bit of tweaking
JSFiddle Here
Sticking to the CSS-only requirements, the only feasible way I can think of is using CSS tables. We create nested table, table-rows and table-cells. The table-cells, by default, will try to maintain equal spacing between itself and other table-cells.
The table-row needs to span the entire topbar minus any Foundation topbar title-areas. To do this, we use an overflow: hidden trick to make the .top-bar-section span the remaining width of the topbar. Finally, we wrap our topbar with a div that has display: table and spans its parent.
Here's the relevant CSS
.top-bar-section {
overflow: hidden;
}
.center-topbar {
display: table;
width: 100%;
}
.center-topbar .full-width {
display: table-row;
}
.center-topbar .full-width li {
display: table-cell;
float: none;
text-align: center;
}
What we are left is with a topbar whose elements are centered and have widths that vary depending on its contents. The $topbar-breakpoint works as normal as well.
Improvements?
Works on Chrome + Safari well on my end (OS X). For Firefox, the dropdown arrow is not displaying due to the removal of the left float. Just wanted to post this to get the conversation going. Anyone have any improvements?
Here's a solution using some built in foundation classes...basically I added 4 classes to your fiddle.
http://jsfiddle.net/x6jvS/7/
<div class="row">
<div class="small-12 columns">
<nav class="top-bar contain-to-grid" data-topbar>
<ul class="title-area">
<li class="name">
<h1></h1>
</li>
<li class="toggle-topbar menu-icon">Menu</li>
</ul>
<section class="top-bar-section">
<!-- Right Nav Section -->
<ul class="full-width web button-group large-block-grid-6">
<li>Link 1</li>
<li class="has-dropdown">
Long Link 2
<ul class="dropdown">
<li>First link in dropdown</li>
</ul>
</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Even Longer Link 5</li>
<li>Link 6</li>
</ul>
</section>
</nav>
</div>
</div>
added class "contain-to-grid" to the nav element
added classes "web button-group large-block-grid-6" to the "section.top-bar-section > ul" (first ul in that section)
and blammo...seems to work fairly well cross-browser
Related
First of all, i'm new to css.
The problem is i'm trying to do a horizontal navigation bar, on the left. But my flexbox doesn't work, the navigation keeps getting vertical. Seems like it's locked, don't know if that's posible.
Please, how can i make my horizontal navigation bar using flexbox?
HTML:
<header>
<nav>
<ul>
<li>MENU</li>
<li>HISTORY</li>
<li>LEARN MORE</li>
</ul>
</nav>
<h1>ORCA</h1>
</header>
CSS:
nav, a {
font-family: outfit-bold;
color: black;
font-size: 16px;
text-decoration: none;
display: flex;
}
When i put "display: flex" i was expecting it to be horizontal, but it was vertical.
I also put display: inline-block and didn't work.
how it is navigation bar vertical
ul {
display: flex;
}
<header>
<nav>
<ul>
<li>MENU</li>
<li>HISTORY</li>
<li>LEARN MORE</li>
</ul>
</nav>
<h1>ORCA</h1>
</header>
try to put display: flex on the ul element. It will put the children in line
If you want to read a really good and detailed resource about flexbox - take a look here -> https://css-tricks.com/snippets/css/a-guide-to-flexbox/
I'm trying to build a menu in css using flexbox.
I have already made it responsive so it shrinks from horizontal menu to dropdown menu.
But I struggle separating first menu link from others and putting on the left while the rest are on the right.
As shown in picture I want to put first li the home icon on the left and the rest li items on the right.
Any suggestions?
menu example
From your image, I think this is a very simple example of what you want.
ul {
display: flex;
align-items: flex-end;
flex-direction: column;
}
li:first-child {
align-self: flex-start;
position: absolute; /* used only to preserve row alignment */
}
<ul>
<li>home</li>
<li>whatever 1</li>
<li>whatever 2</li>
<li>whatever 3</li>
</ul>
Using flex to lay out a menu, all works nicely on chrome on windows:
However on ios devices (various iphones and ipads) the longer content does NOT expand its container as much as it should, and/or the smaller items don't shrink as much as they should, and the longer content gets cut off:
I'm viewing on Safari 9, which should be pretty up to date and not need prefixes (I've tried prefixes anyway - they didn't work). Am I missing something?
<style>
.lvl1Menu
{
width:750px;
display: flex;
flex-direction: row;
justify-content: space-around;
background-color:white;
padding:0;
font-size: 16px;
flex-direction: row;
}
li{
list-style: none;
text-align:center;
background-color:#EEEEEE;
padding:0px;
font-size: 25px;
flex: 1 1 auto;
}
</style>
<ul class="lvl1Menu">
<li>
Some words
</li>
<li>
Some words
</li>
<li>
ONELONGWORD
</li>
<li>
Some words
</li>
<li>
Some words
</li>
<li>
Some words
</li>
<li>
Some words
</li>
</ul>
"Chrome, Opera, and Safari do not honor the default min-content size of flex items. Set flex-shrink to 0 (instead of the default 1) to avoid unwanted shrinkage."
Source:
https://philipwalton.com/articles/normalizing-cross-browser-flexbox-bugs/
I'm guessing the Chrome has fixed this since then, but Safari hasn't.
Perhaps you need to set flex-shrink:1; and possibly flex-grow:1;
https://css-tricks.com/almanac/properties/f/flex-shrink/
How can I make the <li> item width fit the text length in Bootstrap 3? My menu item is shorter than the text which causes my class="active" to look ugly.
This is the navbar in which it occurs:
<div id="menu" class="col-md-1">
<ul class="nav nav-pills nav-stacked">
<li class="active">Startseite</li>
<li>Kontakt</li>
</ul>
</div>
make a .custom_li class and give
.custom_li{
display:inline-block;
}
EDIT
To force same size, i'll suggest using max-width and place it under some media-query
li{
display: inline-block;
max-width:50%; /* limit width */
word-break:break-all; /* wrap extended text*/
border:1px solid red /* demo */
}
demo here
some optional things
some optional things
When I tried display: inline-block; it removes the bullet.
Instead, I use float:left; to have them only as wide as text, while preserving the bullet. Optionally, add clear:both; to keep it as a vertical list with each item on a new line.
CSS
.my-list > li {
float: left;
clear: both; /* remove this if you want them flowing inline */
}
HTML
<ul class="my-list">
<li>First Item</li>
<li>Second Item</li>
<li>Third Item</li>
<li>Fourth Item</li>
</ul>
If the display: inline-block; or display: block; is messing up the alignment.
Then just use width: fit-content;
Prevent it becoming a block by adding display: inline-block; to the proper element.
Post more code and preferably CSS if you want details.
I got it to work with the following css:
ul {
margin: 0 auto;
width: fit-content;
}
li{
display:flex;
margin: 0.5rem auto;
}
Basically what I did was make the container width to fit content. Used the CSS hack to make sure it would center using the margin. In the li tag I wanted the contents to be centered so I set it that way
I need to put an unknown number of divs (likely a limit of about 5) into a parent container and always make sure they remain equally divided. I'm not sure if this can be done with CSS alone but I figured I better ask. So if we know that 3 divs are used:
<style>
.menu-button {
float: left;
width: 33%;
}
</style>
<div>
<div class="menu-button">Button X</div>
<div class="menu-button">Button Y</div>
<div class="menu-button">Button Z</div>
</div>
Seems to work, but what if the number of .menu-button divs is unknown? Is there a better way to do it so it automatically adjusts horizontally?
To do that with any element, you have two solutions:
make the browser simulating the table behavior
using Flexible Box layout
For instance, to build an horizontal menu, with equal width of every li elements, with this HTML code :
<div id="menu">
<ul>
<li>First Element</li>
<li>Second Element</li>
<li>Third Element</li>
...
<li>N Element</li>
</ul>
</div>
Using the table layout, CSS code would look like that:
#menu{
width: 100%; /* for instance */
display: table;
table-layout: fixed;
}
#menu ul{
display: table-row;
}
#menu ul li{
display: table-cell;
}
Flexible Box layout is a more modern solution, and it's pretty widely supported nowadays:
#menu{
width: 100%; /* for instance */
}
#menu ul{
display: flex;
flex-flow: row;
}
#menu ul li{
flex-grow: 1;
}
Unfortunatly I think you'll have to use tables to do this. As <td>'s resize itslef to fit into the full width.
HTH
Try this solution (demo page).
Basically, you need to make the divs display:inline-block, and apply text-align:justify to them. Then force a line break. One drawback is there will always be some space between divs, i.e. no way to make their edges touch.