CSS Property flex-basis








The flex-basis property is a property of the Flexible Box Layout.

The flex-basis property sets the initial length of a flexible item before any available space is distributed according to the flex factors.

When omitted from the flex shorthand, its specified value is the length zero.

A flex-basis value set to auto sizes the element according to its size property.

Summary

Initial value
auto
Inherited
no
CSS Version
CSS3
JavaScript syntax
object.style.flexBasis="20px"
Animatable
yes

CSS Syntax

flex-basis: number|auto|initial|inherit;




Property Values

number
A length unit or percentage sets the initial length of the flexible item(s)
auto
Default value. The length is equal to the length of the flexible item.
initial
sets to default value
inherit
Inherits this property from its parent element

Browser compatibility

flex-basis Yes 11.0 28.0 (18.0 -moz-) Yes Yes

Example

<!DOCTYPE html>
<html>
<head>
<style>
#main {<!--from ww  w . j  a  va  2s  .  co  m-->
    width: 350px;
    height: 100px;
    display: -webkit-flex; /* Safari */
    display: flex;
}

#main div {
    -webkit-flex-grow: 0; /* Safari 6.1+ */
    -webkit-flex-shrink: 0; /* Safari 6.1+ */
    -webkit-flex-basis: 40px; /* Safari 6.1+ */
    flex-grow: 0;
    flex-shrink: 0;
    flex-basis: 20px;
}

#main div:nth-of-type(2) {
    -webkit-flex-basis: 20px; /* Safari 6.1+ */
    flex-basis: 20px;
}
</style>
</head>
<body>

<div id="main">
  <div style="background-color:red;"></div>
  <div style="background-color:blue;"></div>
  <div style="background-color:yellow;"></div>
  <div style="background-color:pink;"></div>
  <div style="background-color:grey;"></div>
</div>

</body>
</html>

Click to view the demo





Example 2

<!DOCTYPE html>
<html class=''>
<style>
.flex-container {<!--from w  ww.j av a2  s  .  c o m-->
  padding: 0;
  margin: 0;
  list-style: none;
  
  -ms-box-orient: horizontal;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -moz-flex;
  display: -webkit-flex;
  display: flex;
}

.flex-item {
  background: red;
  padding: 10px;
  border: 1px solid black;
  color: white;
  text-align: center;
}

.flex1 { flex: 1 1 20em; }
.flex2 { flex: 2 2 20em; } 
</style>
</head>
<body>
<ul class="flex-container">
  <li class="flex-item flex1">1</li>
  <li class="flex-item flex2">2</li>
</ul>
</body></html>

Click to view the demo