How to make sidebar and sections with CSS grid? - html

I'm trying to make a layout like this:
Here's my code:
.grid{
display: grid;
background: gray;
grid-gap: 15px;
justify-content: center
align-items: center;
grid-template-columns: repeat(2, auto);
grid-auto-rows: minmax(50px, auto);
}
.sidebar{
grid-column: 1/2;
background-color: white;
align-items: center;
justify-content: center;
width: 300px;
}
.navbar{
grid-column: 2/3;
background-color: white;
height: 50px;
}
.section2{
grid-column: 2/3;
background-color: white;
height: 300px;
}
.section4{
grid-column: 2/3;
background-color: white;
height: 300px;
}
.section3{
grid-column: 1/2;
background-color: white;
height: 200px;
align-items: center;
width: 300px;
}
.section5{
grid-column: 1/2;
background-color: white;
height: 100px;
align-items: center;
width: 300px;
}
<div class="grid">
<div class="sidebar">sidebar</div>
<div class="navbar">navbar</div>
<div class="section2">section2</div>
<div class="section3">section3</div>
<div class="section4">section4</div>
<div class="section5">section5</div>
</div>
The output is shown here:
I highlighted in red some big problems, I have these massive gaps in between sections, and my columns aren't being centered in the grid I tried adding "justify-content: center" and "align-items: center" but none of those centered the sidebar sections and I have no clue how to reduce the gap in between the sections. How can I fix my code so it looks more like the layout in my mockup?

It's complicated because with CSS Grid you have a grid...
For your layout you must divide them on two sections like this:
<div class="grid">
<div class="left">
<aside class="sidebar">sidebar</aside>
<section class="section2">section2</section>
<section class="section4">section4</section>
</div>
<div class="right">
<nav class="navbar">navbar</nav>
<section class="section3">section3</section>
<section class="section5">section5</section>
</div>
</div>
https://codepen.io/tasmim-concept/pen/mdrxLOR

Related

Grid content doesn't match the grid layout

Basically, I'm building a grid in HTML/CSS using flexbox and grids to get the desired layout.
I've been working on it in VSCode and I've managed to make it look the way I desire (see img below).
my issue comes when this get rendered inside WP, for some reason the boxes do not expand to fit the size of the grid layout creating some weird gaps in the content. (see below)
I used the grid overlay and I noticed that the grid is the right size, is just that the box is not expanding properly.
I'm not really sure what the issue is here, I have tried adjusting the layout, the padding, etc.
HTML
main{
width: 100vw;
display: flex;
justify-content: space-around
}
.grid-section{
width: 100%;
max-width: 960px;
height: 100vh;
max-height: 250px;
background: #313131;
color: rgb(211, 168, 168);
font-size: 15px;
overflow:hidden;
border-radius: 10px;
display: flex;
justify-content: space-around
}
#grid{
display: grid;
grid-template-columns: repeat(32, 1fr);
grid-template-rows: 12% 44% 44% 0%
}
.box {
display: flex;
flex-basis: 100%;
}
/*top*/
.box:nth-child(1){
background: #9dcbe9;
grid-column: 1/33;
grid-row: 1/2;
padding: 5px;
}
/*left*/
.box:nth-child(2){
background: #eed875;
grid-column: 1/6;
grid-row: 2/4;
}
/*right*/
.box:nth-child(3){
background: #c97eec;
grid-column:28/33;
grid-row: 2/4;
}
/*middle long*/
.box:nth-child(4){
background: #9de99d;
grid-column: 6/28;
grid-row: 2/3;
}
/*mini boxes*/
.box:nth-child(5){
background: #ffa569;
grid-column: 6/9;
grid-row: 3/4;
}
.box:nth-child(6){
background: #584905;
grid-column: 9/14;
grid-row: 3/4;
}
.box:nth-child(7){
background: #560b79;
grid-column: 14/17;
grid-row: 3/4;
}
.box:nth-child(8){
background: #00ff00;
grid-column: 17/20;
grid-row: 3/4;
}
.box:nth-child(9){
background: #0a9ed8;
grid-column: 20/23;
grid-row: 3/4;
}
.box:nth-child(10){
background: #5802e2;
grid-column: 23/28;
grid-row: 3/4;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="stylesheet.css">
</head>
<title> flexBox testing</title>
<body>
<main>
<div id="grid" class="grid-section">
<div class="box">
<div class="box-content">n1</div>
</div>
<div class="box">
<div class="box-content">n2</div>
</div>
<div class="box">
<div class="box-content">n3</div>
</div>
<div class="box">
<div class="box-content">n4</div>
</div>
<div class="box">
<div class="box-content">n5</div>
</div>
<div class="box">
<div class="box-content">n6</div>
</div>
<div class="box">
<div class="box-content">n7</div>
</div>
<div class="box">
<div class="box-content">n8</div>
</div>
<div class="box">
<div class="box-content">n9</div>
</div>
<div class="box">
<div class="box-content">n10</div>
</div>
</div>
</main>
</body>
Thanks in advance
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-column-gap: 0.5rem;
grid-row-gap: 0.5rem;
padding: 10px;
}
.grid-item {
background-color: #999;
border: 1px solid #fff;
padding: 20px;
font-size: 30px;
text-align: center;
}
</style>
</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>

Why are my grid cells different sizes despite having them span the same distance?

So I'm trying to emulate this website for practice: [website I'm copying][1] [1]: https://i.stack.imgur.com/d7iP2.jpg
Despite assigning the same exact grid columns/rows distances, "Tacos" and "Kombucha" are different sizes from each other. "Tacos" is bigger than "Kombucha" for some reason, and there's this huge gap between them and the image.
Here's what my attempt looks like [my attempt][1][1]: https://i.stack.imgur.com/N74Qz.jpg
Anyone know what is causing this? I want them to be the same size and for the huge gap to go.
Thanks in advance!
Here's my HTML code:
<body>
<div id="grid-container1">
<div id="grid1_item1">
<img src="pic3.jpg" style="height: 400px; width: 800px" />
</div>
<!---->
<div id="grid1_item2">
<h1>$1.99</h1>
Tacos
</div>
<!---->
<div id="grid1_item3">
<h1>$3.99</h1>
Kombucha
</div>
</div>
</body>
CSS :
#grid-container1 {
display: grid;
grid-template-columns: 200px 100px 100px;
grid-template-rows: 200px 50px 50px;
margin-top: 30px;
margin-left: 27px;
grid-column-gap: 0px;
grid-row-gap: 10px;
}
#grid1_item1 {
grid-column: 1/8;
grid-row: 1/5;
}
#grid1_item2 {
grid-column: 8/10;
grid-row: 1/3;
text-align: center;
background-color: #331a00;
opacity: 0.9;
color: #e6e6e6;
border: 2px solid black;
}
#grid1_item3 {
grid-column: 8/10;
grid-row: 3/5;
text-align: center;
background-color: #331a00;
opacity: 0.9;
color: #e6e6e6;
border: 2px solid black;
}
So the issue here is that you are specifying grid-template-rows. Your specified template rows are:
grid-template-rows: 200px 50px 50px;
If you remove this then it will automatically assume that all blocks should be equally sized.
I've also added a class of grid1_item so you can style thing at the same time: https://codepen.io/jeffteachestheweb/pen/PopONgK
.grid1_item {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}

How can a block go to the middle of a grid?

My code has a grid inside a grid on the css, the grid(that is inside) needs to be at the horizontal center of the grid in which it is contained. The problem here is that this block(grid) appears on the upper left side of the grid when it should be at the middle part. How can I achieve this process?
.wrapper {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-rows: auto 1fr;
grid-gap: 5px;
height: 100vh;
padding: 5px;
}
.wrapper .user-profile {
padding: 40px;
grid-row-start: 2;
grid-row-end: 4;
grid-column-start: 2;
grid-column-end: 3;
width: 100%;
background: red;
overflow: auto;
font-family: sans-serif;
letter-spacing: 1.5px;
border-radius: 20px;
}
.user-header {
justify-content: center;
align-self: center;
}
.user-header .grid-header {
width: 46%;
display: grid;
margin: auto;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 50% 20% 80%;
grid-gap: .2rem;
grid-auto-flow: row;
}
.grid-header-item {
display: flex;
justify-content: center;
align-items: center;
}
.grid-header-items:nth-child(10) {
grid-column: 1 / span 2;
grid-row: 4;
width: 100%;
}
<div class="wrapper">
<div class="user-profile">
<div class="user-header">
<div class="grid-header">
<div class="grid-header-item">Hey</div>
<div class="grid-header-item">Hey</div>
<div class="grid-header-item">Hey</div>
<div class="grid-header-item">Hey</div>
<div class="grid-header-item">Hey</div>
<div class="grid-header-item">Hey</div>
<div class="grid-header-item">Hey</div>
<div class="grid-header-item">Hey</div>
<div class="grid-header-item">Hey</div>
<div class="grid-header-item">Hey</div>
<div class="grid-header-item">Hey</div>
</div>
</div>
</div>
</div>
If you have any questions please let me know in the comments below;)

How can I center a text in grid?

A specific text can't be put in the center in CSS grid.
I have a grid CSS but the text "Post Manager" seems to not go to center. I have made grid items into flex container but only this text seem to not follow the rule. Any thought is appreciated. Thank you.
body {
font-family: sans-serif;
}
.wrapper {
display: grid;
grid-template-columns: repeat(15, 1fr);
grid-template-rows: repeat(15, 1fr);
width: 100vw;
height: 100vh;
}
.gn {
background-color: #7fd2f0;
grid-row: 1 / 16;
writing-mode: vertical-lr;
text-orientation: mixed;
transform: rotateZ(180deg);
}
.ab {
background-color: #85fedd;
grid-column: 2/16;
}
.pm {
background-color: #f7f7f7;
grid-row: 2/16;
grid-column: 2/5;
text-align: center;
}
.wp {
background-color: #c6c6c6;
grid-column: 6/16;
grid-row: 2/16;
}
.center-text {
display: flex;
justify-content: center;
align-items: center;
}
<div class="wrapper">
<div class="center-text gn">Global Navigation</div>
<div class="center-text ab">Application Bar</div>
<div class="center-text pm">Post Manager</div>
<div class="center-text wp">Writing Space</div>
</div>
try this.
.pm {
grid-column: 2/6;
}

Css-Grid: Trouble defining grid rules

Using CSS Grid, I'm trying to create a layout that looks like this:
How do I place the bottom (smaller) elements beyond the first row without using nested elements? The number of smaller elements can be anything. I tried using grid-auto-columns: 2fr; to accomplish my task, but it does not work.
Here's what I've tried so far: Codepen
main {
display: grid;
padding: 10px 0 10px 10px;
grid-template: 1fr/repeat(6, 1fr);
grid-template-areas: "a a a b b b";
grid-auto-columns: 2fr;
grid-column-gap: 10px;
grid-row-gap: 10px;
}
main .a {
grid-area: a;
}
main .b {
grid-area: b;
}
.cat {
height: 150px;
background-size: cover;
background-position: center;
overflow: hidden;
position: relative;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
font-size: 20px;
background-color: green;
color: #fff;
}
.cat:hover {
background-color: #004800;
}
<main>
<div class="cat a"></div>
<div class="cat b"></div>
<div class="cat c"></div>
<div class="cat d"></div>
<div class="cat e"></div>
<div class="cat f"></div>
<div class="cat g"></div>
<div class="cat h"></div>
<div class="cat i"></div>
</main>
The first thing I did is define how many columns the grid should be. Looking at your image, it made sense to me to go with 12 columns.
In the main element, I add the rule:
grid-template-columns: repeat(12, 1fr);
In your .cat rules, I added a few things. By default, each block takes up 4/12 columns.
grid-column: span 4;
The last part was dealing with the first two .cat blocks, a and b. No problem. They each take up 6/12 columns.
&.a,
&.b {
grid-column: span 6;
}
And that's it. Here's a working demo.
main {
display: grid;
padding: 10px 0 10px 10px;
grid-template-columns: repeat(12, 1fr);
grid-column-gap: 10px;
grid-row-gap: 10px;
}
.cat {
height: 150px;
background-size: cover;
background-position: center;
overflow: hidden;
position: relative;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
font-size: 20px;
background-color: green;
color: #fff;
grid-column: span 4;
}
.cat:hover {
background-color: #004800;
}
.cat.a,
.cat.b {
grid-column: span 6;
}
<main>
<div class="cat a"></div>
<div class="cat b"></div>
<div class="cat c"></div>
<div class="cat d"></div>
<div class="cat e"></div>
<div class="cat f"></div>
<div class="cat g"></div>
<div class="cat h"></div>
<div class="cat i"></div>
</main>