Creating Layouts with Grid Areas
Grid areas stand out as a feature of CSS Grid that enables you to assign names to sections of your layout grid structure.
When you label sections of your grid, with grid template areas you can easily position items into areas, by name simplifying the layout for the organization.
Code Sample
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Grid Areas</title> <style> .grid-container { display: grid; grid-template-columns: 1fr 3fr; grid-template-rows: auto; grid-template-areas: "header header" "sidebar main" "footer footer"; gap: 10px; } .grid-item.header { grid-area: header; } .grid-item.sidebar { grid-area: sidebar; } .grid-item.main { grid-area: main; } .grid-item.footer { grid-area: footer; } .grid-item { background-color: #8cacea; padding: 20px; text-align: center; color: white; } </style> </head> <body> <div class="grid-container"> <div class="grid-item header">Header</div> <div class="grid-item sidebar">Sidebar</div> <div class="grid-item main">Main Content</div> <div class="grid-item footer">Footer</div> </div> </body> </html> |
Explanation of code
HTML structure
CSS Styling
Creating Layouts with Grid Areas
Grid areas stand out as a feature of CSS Grid that enables you to assign names to sections of your layout grid structure.
When you label sections of your grid, with grid template areas you can easily position items into areas, by name simplifying the layout for the organization.
Code Sample
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Grid Areas</title> <style> .grid-container { display: grid; grid-template-columns: 1fr 3fr; grid-template-rows: auto; grid-template-areas: "header header" "sidebar main" "footer footer"; gap: 10px; } .grid-item.header { grid-area: header; } .grid-item.sidebar { grid-area: sidebar; } .grid-item.main { grid-area: main; } .grid-item.footer { grid-area: footer; } .grid-item { background-color: #8cacea; padding: 20px; text-align: center; color: white; } </style> </head> <body> <div class="grid-container"> <div class="grid-item header">Header</div> <div class="grid-item sidebar">Sidebar</div> <div class="grid-item main">Main Content</div> <div class="grid-item footer">Footer</div> </div> </body> </html> |
Explanation of code
HTML structure
CSS Styling
Copyrights © 2024 letsupdateskills All rights reserved