Javascript Reference - HTML DOM Style flexBasis Property








The flexBasis property gets and sets the initial length of a flexible item.

Browser Support

flexBasis Yes 10.0 Yes Yes (WebkitFlexBasis) Yes

Syntax

Return the flexBasis property:

var v = object.style.flexBasis

Set the flexBasis property:

object.style.flexBasis='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




Technical Details

Default Value: auto
Return Value: A string representing the flex-basis property
CSS Version CSS3

Example

The following code shows how to set the initial length of a flex-item to 200 pixels.


<!DOCTYPE html>
<html>
<head>
<style>
#main {<!--  w  w  w  .  jav  a2 s  .c om-->
    width: 350px;
    height: 100px;
    border: 1px solid #c3c3c3;
    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: 50px;
}
</style>
</head>
<body>

<div id="main">
  <div style="background-color:coral;">A</div>
  <div style="background-color:lightblue;" id="myDIV">B</div>
</div>

<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myDIV").style.WebkitFlexBasis = "200px"; // Safari 6.1+
    document.getElementById("myDIV").style.flexBasis = "200px";
}
</script>

</body>
</html>

The code above is rendered as follows: