Welcome to the tenth lecture of the "Basic to Brilliance" course. In this lecture, we’ll delve deeper into advanced CSS Grid techniques. These techniques will enable you to create even more complex and responsive layouts. By the end of this lecture, you’ll be able to work with grid areas, grid auto-placement, and combine CSS Grid with other layout systems like Flexbox.
Grid areas allow you to assign names to sections of your grid, making it easier to manage and visualize your layout.
HTML:
<div class="grid-container"> <header class="header">Header</header> <aside class="sidebar">Sidebar</aside> <main class="content">Main Content</main> <footer class="footer">Footer</footer> </div>
CSS:
.grid-container { display: grid; grid-template-areas: "header header" "sidebar content" "footer footer"; grid-template-columns: 200px 1fr; grid-template-rows: auto 1fr auto; } .header { grid-area: header; } .sidebar { grid-area: sidebar; } .content { grid-area: content; } .footer { grid-area: footer; }
In this example:
CSS Grid has an auto-placement feature that automatically positions grid items when they are not explicitly placed. You can control how this works using grid-auto-flow.
Values:
Example:
.grid-container { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-flow: row dense; /* Auto-places items and packs them densely */ }
In this case, grid items will be placed row by row, and smaller items will fill any gaps.
The minmax() function allows you to define a range for grid tracks, such as specifying the minimum and maximum size a grid track can take.
.grid-container { display: grid; grid-template-columns: repeat(3, minmax(150px, 1fr)); }
In this example:
Both auto-fill and auto-fit are used to create dynamic grids, but they work slightly differently:
Auto-Fit: Shrinks or grows columns to fit the available space.
Example: Auto-fit and auto-fill comparison.
.grid-container { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); /* Auto-fill */ } .grid-container-fit { display: grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); /* Auto-fit */ }
Both options automatically adjust the number of columns based on the available space, but auto-fit will collapse empty columns.
While CSS Grid is great for structuring the overall layout, Flexbox is perfect for controlling alignment within individual items. You can combine both to handle different parts of your layout.
HTML:
<div class="grid-container"> <div class="header">Header</div> <div class="content"> <div class="flexbox-item">Item 1</div> <div class="flexbox-item">Item 2</div> </div> <div class="footer">Footer</div> </div>
CSS:
.grid-container { display: grid; grid-template-columns: 1fr; grid-template-rows: auto 1fr auto; } .content { display: flex; justify-content: space-around; align-items: center; } .flexbox-item { padding: 20px; background-color: #ddd; }
In this example:
Grids can be defined explicitly (using grid-template-columns and grid-template-rows) or implicitly (where new tracks are created automatically).
Implicit Grid: The grid automatically creates rows or columns to accommodate extra items.
Example:
.grid-container { display: grid; grid-template-columns: 100px 100px; grid-auto-rows: 50px; /* Implicitly adds rows */ }
In this case, the grid will automatically create new rows if more items are added than fit in the defined columns.
Let’s use CSS Grid to create a responsive full-page layout with a header, main content area, sidebar, and footer.
HTML:
<div class="grid-container"> <header class="header">Header</header> <nav class="sidebar">Sidebar</nav> <main class="main-content">Main Content</main> <footer class="footer">Footer</footer> </div>
CSS:
.grid-container { display: grid; grid-template-areas: "header header" "sidebar main" "footer footer"; grid-template-columns: 200px 1fr; grid-template-rows: auto 1fr auto; height: 100vh; /* Full height layout */ } .header { grid-area: header; background-color: #333; color: white; padding: 20px; text-align: center; } .sidebar { grid-area: sidebar; background-color: #f4f4f4; padding: 20px; } .main-content { grid-area: main; padding: 20px; } .footer { grid-area: footer; background-color: #333; color: white; padding: 10px; text-align: center; }
In this example:
다음: 다음 강의에서는 CSS 위치 지정과 절대, 상대, 고정과 같은 속성을 사용하여 요소의 위치를 지정하는 방법을 살펴보겠습니다. 레이아웃 제어를 더욱 강화할 준비를 하세요!
리도이 하산
The above is the detailed content of Advanced CSS Grid Techniques. For more information, please follow other related articles on the PHP Chinese website!