Open In App

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:

object.style.flexBasis
object.style.flexBasis = "number | auto | initial | inherit"

Return Values: It returns a string value, which represents the flex-basis property of an element.



Property Values:

Example-1: Using the number value. 




<!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>

Output: 

Before clicking the button:

  

After clicking the button: 

 

Example-2: Using the auto value. 




<!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>

Output: 

Before clicking the button:

  

After clicking the button:

  

Example-3: Using the initial value. 




<!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>

Output: 

Before clicking the button:

  

After clicking the button:

  

Example-4: Using the inherit value. 




<!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>

Output: 

Before clicking the button:

  

After clicking the button:

  

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


Article Tags :