Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS grid-template-columns Property

Posted in CSS Tutorial
Updated on Oct 03, 2024
By Mari Selvan
👁️ 28 - Views
⏳ 4 mins
💬 1 Comment
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.

Syntax
Copied
Copy To Clipboard
.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

ValueDescription
lengthDefines a fixed size, such as 100px.
percentageDefines a size relative to the grid container, such as 20%.
autoAutomatically adjusts the column size based on content.
frDefines 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.

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-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:

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