DEV Community

Bharat Singh Rajput
Bharat Singh Rajput

Posted on

Understanding css Flex and Grid

CSS Flexbox and Grid are two powerful tools for creating flexible and responsive layouts in web development. In this article, we'll take a closer look at each of these layout models, including code examples to help you get started.

CSS Flexbox

CSS Flexbox is a one-dimensional layout model that allows you to control the direction, alignment, and size of elements within a container. It's ideal for creating flexible and responsive user interfaces, and is particularly useful for creating layouts with items that have different sizes and need to be aligned in a specific way.

Here's an example of how you might use CSS Flexbox to create a simple layout with items that are aligned vertically and spaced evenly:

Image description

In this example, we set the display property of the parent container to flex to enable Flexbox layout. The flex-direction property is set to column to align the items vertically. And finally, the align-items property is set to center to center the items within the container.

CSS Grid

CSS Grid is a two-dimensional layout model that allows you to create complex grid-based layouts with rows and columns. It's ideal for creating flexible and responsive user interfaces, and is particularly useful for creating complex layouts with multiple items.

Here's an example of how you might use CSS Grid to create a simple layout with three rows and two columns:

Image description

In this example, we set the display property of the parent container to grid to enable Grid layout. The grid-template-columns property is set to 1fr 1fr to create two columns with equal widths. The grid-template-rows property is set to 100px 100px 100px to create three rows with the specified heights. The child elements are then positioned within the grid using the grid-column and grid-row properties.

In conclusion, CSS Flexbox and Grid are both powerful tools for creating flexible and responsive layouts in web development. Whether you're creating a simple layout with a few items, or a complex grid-based layout, these layout models

Top comments (0)