ads
Describe about css.
ads
In this article, I'll show you how to use CSS Grid to create a super cool image grid with varying number of columns based on screen width.
ads
Best of all, responsiveness is added with a single line of CSS.
This means you don't have to clutter your HTML with ugly class names (col-sm-4, col-md-8, etc.) or create media queries for each individual screen size .
I also created a free CSS Grid course. Click here for full access.
ads
Click the picture to jump to the course.
Let's jump in!
Setup
In this article we will continue to use the grid we used in our first CSS Grid article. Then add a photo at the end of the article. The first grid looks like this:
Here is the HTML code:
CSS:
ads
.container {
display: grid;
grid template columns: 100px 100px 100px;
grid template rows: 50px 50px; It contains.
ads
If this code confuses you, I recommend reading the article "Learn CSS Grid in 5 minutes" which explains the basics of layout engines.
Let's start by making the columns responsive.
ads
Basic Responsiveness with Fractional Unit
CSS Grid introduces a brand new value called Fractional Unit.
Fractional units are written fr and can divide the container into any number of decimals.
Let's change each column to have a fractional width.
ads
.container {
Display: Grid;
Grid Template Columns: 1fr 1fr 1fr;
Grid Template Rows: 50px 50px; occupy 1 unit each. The results are as follows.
ads
Changing the value of grid-template-columns to 1fr 2fr 1fr makes the second column twice as wide as the other two columns. The total width is now 4 faction units, the second occupying 2 of them, the other units occupying 1 each.
In other words, fractional values make it very easy to change the width of a column.
ads
Improved responsiveness
However, the above example does not give the desired responsiveness, as this grid will always be 3 columns wide. I would like the number of columns in the grid to vary with the width of the container. To do that, we need to learn three new concepts.
ads
repeat()
Start with the repeat() function. This is a more powerful way to specify columns and rows. Let's take the original grid and modify it with repeat():
ads
.container {
display: grid;
Grid Template Column: Repeat (3, 100px);
Grid Template Row: repeat(2, 50px);
}
So repeat(3, 100px) is the same as 100px 100px 100px. The first parameter is the number of columns or rows you want, the second parameter is their width, and you get exactly the same layout you used the first time:
ads
auto-fit
Then there's autofit.
Let's skip the fixed number of columns and replace the 3 with an auto-adjust.
ads
.container {
display: grid;
grid spacing: 5px;
grid template columns: repeat(auto-fit, 100px);
grid template rows: repeat(2, 100px); Behavior occurs:
ads
The grid varies the number of columns depending on the width of the container.
Attempts to fit as many 100 pixel wide columns as possible in the container.
However, hardcoding every column to be exactly 100 pixels wide doesn't give you the flexibility you need. As you can see in the GIF above, it often leaves white space on the right side of the grid.
ads
minmax()
The last element we need to fix is minmax(). Just replace 100px with minmax(100px, 1fr) . Here is the final CSS.
.container {
display: grid;
grid spacing: 5px;
grid template columns: repeat(auto-fit, minmax(100px, 1fr));
grid template rows: repeat(2, 100px); Note that all responsiveness is done in one line of CSS.
ads
This causes the following behavior:
ads
And as you can see, this works perfectly. The minmax() function defines a size range between min and below max
ads
Column size is now always at least 100 pixels. However, if more space was available, the grid would simply spread evenly across each column, since the columns would be fractional units instead of 100 pixels.
Adding an image
The final step is to add an image. This has nothing to do with CSS Grid, but let's take a look at the code anyway.
ads
First add an image tag to each grid item.
ads
To fit the image inside the object, set it to the same width and height as the object itself, then Object-Fit: use cover; This will cause the image to cover the entire container and the browser will crop the image if necessary.
ads
.container > div > img {
width: 100%;
height: 100%;
suitable for object: cover; That's it! Now that you understand one of the most complicated concepts of CSS grids, give yourself a boost.
ads
Browser Support
Before we leave, we should also mention browser support. At the time of writing, 77% of global website traffic supports CSS Grid and counting.
I think 2018 will be the year of the CSS grid. It's a breakthrough and a must-have skill for front-end developers. Similar to what happened with CSS Flexbox in recent years.
ads
Alternatively, you can view a preview of the course.
ads
Thanks for reading! My name is Per. Co-founder of Scrimba, a tool for creating interactive coding screencasts
ads
In this article, I'll show you how to use CSS Grid to create a super cool image grid with varying number of columns based on screen width.
ads
Best of all, responsiveness is added with a single line of CSS.
This means you don't have to clutter your HTML with ugly class names (col-sm-4, col-md-8, etc.) or create media queries for each individual screen size .
I also created a free CSS Grid course. Click here for full access.
ads
Click the picture to jump to the course.
Let's jump in!
Setup
In this article we will continue to use the grid we used in our first CSS Grid article. Then add a photo at the end of the article. The first grid looks like this:
Here is the HTML code:
CSS:
ads
.container {
display: grid;
grid template columns: 100px 100px 100px;
grid template rows: 50px 50px; It contains.
ads
If this code confuses you, I recommend reading the article "Learn CSS Grid in 5 minutes" which explains the basics of layout engines.
Let's start by making the columns responsive.
ads
Basic Responsiveness with Fractional Unit
CSS Grid introduces a brand new value called Fractional Unit.
Fractional units are written fr and can divide the container into any number of decimals.
Let's change each column to have a fractional width.
ads
.container {
Display: Grid;
Grid Template Columns: 1fr 1fr 1fr;
Grid Template Rows: 50px 50px; occupy 1 unit each. The results are as follows.
ads
Changing the value of grid-template-columns to 1fr 2fr 1fr makes the second column twice as wide as the other two columns. The total width is now 4 faction units, the second occupying 2 of them, the other units occupying 1 each.
In other words, fractional values make it very easy to change the width of a column.
ads
Improved responsiveness
However, the above example does not give the desired responsiveness, as this grid will always be 3 columns wide. I would like the number of columns in the grid to vary with the width of the container. To do that, we need to learn three new concepts.
ads
repeat()
Start with the repeat() function. This is a more powerful way to specify columns and rows. Let's take the original grid and modify it with repeat():
ads
.container {
display: grid;
Grid Template Column: Repeat (3, 100px);
Grid Template Row: repeat(2, 50px);
}
So repeat(3, 100px) is the same as 100px 100px 100px. The first parameter is the number of columns or rows you want, the second parameter is their width, and you get exactly the same layout you used the first time:
ads
auto-fit
Then there's autofit.
Let's skip the fixed number of columns and replace the 3 with an auto-adjust.
ads
.container {
display: grid;
grid spacing: 5px;
grid template columns: repeat(auto-fit, 100px);
grid template rows: repeat(2, 100px); Behavior occurs:
ads
The grid varies the number of columns depending on the width of the container.
Attempts to fit as many 100 pixel wide columns as possible in the container.
However, hardcoding every column to be exactly 100 pixels wide doesn't give you the flexibility you need. As you can see in the GIF above, it often leaves white space on the right side of the grid.
ads
minmax()
The last element we need to fix is minmax(). Just replace 100px with minmax(100px, 1fr) . Here is the final CSS.
.container {
display: grid;
grid spacing: 5px;
grid template columns: repeat(auto-fit, minmax(100px, 1fr));
grid template rows: repeat(2, 100px); Note that all responsiveness is done in one line of CSS.
ads
This causes the following behavior:
ads
And as you can see, this works perfectly. The minmax() function defines a size range between min and below max
ads
Column size is now always at least 100 pixels. However, if more space was available, the grid would simply spread evenly across each column, since the columns would be fractional units instead of 100 pixels.
Adding an image
The final step is to add an image. This has nothing to do with CSS Grid, but let's take a look at the code anyway.
ads
First add an image tag to each grid item.
ads
To fit the image inside the object, set it to the same width and height as the object itself, then Object-Fit: use cover; This will cause the image to cover the entire container and the browser will crop the image if necessary.
ads
.container > div > img {
width: 100%;
height: 100%;
suitable for object: cover; That's it! Now that you understand one of the most complicated concepts of CSS grids, give yourself a boost.
ads
Browser Support
Before we leave, we should also mention browser support. At the time of writing, 77% of global website traffic supports CSS Grid and counting.
I think 2018 will be the year of the CSS grid. It's a breakthrough and a must-have skill for front-end developers. Similar to what happened with CSS Flexbox in recent years.
ads
Alternatively, you can view a preview of the course.
ads
Thanks for reading! My name is Per. Co-founder of Scrimba, a tool for creating interactive coding screencasts
ads
0 Comments