CSS Grid :-
CSS Grid is a layout system in CSS that allows you to create grid-based designs with precision and flexibility. It's like a virtual grid on which you can place and arrange your content in rows and columns. Unlike older layout methods, CSS Grid simplifies the process of creating complex layouts, making it perfect for both beginners and experienced web designers.
There are some keys of CSS Grid:-
.container {
display: grid;
}
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* Three equal columns */
grid-template-rows: 100px 200px; /* Two rows with different heights */
}
.container {
display: grid;
grid-gap: 20px;
}
.item {
grid-column: 2 / 4; /* Starts at column 2, ends at column 4 */
grid-row: 1 / 3; /* Starts at row 1, ends at row 3 */
}
.container {
display: grid;
grid-template-areas:
"header header"
"main sidebar"
"footer footer";
}
.container {
display: grid;
grid-auto-columns: 100px;
grid-auto-rows: auto;
}
领英推荐
.container {
display: grid;
justify-items: center; /* Centers items horizontally */
align-items: center; /* Centers items vertically */
}
.container {
display: grid;
justify-content: center; /* Centers the grid horizontally */
align-content: center; /* Centers the grid vertically */
}
.container {
display: grid;
grid-auto-flow: column; /* Automatically place items in columns */
}
.container {
display: grid;
grid-template: 1fr 1fr / 1fr 2fr; /* Rows / Columns */
}
.container {
display: grid;
grid-template-columns: 1fr 1fr; /* Two equal columns by default */
/* Media query for screens 768px wide and above */
@media (min-width: 768px) {
grid-template-columns: 1fr 1fr 1fr; /* Three equal columns on larger screens */
}
/* Media query for screens 1024px wide and above */
@media (min-width: 1024px) {
grid-template-columns: 1fr 1fr 1fr 1fr; /* Four equal columns on even larger screens */
}
}
In this code, the layout starts with two equal columns by default. However, as the screen width increases, we use media queries to adjust the number of columns to three and then four. This approach ensures that the layout adapts to different screen sizes, making it a responsive grid design.
Conclusion :-
Lastly, while not an exclusive CSS Grid property, media queries play a vital role in responsive design. You can use them to adjust your grid layout based on screen size or other conditions, ensuring that your grid design adapts seamlessly to different devices and screen sizes.
By understanding and applying these CSS Grid properties and considering responsive design through media queries, you can create sophisticated and adaptable web layouts that cater to a variety of devices and screen dimensions. Embracing the power of CSS Grid empowers you to design modern, efficient, and visually appealing web pages.
Link --> Click here for Grid CSS