Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between width: 50% and flex: 50% in a CSS flexbox?

When its container has a display for flex, what is the difference between setting an element to be flex: 50% and width: 50%. The outcome appears to be the same:

ul {   display: flex;   flex-flow: row wrap; }  .table a {   flex: 50%;   background: grey; }  .table2 a {   width: 50%;   background: grey; }  <ul class="table">   <a href="#">ad ds fdsaf dsfa dsfj dsf dsfj lkdsjflkdsa jflkdsja lkfjdslkjfldska jlkfdsajlkdjslkajflkd sjalkfjdslkjdsalkjdlakjfldksjflkdsjflkdsjd fdsd</a>   <a href="#">b</a>   <a href="#">c</a>   <a href="#">d</a> </ul>  <ul class="table2">   <a href="#">ad ds fdsaf dsfa dsfj dsf dsfj lkdsjflkdsa jflkdsja lkfjdslkjfldska jlkfdsajlkdjslkajflkd sjalkfjdslkjdsalkjdlakjfldksjflkdsjflkdsjd fdsd</a>   <a href="#">b</a>   <a href="#">c</a>   <a href="#">d</a> </ul> 

http://codepen.io/anon/pen/KAbof

like image 786
Evanss Avatar asked May 21 '14 09:05

Evanss


People also ask

What is the difference between Flex and width?

By default, if you have a width set and did not declare a flex basis value, width will function normally on that element. There is no difference between how flex-basis: will function on your element and how width: will function on your element. Width will even obey flex-shrink when using Flexbox.

What is difference between Flex and flexbox?

Flexbox allows fine-tuning of alignments to ensure exact specification sharing. Flex Direction allows developers to align elements vertically or horizontally, which is used when developers create and reverse rows or columns.

What does flex 1 mean flexbox?

If an element has flex: 1 , this means the size of all of the other elements will have the same width as their content, but the element with flex: 1 will have the remaining full space given to it.

How do you define flexbox width?

A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.


1 Answers

There is no effective difference in this instance.

In fact, it's because flex is shorthand for flex-grow, flex-shrink and flex-basis combined.

flex-basis defines the default size of an element before the remaining space is distributed.

So in this case, you have defined all a children` to 50%.

Reference Article: http://css-tricks.com/snippets/css/a-guide-to-flexbox/

like image 80
Paulie_D Avatar answered Sep 28 '22 11:09

Paulie_D