CSS | flex Property

The flex property in CSS is the combination of flex-grow, flex-shrink, and flex-basis property. It is used to set the length of flexible items. The flex property is much responsive and mobile friendly. It is easy to positioning child elements and the main container. The margin doesn’t collapse with the content margins. Order of any element can be easily changed without editing the HTML section.

Syntax:

flex: flex-grow flex-shrink flex-basis|auto|initial|inherit;

Property Values:



  • flex-grow: A number that specify, how much items will grow relative to the rest of the flexible items.
  • flex-shrink: A number that specify, how much items will shrink relative to the rest of the flexible items.
  • flex-basis: It sets the length of items. Legal values of flex basis are: auto, inherit, or a number followed by %, em, px or any other length unit.
    • Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <!DOCTYPE html>
      <html>
          <head>
              <title>
                  CSS flex Property
              </title>
                
              <style
                  #Geeks {
                      width: 300px;
                      height: 200px;
                      border: 1px solid black;
                      display: flex;
                  }
                  #Geeks div{
                      flex: 1;
                  }
                  .GFG1 {
                      background-color: green;
                  }
                  .GFG2 {
                      background-color: lightgreen;
                  }
                  .GFG3 {
                      background-color: darkgreen;
                  }
              </style>
          </head>
            
          <body>
              <h2>CSS flex Property</h2>
                
              <div id = "Geeks">
                  <div class = "GFG1">
                      Geeksforgeeks
                  </div>
                  <div class = "GFG2">
                      Lite Content
                  </div
                  <div class = "GFG3">
                      Special Content
                  </div>
              </div>
          </body>
      </html>                    

      chevron_right

      
      

      Output:

      Example 2:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <!DOCTYPE html>
      <html>
          <head>
              <title>
                  CSS flex Property
              </title>
                
              <style
                  #Geeks {
                      width: 300px;
                      height: 200px;
                      border: 1px solid black;
                      display: flex;
                  }
                  #Geeks div{
                      flex: 1 0 auto;
                  }
                  .GFG1 {
                      background-color: green;
                  }
                  .GFG2 {
                      background-color: lightgreen;
                  }
                  .GFG3 {
                      background-color: darkgreen;
                  }
              </style>
          </head>
            
          <body>
              <h2>CSS flex Property</h2>
                
              <div id = "Geeks">
                  <div class = "GFG1">
                      Geeksforgeeks
                  </div>
                  <div class = "GFG2">
                      Lite Content
                  </div
                  <div class = "GFG3">
                      Special Content
                  </div>
              </div>
          </body>
      </html>                    

      chevron_right

      
      

      Output:

      Supported Browser: The browser supported by flex property are listed below:

      • Google Chrome 29.0, 21.0 -webkit-
      • Internet Explorer 11.0, 10.0 -ms-
      • Firefox 28.0, 18.0 -moz-
      • Safari 9.0, 6.1 -webkit-
      • Opera 17.0


      My Personal Notes arrow_drop_up

      Check out this Author's contributed articles.

      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.