A not uncommon building style on the web is the masonry construction, used incessantly in image galleries and portfolio web pages to transform films or elements of various sizes.
Traditionally, create a masonry JavaScript libraries required for construction, similar to the more popular Masonry.js by DeSandro and MiniMasonry. However, with CSS Grid, you will now be able to create this construction using herbal CSS.
In this article, we will learn what a CSS Grid masonry construction is and provide some examples that will help you understand how it actually works. Let’s get started.
Start
CSS Grid is a CSS area builder that provides a grid-based framework where you will be able to define rows and columns and place elements within this grid. Masonry construction can be created by combining CSS Grid with certain properties that allow elements to span a couple of rows or columns, creating the masonry effect. Let’s start with a simple example to understand the basics of creating a masonry construction using CSS Grid.
First, we have the HTML now. We have a container with 8 elements. Each product has a defined height and width, so we can see the effect of the masonry more clearly.
// 6 further items...
And the CSS, as follows:
.masonry { display: grid; grid-template-columns: repeat(6, 1fr); grid-template-rows: masonry; hollow: 10px; } // Further stylistic sorts...
THE grid-template-columns
resources define six identical columns, hollow
resources provide space between grid elements and the grid-template-rows: masonry;
resources create masonry construction consistent with the available space throughout the grid container.
You will modify the construction pipeline using the masonry-auto-flow
asset. When set to next
This resource displays elements in order along the grid axis rather than placing them within the application with necessarily the most free space. As confirmed below, elements are placed in order from left to right, from the absolute best to the lowest, regardless of available space.
Making it responsive
One of the most important benefits of using CSS Grid is that it means you can create responsive layouts with ease. You will use media queries to control the construction of the grid consistently with the size of the visual display unit. For example, you will be able to swap the selection of columns within the grid consistently with the width of the visual display unit to ensure that the construction looks great on different devices.
@media (max-width: 1200px) { .masonry { grid-template-columns: repeat(4, 1fr); } } @media (max-width: 640px) { .masonry { grid-template-columns: repeat(2, 1fr); } }
You will also be able to exchange the repeat()
value to use auto-fill
AND minmax()
functions to create a responsive build that regularly adjusts column selection consistent with available space. minmax()
The feature allows you to set a minimum and maximum size for columns, ensuring they don’t get too small or too large.
.masonry { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); grid-template-rows: masonry; hollow: 10px; }
That’s it! You now have a responsive brickwork that scales consistently with the size of the viewport unit. You will experiment with different values ​​for the minmax()
function and the hollow
resources to make the desired construction for your problem.
Browser improvement
It should be noted that CSS Grid with masonry construction remains experimental and is not yet supported in popular browsers.
Navigator | Style | Improve |
---|---|---|
Google Chrome | No longer supported | |
Snap hook | 77-131 | Experimental. It will also be enabled by configuration. |
Safari | Generation Preview | Supported |
Microsoft Edge | No longer supported | |
Opera | No longer supported | |
Safari on iOS | Generation Preview | Supported |
Android Web View | No longer supported | |
Samsung Internet | No longer supported |
As confirmed in the table, you will be able to view the brickwork construction within Safari Era Preview or Firefox.
For many who use Firefox, kind about:config
inside the management bar, look for the configuration identifier and set it to true
The change will take effect immediately, so you can refresh the web page without having to restart your browser.
The browser hardening status may change, so it is a good idea to check the respective browser documentation or hardening pages for the latest updates.
Conclusion
CSS Grid Masonry is an impressive tool that allows you to create complex grid-based layouts with ease. By combining CSS Grid with media queries and other CSS properties, you will be able to create responsive masonry layouts that look great on all devices. While Masonry remains in the experimental stage, it is worth experimenting to see how it can benefit your projects.
Last but not least, here is a demo of CodePen that you can try. Enjoy!
Check out Pen CSS Grid Masonry via HONGKIAT (@hkdc) on CodePen.
Sending local CSS grid to masonry appeared first on Hongkiat.
Source: https://www.hongkiat.com/blog/native-css-masonry-grid/
[ continue ]
wordpress Maintenance Plans | wordpress hosting
Learn more