CSS flex-basis Property

You are Here:

Demo

CSS 'flex-basis' property belongs to child element and not to the parent element. In this demo, we are targeting 'Div 2' child element.

CSS flex-basis Property

CSS flex-basis property sets the initial main size of a flex item.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> #parent{ display: flex; width: 100%; border: 1px solid #8c8c8c; width: 500px; } #parent > div{ border: 1px solid #267326; padding: 25px 0; text-align: center; flex-basis: 100px; -webkit-flex-basis: 100px; } #parent > div:nth-child(2){ flex-basis: 200px; -webkit-flex-basis: 200px; } </style> </head> <body> <h1>CSS flex-basis Property</h1> <div id="parent"> <div>Div 1</div> <div>Div 2</div> <div>Div 3</div> </div> </body> </html>

Syntax

Using CSS

element{ flex-basis: 300px; }

Using Javascript

object.style.flexBasis="300px";

Animatable

Yes, flex-basis property is animatable. CSS Animatable Properties Reference.

Default Value

Default value for CSS flex-basis property is auto.

Property Value

The following table provides a list of values for CSS flex-basis property.

ValueExplanation
autoThe length is equal to the length of the flexible item.
lengthAllows you to define the size of the font in any CSS length unit.

Using JavaScript

In the following example, we will demonstrate how to change the CSS flex-basis property of an element using JavaScript.

Example

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <head> <style> #parent{ display: flex; width: 100%; border: 1px solid #8c8c8c; width: 500px; } #parent > div{ border: 1px solid #267326; padding: 25px 0; text-align: center; flex-basis: 100px; -webkit-flex-basis: 100px; } </style> </head> <body> <h1>CSS flex-basis Property</h1> <div id="parent"> <div>Div 1</div> <div>Div 2</div> <div>Div 3</div> </div> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementById("parent").firstElementChild; function myFunction(){ x.style.flexBasis = "200px"; x.style.WebkitFlexBasis = "200px"; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in CSS.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author