Ad Code

Responsive Advertisement

How to Create a Basic CSS Grid | VlogKomputer


 

How to Create a Basic CSS Grid

CSS Grid is a powerful layout system in CSS that allows you to create complex grid-based layouts with ease. It provides a way to arrange elements into rows and columns, making it ideal for creating responsive web designs. In this tutorial, we will learn how to create a basic CSS grid.


Step 1: Create HTML Structure

Let's start by setting up the HTML structure for our grid. We'll create a simple grid with three columns and three rows. Here's the HTML:


<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Basic CSS Grid</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="grid-container"> <div class="grid-item">1</div> <div class="grid-item">2</div> <div class="grid-item">3</div> <div class="grid-item">4</div> <div class="grid-item">5</div> <div class="grid-item">6</div> <div class="grid-item">7</div> <div class="grid-item">8</div> <div class="grid-item">9</div> </div> </body> </html>


In this HTML structure, we have a container div with the class "grid-container" and nine child divs with the class "grid-item." These child divs represent the grid items.


Step 2: Create CSS Grid

Now, let's create the CSS grid by defining the grid container's layout. Create a styles.css file and link it in the HTML head as shown in the previous code.


In styles.css, add the following CSS rules to create the grid:


.grid-container { display: grid; grid-template-columns: repeat(3, 1fr); /* Three columns with equal width */ grid-template-rows: repeat(3, 100px); /* Three rows with a fixed height of 100px */ gap: 10px; /* Gap between grid items */ } .grid-item { background-color: #3498db; /* Background color for grid items */ color: #fff; /* Text color */ padding: 20px; /* Padding inside grid items */ text-align: center; /* Center-align text */ }

In this CSS code:


We set the "grid-container" to display: grid; to activate the CSS Grid layout.


grid-template-columns defines the number and width of columns. In this case, we use repeat(3, 1fr), which creates three columns with equal width using the fr unit, which stands for "fraction."


grid-template-rows defines the number and height of rows. We use repeat(3, 100px) to create three rows with a fixed height of 100 pixels.


gap adds a 10-pixel gap between grid items for better spacing.


The "grid-item" class styles the individual grid items. We set background color, text color, padding, and text alignment to make them visually appealing.


Step 3: View the Result

Save your HTML and CSS files, and open the HTML file in a web browser. You should see a basic CSS grid with three columns and three rows, containing numbered grid items.


Feel free to customize the grid by adjusting the number of columns, rows, grid item content, or styles to match your specific layout needs.


Congratulations! You've created a basic CSS grid. You can build upon this foundation to create more complex and responsive grid layouts for your web projects.


Conclusion

CSS Grid is a versatile tool for creating layout structures in web design. In this tutorial, we've covered the basics of creating a simple grid. As you become more familiar with CSS Grid, you can explore advanced features like grid templates, responsive layouts, and grid item placement to take your web design skills to the next level.


Happy coding!

Post a Comment

0 Comments

Close Menu