CSS Properties
CSS grid-template-columns Property
Photo Credit to CodeToFun
🙋 Introduction
The grid-template-columns
property in CSS is a fundamental part of the CSS Grid Layout Module. It defines the columns of a grid container, specifying the size and number of columns.
This property allows developers to create complex, responsive layouts with ease.
💡 Syntax
The syntax for the grid-template-columns
property involves defining one or more track sizes, which can be a length, a percentage, a fraction, or the keyword auto.
.container {
grid-template-columns: track-size track-size ...;
}
🎛️ Default Value
The default value of the grid-template-columns
property is none, meaning no grid columns are defined, and the grid container has no explicit columns.
🏠 Property Values
Value | Description |
---|---|
length | Defines a fixed size, such as 100px. |
percentage | Defines a size relative to the grid container, such as 20%. |
auto | Automatically adjusts the column size based on content. |
fr | Defines a fraction of the available space, such as 1fr. |
minmax(min, max) | Defines a size range, with a minimum and maximum value. |
repeat(count, track-size) | Repeats a pattern of columns a specified number of times. |
📄 Example
In this example, we'll create a grid with three columns: the first column is 100px wide, the second column is 1fr (taking up the remaining space), and the third column is 200px wide.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS grid-template-columns Example</title>
<style>
.container {
display: grid;
grid-template-columns: 100px 1fr 200px;
gap: 10px;
}
.item {
background-color: #ddd;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<h1>Grid Template Columns Example</h1>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
</body>
</html>
🖥️ Browser Compatibility
The grid-template-columns
property is widely supported in modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. It's always a good practice to test your grid layout across different browsers to ensure consistent behavior.
🎉 Conclusion
The grid-template-columns
property is an essential tool for web developers working with CSS Grid.
By defining the size and number of columns in a grid container, you can create flexible, responsive layouts that adapt to different screen sizes and content requirements. Experiment with different values and combinations to see how this property can enhance your web designs.
👨💻 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-template-columns Property), please comment here. I will help you immediately.