[html] Flexbox not giving equal width to elements

Attempting a flexbox nav that has up to 5 items and as little as 3, but it's not dividing the width equally between all the elements.

Fiddle

The tutorial I'm modeling this after is http://www.sitepoint.com/responsive-fluid-width-variable-item-navigation-css/

SASS

_x000D_
_x000D_
* {_x000D_
  font-size: 16px;_x000D_
}_x000D_
_x000D_
.tabs {_x000D_
  max-width: 1010px;_x000D_
  width: 100%;_x000D_
  height: 5rem;_x000D_
  border-bottom: solid 1px grey;_x000D_
  margin: 0 0 0 6.5rem;_x000D_
  display: table;_x000D_
  table-layout: fixed;_x000D_
}_x000D_
.tabs ul {_x000D_
  margin: 0;_x000D_
  display: flex;_x000D_
  flex-direction: row;_x000D_
}_x000D_
.tabs ul li {_x000D_
  flex-grow: 1;_x000D_
  list-style: none;_x000D_
  text-align: center;_x000D_
  font-size: 1.313rem;_x000D_
  background: blue;_x000D_
  color: white;_x000D_
  height: inherit;_x000D_
  left: auto;_x000D_
  vertical-align: top;_x000D_
  text-align: left;_x000D_
  padding: 20px 20px 20px 70px;_x000D_
  border-top-left-radius: 20px;_x000D_
  border: solid 1px blue;_x000D_
  cursor: pointer;_x000D_
}_x000D_
.tabs ul li.active {_x000D_
  background: white;_x000D_
  color: blue;_x000D_
}_x000D_
.tabs ul li:before {_x000D_
  content: "";_x000D_
}
_x000D_
<div class="tabs">_x000D_
  <ul>_x000D_
    <li class="active" data-tab="1">Pizza</li>_x000D_
    <li data-tab="2">Chicken Noodle Soup</li>_x000D_
    <li data-tab="3">Peanut Butter</li>_x000D_
    <li data-tab="4">Fish</li>_x000D_
  </ul>_x000D_
</div>
_x000D_
_x000D_
_x000D_

This question is related to html css flexbox

The answer is


There is an important bit that is not mentioned in the article to which you linked and that is flex-basis. By default flex-basis is auto.

From the spec:

If the specified flex-basis is auto, the used flex basis is the value of the flex item’s main size property. (This can itself be the keyword auto, which sizes the flex item based on its contents.)

Each flex item has a flex-basis which is sort of like its initial size. Then from there, any remaining free space is distributed proportionally (based on flex-grow) among the items. With auto, that basis is the contents size (or defined size with width, etc.). As a result, items with bigger text within are being given more space overall in your example.

If you want your elements to be completely even, you can set flex-basis: 0. This will set the flex basis to 0 and then any remaining space (which will be all space since all basises are 0) will be proportionally distributed based on flex-grow.

li {
    flex-grow: 1;
    flex-basis: 0;
    /* ... */
}

This diagram from the spec does a pretty good job of illustrating the point.

And here is a working example with your fiddle.


To create elements with equal width using Flex, you should set to your's child (flex elements):

flex-basis: 25%;
flex-grow: 0;

It will give to all elements in row 25% width. They will not grow and go one by one.


Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to flexbox

Force flex item to span full row width vuetify center items into v-flex Equal height rows in CSS Grid Layout display: flex not working on Internet Explorer Center the content inside a column in Bootstrap 4 Vertical Align Center in Bootstrap 4 How to use zIndex in react-native Bootstrap 4 align navbar items to the right Does 'position: absolute' conflict with Flexbox? Make flex items take content width, not width of parent container