2 boxes across for desktop, and 1 across for mobile.
It's a fluid and simple css grid layout.
Here is another CSS Grid example, with boxes that flow neatly 2 up for desktop, and 1 across for mobile.
HTML:
<div class="grid-container-10">
<div class="grid-item-10">Box 1</div>
<div class="grid-item-10">Box 2</div>
<div class="grid-item-10">Box 3</div>
<div class="grid-item-10">Box 4</div>
<div class="grid-item-10">Box 5</div>
</div>
CSS:
<style>
.grid-container-10 {
display: grid;
grid-template-columns: repeat(2, 1fr); /* 2 columns, each taking 50% */
gap: 1rem; /* Adjusts the space between boxes */
}
.grid-item-10 {
background-color: #f0f0f0; /* Optional styling for each item */
padding: 1rem; /* Optional padding */
}
/* Responsive behavior for smaller screens */
@media (max-width: 768px) {
.grid-container-10 {
grid-template-columns: 1fr; /* Stacks items vertically on smaller screens */
}
}
</style>
2-column grid layout, where the first column is 20% width and the second column is 80%.
It's a fluid and simple layout.
This simple example is a testament to the usefulness of CSS Grid, and the simplicity of using it in css.
HTML:
<div class="grid_container1">
<div class="grid_column1">Column 1 (20%)</div>
<div class="grid_column2">Column 2 (80%)</div>
</div>
CSS:
<style>
.grid_container1 {
display: grid;
grid-template-columns: 20% 80%;
gap: 0rem;
width: 100%;
height:50vh;
}
.grid_column1 {
background-color: #f0f0f0;
}
.grid_column2 {
background-color: #e0e0e0;
}
</style>
How is Grid helpful in css responsive layouts?
Two-Dimensional Layout: CSS Grid is designed for both rows and columns, making it suitable for creating complex layouts that require alignment along both axes. It's great for grid-like structures and entire page layouts.
Grid Control: Grid provides more control over row and column sizing, enabling you to define explicit sizes, fixed and flexible tracks, and content alignment. This is especially useful for layouts that demand precise control.
Responsive by Default: Grid has built-in support for creating responsive designs with media queries. You can easily change the layout of items based on different screen sizes without altering the HTML structure.
Grid Template Areas: Grid allows you to define layout areas with grid-template-areas, which can be helpful for creating visually complex layouts with different content sections.
Implicit Grids: CSS Grid can automatically create rows and columns based on the content you have, allowing you to build layouts without explicitly defining the number of rows or columns.
In summary, CSS Grid is powerful for creating complex two-dimensional layouts, especially for entire page structures.
In this CSS Grid example, we leverage the power of grid-based layout to create a dynamic and versatile structure. By establishing a grid container with the class .grid-container, we create a two-dimensional grid that accommodates both rows and columns. Grid cells are defined with the class .grid-cell, allowing content to be seamlessly arranged in a way that maximizes both alignment and responsiveness.
The ability to specify explicit column and row sizes using the grid-template-columns and grid-template-rows properties offers precise control over the layout's appearance. Furthermore, utilizing the grid-gap property for spacing enhances the readability and aesthetics of the design.
This approach empowers designers to create complex layouts with ease, such as magazine-style layouts, image galleries, and intricate dashboard designs. With CSS Grid, designers have the flexibility to achieve a structured, yet adaptable, layout that optimizes content presentation across various screen sizes and devices.
HTML:
<div class="grid-container">
<div class="grid-cell">Cell 1</div>
<div class="grid-cell">Cell 2</div>
<div class="grid-cell">Cell 3</div>
<div class="grid-cell">Cell 4</div>
<div class="grid-cell">Cell 5</div>
<div class="grid-cell">Cell 6</div>
</div><br>
CSS:
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 columns */
grid-template-rows: auto; /* Automatic row sizing */
grid-gap: 20px; /* Gap between cells */
padding: 20px;
background-color: #f1f1f1;
}
.grid-cell {
background-color: #fff;
padding: 20px;
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
}
</style>