HTML | DOM Style flexBasis Property

The DOM Style flexBasis property is used to set or return the initial length of a flexible item.
Note: If the element is not a flexible item, this property would have no effect.

Syntax:

  • It returns the flexBasis Property:
    object.style.flexBasis
  • It used to set the flexBasis Property:
    object.style.flexBasis = "number | auto | initial | inherit"

Property Values:



  • number: This is used to specify the initial length in terms of fixed the length units or a percentage.
  • auto: This is used to set the length according to the length of the flexible item. The length will however, be according to the content if length is not specified. This is the default value.
  • initial: This is used to set this property to its default value.
  • inherit: This inherits the property from its parent.

Example-1: Using the number value.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        DOM Style flexBasis property
    </title>
  
    <style>
        .main {
            width: 300px;
            height: 150px;
            border: 1px solid;
            display: flex;
        }
          
        .main div {
            width: 250px;
            height: 150px;
            font-size: 2rem;
            text-align: center;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green">
      GeeksforGeeks
  </h1>
    <b>DOM Style flexBasis</b>
  
    <p>
      Click the button to change the value
      of the flexBasis to "100px"
    </p>
  
    <div class="main">
        <div style="background-color:green;">
            Geeks</div>
        <div id="div1"
             style="background-color:lightgreen;">
            For</div>
        <div style="background-color:green;">
            Geeks</div>
    </div>
  
    <button onclick="changeFlexBasis()">
        Change flexBasis
    </button>
  
    <script>
        function changeFlexBasis() {
            document.querySelector(
                '#div1').style.flexBasis = "100px";
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:

Before clicking the button:
length-before

After clicking the button:
length-after

Example-2: Using the auto value.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>
        DOM Style flexBasis property
    </title>
  
    <style>
        .main {
            width: 300px;
            height: 150px;
            border: 1px solid;
            display: flex;
        }
          
        .main div {
            width: 250px;
            height: 150px;
            font-size: 2rem;
            text-align: center;
        }
          
        #div1 {
            flex-basis: 50px;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green">
      GeeksforGeeks
  </h1>
    <b>DOM Style flexBasis</b>
  
    <p>
      Click the button to change the
      value of the flexBasis to "auto"
    </p>
  
    <div class="main">
        <div style="background-color:green;">
            Geeks</div>
        <div id="div1" 
             style="background-color:lightgreen;">
            For</div>
        <div style="background-color:green;">
            Geeks</div>
    </div>
  
    <button onclick="changeFlexBasis()">
        Change flexBasis
    </button>
  
    <script>
        function changeFlexBasis() {
            document.querySelector(
                '#div1').style.flexBasis = "auto";
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:

Before clicking the button:
auto-before

After clicking the button:
auto-after

Example-3: Using the initial value.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        DOM Style flexBasis property
    </title>
  
    <style>
        .main {
            width: 300px;
            height: 150px;
            border: 1px solid;
            display: flex;
        }
          
        .main div {
            width: 250px;
            height: 150px;
            font-size: 2rem;
            text-align: center;
        }
          
        #div1 {
            flex-basis: 50px;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green">
      GeeksforGeeks
  </h1>
    <b>DOM Style flexBasis</b>
  
    <p>
      Click the button to change the 
      value of the flexBasis to "initial"
    </p>
  
    <div class="main">
        <div style="background-color:green;">
            Geeks</div>
        <div id="div1" style="background-color:lightgreen;">
            For</div>
        <div style="background-color:green;">
            Geeks</div>
    </div>
  
    <button onclick="changeFlexBasis()">
        Change flexBasis
    </button>
  
    <script>
        function changeFlexBasis() {
            document.querySelector(
                '#div1').style.flexBasis = "initial";
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:


Before clicking the button:
initial-before

After clicking the button:
initial-after

Example-4: Using the inherit value.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        DOM Style flexBasis property
    </title>
  
    <style>
        #parent {
            flex-basis: 50%;
        }
          
        .main {
            width: 300px;
            height: 150px;
            border: 1px solid;
            display: flex;
        }
          
        .main div {
            width: 250px;
            height: 150px;
            font-size: 2rem;
            text-align: center;
        }
          
        #div1 {
            flex-basis: 50px;
        }
    </style>
</head>
  
<body>
    <h1 style="color: green">
      GeeksforGeeks
  </h1>
    <b>DOM Style flexBasis</b>
  
    <p>
      Click the button to change the value
      of the flexBasis to "inherit"
    </p>
    <div id="parent">
        <div class="main">
            <div style="background-color:green;">
                Geeks</div>
            <div id="div1" 
                 style="background-color:lightgreen;">
                For</div>
            <div style="background-color:green;">
                Geeks
            </div>
        </div>
    </div>
    <button onclick="changeFlexBasis()">
        Change flexBasis
    </button>
  
    <script>
        function changeFlexBasis() {
            document.querySelector(
                '#div1').style.flexBasis = "inherit";
        }
    </script>
  
</body>
  
</html>

chevron_right


Output:
Before clicking the button:
inherit-before

After clicking the button:
inherit-after

Supported Browsers: The browser supported by flexBasis property are listed below:

  • Google Chrome
  • Internet Explorer 11.0
  • Firefox
  • Opera
  • Apple Safari 6.1


My Personal Notes arrow_drop_up

Maybe this silence is dangerous

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.