Open In App

CSS Flexbox and Its Properties

Before we dive in to CSS Flexbox, let us learn about Pre Flexbox

Pre Flexbox: A recap of some properties in CSS which might be related to learning Flexbox.



What exactly is a flexbox?

Note: Not all browsers support the flexbox properties, so make sure that the browser you are using supports this property.



Basics of Flexbox:
Apply a display type of flexbox to the parent container, this would make all the child elements in it to adjust into the flex type and these would be called the “flex items”. This means they have become more flexible i.e. we can control how much they can shrink and how much they can grow and also the spacing between these elements.

display: flex property:

Axes of flexbox:

              A) left to right

      flex-direction: row

              B) right to left

      flex-direction: row-reverse

              C) top to bottom

        flex-direction: column

              D) bottom to top

        flex-direction: column

Properties of flexbox:

Parent properties:

Children/Flex-items properties:

Example:




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0"
  
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            color: green;
            font-size: 50;
        }
        h2 {
            margin: 10px;
        }
    </style>  
</head>
  
<body>
    <h2>GeeksforGeeks</h2>
</body>
  
</html>

Before applying flex properties:

After applying flex properties: 

Yes, only this much code in CSS can make such a huge difference using flex-box.

Benefits of a flexbox:


Article Tags :