Quantcast
Channel: Active questions tagged flexbox - Stack Overflow
Viewing all articles
Browse latest Browse all 1623

Why does flex: 0; override the width property?

$
0
0

This is my first year teaching web design, and we are covering flexbox layout. I have run into a glitch with the flex property that I cannot find an explanation for anywhere.

I have reviewed this using a wide range of resources and all indicated that setting flex to 0 is the same as setting it to flex: 0, 0, auto. In this case, auto will use a flex item's width property as its flex-basis value. However, I am finding this to not be true. When using the shorthand method (as most of these resources suggest) of flex:0, this overrides my width value.

Here is the HTML:

<nav id="menu"><div id="Item1" class="menuitem">Item 1</div><div id="Item2" class="menuitem">Item 2</div><div id="Item3" class="menuitem">Item 3</div><div id="Item4" class="menuitem">Item 4</div><div id="Item5" class="menuitem">Item 5</div></nav>

Here is the pertinent CSS:

.menuitem { width: 120px;            padding: 3px;             text-align: center;            font-family: Arial;            font-size: 16pt;            font-weight: bold;            flex: 0;            }

This is what I expected to happen from what I have researched (Flexbox row using flex: 0, 0, auto;):

A navigation bar with five items arranged horizontally. Each contains a label containing the word "item" and the one-based index of the item.

However, here is the result I get (Flexbox row using flex:0;):

A navigation bar with five items arranged horizontally. Each contains a label containing the word "item" and the one-based index of the item. The labels have wrapped so that the items overflow the height of the navigation bar.

When I use the longer version, flex: 0, 0, auto;, all of my items are sized properly.

Curious to see if anyone has an explanation for why this would be occurring that I could pass along to my students.


Viewing all articles
Browse latest Browse all 1623

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>