CSS Grid is a two - dimensional layout model in CSS that allows you to create grid - based layouts by defining rows and columns. It provides a more intuitive and powerful way to position elements on a web page compared to traditional layout methods like floats and tables. With CSS Grid, you can easily control the size, position, and alignment of grid items.
Semantic HTML refers to the use of HTML tags that have a clear and meaningful purpose. For example, <header>
is used for the top - level section of a page, <nav>
for navigation menus, <main>
for the main content of the page, <article>
for self - contained content, <section>
for thematic groupings of content, and <footer>
for the bottom - level section of a page. Using Semantic HTML makes the code more readable for developers and helps search engines understand the structure and content of the page.
To create a CSS Grid, you first need to define a grid container. You can do this by setting the display
property of an HTML element to grid
or inline - grid
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<style>
.grid - container {
display: grid;
}
</style>
</head>
<body>
<div class="grid - container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
</body>
</html>
Grid tracks are the rows and columns of a grid. You can define the size of grid tracks using the grid - template - rows
and grid - template - columns
properties.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<style>
.grid - container {
display: grid;
grid - template - columns: 100px 200px;
grid - template - rows: 50px 100px;
}
</style>
</head>
<body>
<div class="grid - container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
</div>
</body>
</html>
You can place grid items in specific cells or spans of the grid using the grid - row
and grid - column
properties.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<style>
.grid - container {
display: grid;
grid - template - columns: 100px 100px 100px;
grid - template - rows: 100px 100px;
}
.item1 {
grid - row: 1;
grid - column: 1 / 3;
}
</style>
</head>
<body>
<div class="grid - container">
<div class="item1">Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
</div>
</body>
</html>
To create responsive grid layouts, you can use media queries to change the grid layout based on the screen size.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<style>
.grid - container {
display: grid;
grid - template - columns: repeat(3, 1fr);
}
@media (max - width: 768px) {
.grid - container {
grid - template - columns: repeat(2, 1fr);
}
}
@media (max - width: 480px) {
.grid - container {
grid - template - columns: 1fr;
}
}
</style>
</head>
<body>
<div class="grid - container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
</div>
</body>
</html>
You can nest grids inside other grids to create more complex layouts.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<style>
.outer - grid {
display: grid;
grid - template - columns: 1fr 1fr;
}
.inner - grid {
display: grid;
grid - template - columns: repeat(2, 1fr);
}
</style>
</head>
<body>
<div class="outer - grid">
<div>Outer Item 1</div>
<div class="inner - grid">
<div>Inner Item 1</div>
<div>Inner Item 2</div>
</div>
</div>
</body>
</html>
Combining CSS Grid with Semantic HTML is a powerful approach for creating modern, accessible, and SEO - friendly web layouts. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can build complex and visually appealing grid - based layouts with ease. Whether you are a beginner or an experienced developer, these techniques will help you take your web development skills to the next level.