Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS grid-auto-flow Property

Posted in CSS Tutorial
Updated on Oct 03, 2024
By Mari Selvan
👁️ 22 - Views
⏳ 4 mins
💬 1 Comment
CSS grid-auto-flow Property

Photo Credit to CodeToFun

🙋 Introduction

The grid-auto-flow property in CSS is used to control how auto-placed items are inserted into the grid. When items are not explicitly positioned, the grid-auto-flow property determines their placement.

This property is particularly useful in creating responsive and dynamic grid layouts.

💡 Syntax

The syntax for the grid-auto-flow property is as follows:

Syntax
Copied
Copy To Clipboard
element {
  grid-auto-flow: value;
}

🎛️ Default Value

The default value of the grid-auto-flow property is row.

🏠 Property Values

ValueDescription
rowPlaces items by filling each row, adding new rows as necessary.
columnPlaces items by filling each column, adding new columns as necessary.
row densePlaces items by filling each row, trying to fill in gaps in earlier rows if possible.
column densePlaces items by filling each column, trying to fill in gaps in earlier columns if possible.

📄 Example

In this example, we'll create a simple grid layout and demonstrate the effect of different grid-auto-flow values.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS grid-auto-flow Example</title>
  <style>
    .container {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-auto-rows: 100px;
      grid-gap: 10px;
    }
    
    .item {
      background-color: lightblue;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 24px;
    }

    .row {
      grid-auto-flow: row;
    }

    .column {
      grid-auto-flow: column;
    }
  </style>
</head>
<body>
  <h1>Grid with grid-auto-flow Property</h1>

  <h2>Auto Flow: Row (Default)</h2>
  <div class="container row">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
  </div>

  <h2>Auto Flow: Column</h2>
  <div class="container column">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
  </div>
</body>
</html>

🖥️ Browser Compatibility

The grid-auto-flow property is supported in most modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. However, it is always a good practice to test your website across different browsers to ensure compatibility.

🎉 Conclusion

The grid-auto-flow property is an essential tool for web developers working with CSS Grid layouts. It provides flexibility in how auto-placed items are arranged within the grid, allowing for more dynamic and responsive designs.

By understanding and utilizing this property, you can create more efficient and visually appealing grid layouts for your web projects.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy