I’m working on building an admin dashboard, but I can’t seem to get the hang of Gridbox. Currently there is a big gap between the header and main div
. I’ve tried reducing the gap in the css, but for some reason nothing I do is working.
html {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
a{
text-decoration: none;
color: black;
}
a:hover {
color: orange;
transition: 0.3s;
}
.container {
display: grid;
grid-template-columns: auto 1fr;
grid-template-rows: auto auto;
gap: 10px 10px;
}
.sidebar {
grid-column: 1;
height: 100vh;
border: solid black;
}
.header {
grid-column: 2;
border: 1px solid black;
height: 20vh;
}
.main {
grid-column: 2;
grid-row: 2;
margin-top: 0;
border: solid black;
}
.navbar {
list-style: none;
}
.projects {
display: grid;
grid-template-columns: repeat(3, minmax(100px, 200px));
grid-template-rows: auto;
gap: 5px;
}
.project-item {
border-radius: 10px;
text-align: center;
width: auto;
}
.project-item:hover {
background-color: gray;
color: white;
transition: 0.3s;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Admin Dashboard</title>
</head>
<body>
<div class="container">
<div class="sidebar">
<h1>Admin Dashboard</h1>
<ul class="navbar">
<li class="nav-item"><a href="#">Home</a></li>
<li class="nav-item"><a href="#">Profile</a></li>
<li class="nav-item"><a href="#">Messages</a></li>
<li class="nav-item"><a href="#">History</a></li>
<li class="nav-item"><a href="#">Tasks</a></li>
<li class="nav-item"><a href="#">Communities</a></li>
</ul>
<ul class="navbar">
<li class="nav-item"><a href="#">Settings</a></li>
<li class="nav-item"><a href="#">Support</a></li>
<li class="nav-item"><a href="#">Privacy</a></li>
</ul>
</div>
<div class="header">
Hi there,
<h2>John Smith(@Smithjohn)</h2>
</div>
<div class="main">
<div class="projects">
<div class="project-item">1</div>
<div class="project-item">2</div>
<div class="project-item">3</div>
<div class="project-item">4</div>
<div class="project-item">5</div>
<div class="project-item">6</div>
</div>
</div>
</div>
</body>
</html>
Any ideas as to how I could go about fixing this? Also if anyone knows any good guides to Gridbox, that would also be highly appreciated.
4
Answers
You need your sidebar to span both rows. Add the following declaration to your sidebar style rule:
I would personally not recommend this,but you can use negative values for the border or padding,
try
if that doesn’t work, you can give the same for padding
You can use
grid-row: 1/4;
in.sidebar
. It will fix the layout.For more you can refer : CSS GRID
When you think about gridbox using grid-column and grid-row, you often need to specify the start and end coordinates. look at the following illustration:
so if a div is meant to extend from grid column and grid row coordinates (1,1) to (3,2) respectively, it’d better be
grid-column: 1/3
andgrid-row: 1 / 2
. the div would look like thisIf you understand this, everything should be easy. I myself rarely use
grid-column
andgrid-row
because usinggrid-template-areas
is much easier.As for your problem, here’s my solution with comment