"Mastering CSS Grid: Repeat 3 & 1fr in Template Columns"

Mastering CSS Grid: Understanding `grid-template-columns: repeat(3, 1fr)`

In the realm of web design, CSS Grid has emerged as a powerful tool for creating flexible and responsive layouts. One of its most versatile features is the `grid-template-columns` property, which allows you to define the size and number of columns in your grid. Today, we're going to delve into a specific use case of this property: `grid-template-columns: repeat(3, 1fr)`.

Understanding the Syntax

The `repeat()` function in CSS Grid takes two arguments: the number of times to repeat a pattern, and the pattern itself. In the case of `repeat(3, 1fr)`, we're telling the browser to repeat a pattern of three equal-width columns, where each column takes up an equal fraction of the available space.

The `1fr` unit is a fraction unit, where `fr` stands for 'fraction'. It represents a fraction of the available space in the grid container. In our example, each of the three columns will take up one-third of the available space in the container.

CSS Grid + CSS Multi-Columns = ♥
CSS Grid + CSS Multi-Columns = ♥

Creating Equal-Width Columns

The most straightforward use of `repeat(3, 1fr)` is to create three equal-width columns. This can be particularly useful in responsive designs, as the columns will automatically adjust their width based on the size of the viewport.

Here's a simple example:

```css .container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; } .item { background: lightgray; padding: 20px; } ```

In this example, the `.container` class will have three equal-width columns, with a gap of 10 pixels between each column. The `.item` class represents the content within each column.

the different types of web pages are shown in red and black, with text below them
the different types of web pages are shown in red and black, with text below them

Mixing `fr` with Other Units

One of the strengths of `repeat()` is its flexibility. You're not limited to using just `fr` units. You can mix and match `fr` with other units like `px`, `em`, or even `auto` to create more complex layouts.

For instance, you might want to create a layout with two narrow columns on the left and one wider column on the right. You could achieve this with `grid-template-columns: repeat(2, 1fr) 2fr`. In this case, the first two columns will each take up one-third of the available space, while the third column will take up two-thirds.

Responsiveness with Media Queries

CSS Grid is inherently responsive, but you can also use media queries to adjust your grid layout at different screen sizes. For example, you might want to switch from a three-column layout to a single-column layout on smaller screens.

Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes

Here's how you might do that:

```css .container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; } @media (max-width: 600px) { .container { grid-template-columns: 1fr; } } ```

In this example, the `.container` class will have three equal-width columns on screens wider than 600 pixels, but will switch to a single column layout on screens 600 pixels wide or smaller.

Browser Support

CSS Grid, including the `repeat()` function, is well-supported across modern browsers. However, as with any CSS feature, it's always a good idea to check the caniuse.com website to ensure that your specific use case is supported in the browsers you're targeting.

Conclusion

The `repeat()` function in CSS Grid is a powerful tool for creating flexible and responsive layouts. Whether you're creating a simple three-column layout or a more complex design with mixed units, `repeat()` can help you achieve your goals with ease. By understanding and mastering this feature, you'll be well on your way to becoming a CSS Grid expert.

CSS Grid with grid-template-columns: repeat();
CSS Grid with grid-template-columns: repeat();
How to create an image gallery with CSS Grid
How to create an image gallery with CSS Grid
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
4 Practical Use Cases for CSS Grids in Modern Web Design
4 Practical Use Cases for CSS Grids in Modern Web Design
Introduction to the CSS Grid Layout Module
Introduction to the CSS Grid Layout Module
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
8 CSS Grid Layouts For Divi 5 (Free Download!)
8 CSS Grid Layouts For Divi 5 (Free Download!)
the diagram shows how to use grid templates for creating an image in photoshop
the diagram shows how to use grid templates for creating an image in photoshop
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
X
X
the css flexbox cheat sheet is shown in yellow and red, as well as black
the css flexbox cheat sheet is shown in yellow and red, as well as black
CSS Grid
CSS Grid
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
How To: Use CSS Grid to Mix and Match Design Patterns
How To: Use CSS Grid to Mix and Match Design Patterns
CSS Grid Cheat Sheet Illustrated in 2021🎖️
CSS Grid Cheat Sheet Illustrated in 2021🎖️
css cheat sheet
css cheat sheet
Create A Grid In HTML & CSS (Simple Examples)
Create A Grid In HTML & CSS (Simple Examples)
Responsive CSS Grid: The Ultimate Layout Freedom
Responsive CSS Grid: The Ultimate Layout Freedom
CSS Grid Template Builder
CSS Grid Template Builder
Css responsive columns
Css responsive columns
the css border radius is shown in black and white, with text below it
the css border radius is shown in black and white, with text below it
an image of a computer screen with the words csss grid on it and a black background
an image of a computer screen with the words csss grid on it and a black background
Coding with Parvez
Coding with Parvez