/

HTML / CSS / JavaScript Tutorial

CSS Property: flex-basis

[this page | pdf | back links]

The CSS (CSS3) flex-basis property indicates the initial length of a flexible element. If an element is not a flexible element then this property has no effect.

 

Valid property values (other than inherit and initial) are:

 

Value

Description

number

A length unit or percentage specifying initial length of flexible item

auto

(default value). Length is equal to length of flexible item or if not specified is set according to context

 

Default Value:

auto

JavaScript syntax:

e.g. object.style.flexBasis="100px"

Inherited:

No

Animatable:

Yes

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
div.x1 {border: 1px solid blue;}
div.x2 {border: 1px solid blue; flex-basis: 25px;}
div.x2a {border: 1px solid blue; flex-basis: 50px;}
</style>
</head>
<body>
Element with default property<br>
<div style="width: 200px; display: flex;">
  <div class="x1">1</div><div class="x1">2000</div>
</div><br>

Property set using in-file HTML style<br>
<div style="width: 200px; display: flex;">
  <div class="x2">1</div><div class="x2a">2000</div>
</div><br>

Property set using JavaScript<br>
<div style="width: 200px; display: flex;">
  <div id="x3">1</div><div id="x4">2000</div>
</div><br>

<script>
document.getElementById("x3").style.border = "1px solid blue";
document.getElementById("x4").style.border = "1px solid blue";
document.getElementById("x3").style.flexBasis = "25px";
document.getElementById("x4").style.flexBasis = "50px";
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedCSSPropertyFlexBasis() {
  var x = document.createElement("DIV"); x.style.flexBasis = "25px"; return (window.getComputedStyle(x, null).flexBasis == "25px");
}


NAVIGATION LINKS
Contents | Prev | Next | CSS Properties


Desktop view | Switch to Mobile