CSS Properties
CSS grid-auto-rows Property
Photo Credit to CodeToFun
🙋 Introduction
The grid-auto-rows
property in CSS is used to specify the size of rows in a grid container. When you create a grid layout, you may define explicit rows and columns, but any additional rows created by content will use the grid-auto-rows
property to determine their size.
This property is essential for creating flexible and dynamic grid layouts that adapt to varying content sizes.
💡 Syntax
The syntax for the grid-auto-rows
property is straightforward. You can set it using length values, percentage values, or the auto keyword.
.container {
grid-auto-rows: value;
}
🎛️ Default Value
The default value of the grid-auto-rows
property is auto, which means that the size of the row will be determined by the content inside it.
🏠 Property Values
Value | Description |
---|---|
length | Specifies a fixed size for the rows, such as 100px, 2em, etc. |
percentage | Specifies the size of the rows as a percentage of the grid container's size. |
auto | The size of the rows is determined by the content inside them. |
min-content | The size of the rows is determined by the smallest content. |
max-content | The size of the rows is determined by the largest content. |
fr | Represents a fraction of the available space in the grid container. |
📄 Example
In this example, we'll create a grid container with a defined height for auto-generated rows.
<!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-rows Example</title>
<style>
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 100px;
gap: 10px;
}
.item {
background-color: lightblue;
padding: 10px;
text-align: center;
}
</style>
</head>
<body>
<h1>Grid with Custom Auto Rows</h1>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
<div class="item">Item 6</div>
</div>
</body>
</html>
🖥️ Browser Compatibility
The grid-auto-rows
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-rows
property is a powerful tool for web developers looking to create flexible and dynamic grid layouts.
By controlling the size of automatically generated rows, you can ensure that your grid adapts to varying content sizes while maintaining a consistent and visually appealing layout. Experiment with different values to see how this property can enhance the design of your web projects.
👨💻 Join our Community:
Author
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
If you have any doubts regarding this article (CSS grid-auto-rows Property), please comment here. I will help you immediately.