Grid items wont center align despite using align and justify items - html

I created a grid using 6 images and structured it the way how i want it. The only issue im having with my grid is that I cannot center it. It's leaning too far to the left and have a huge gap on the right. I tried using align items: Center and justify content: center but my grid does not move at all. If i were to use padding it will move but it does not translate well when i look at my project from a different device. What can i do to fix this issue? Thanks.
.PophikeMargin{
margin-top: 50px;
margin-bottom: 50px;
text-align: center;
}
.PophikeMargin h1{
margin: 30px;
}
.Hiking-grid-container{
display: grid;
grid-template-columns: repeat(9, 1fr);
grid-template-rows: repeat(4, 1fr);
grid-column-gap: 10px;
grid-row-gap: 10px;
justify-content: center
}
.Hiking-item1 {
grid-area: 1 / 1 / 3 / 2;
height: 400px;
width: 350px;
}
.Hiking-item2 {
grid-area: 3 / 1 / 5 / 2;
height: 400px;
width: 350px;
}
.Hiking-item3 {
grid-area: 1 / 2 / 6 / 4;
height: 800px;
width: 700px;
}
.Hiking-item4 {
grid-area: 1 / 4 / 3 / 6;
height: 400px;
width: 705px;
}
.Hiking-item5 {
grid-area: 3 / 4 / 5 / 5;
height: 400px;
width: 350px;
}
.Hiking-item6 {
grid-area: 3 / 5 / 5 / 6;
height: 400px;
width: 350px;
}
<section class="Popular-Hiking-Trails">
<div class="PophikeMargin">
<h1>Popular Hiking Destinations</h1>
<div class="Hiking-grid-container">
<div class="Hiking-item1">
<img src="Images/****" width="350" height="400">
</div>
<div class="Hiking-item2">
<img src="Images/****" width="350" height="400">
</div>
<div class="Hiking-item3">
<img src="Images/****" width="350" height="400">
</div>
<div class="Hiking-item4">
<img src="Images/****" width="350" height="400">
</div>
<div class="Hiking-item5">
<img src="Images/****" width="350" height="400">
</div>
<div class="Hiking-item6">
<img src="Images/****" width="350" height="400">
</div>
</div>
</div>
</section>

Several points in your code:
you defined a 9 cols and 4 rows grid
you position the item inside with their row-start, col-start, row-end, col-end but not with the good values. If you want to design grid with grid-area check here: http://cssgridbuilder.com/
if you want to center the element use ...-self for the element to center
Here a snippet with 2 cols, 2 rows and images inside with grid-area. If you need dynamic numbering of images, it's more an approach with span to take.
.PophikeMargin {
margin-top: 50px;
margin-bottom: 50px;
text-align: center;
}
.PophikeMargin h1 {
margin: 30px;
}
.Hiking-grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 1fr);
gap: 10px;
}
.Hiking-item1 {
grid-area: 1 / 1 / 2 / 2;
}
.Hiking-item2 {
grid-area: 1 / 2 / 2 / 3;
}
.Hiking-item3 {
grid-area: 1 / 3 / 2 / 4;
}
.Hiking-item4 {
grid-area: 2 / 1 / 3 / 2;
}
.Hiking-item5 {
grid-area: 2 / 2 / 3 / 3;
}
.Hiking-item6 {
grid-area: 2 / 3 / 3 / 4;
}
.Hiking-grid-container>div {
height: auto;
width: 33%;
justify-self: center;
align-self: center;
}
<section class="Popular-Hiking-Trails">
<div class="PophikeMargin">
<h1>Popular Hiking Destinations</h1>
<div class="Hiking-grid-container">
<div class="Hiking-item1">
<img src="https://picsum.photos/id/237/350/400">
</div>
<div class="Hiking-item2">
<img src="https://picsum.photos/id/184/350/400">
</div>
<div class="Hiking-item3">
<img src="https://picsum.photos/id/125/350/400">
</div>
<div class="Hiking-item4">
<img src="https://picsum.photos/id/85/350/400">
</div>
<div class="Hiking-item5">
<img src="https://picsum.photos/id/51/350/400">
</div>
<div class="Hiking-item6">
<img src="https://picsum.photos/id/100/350/400">
</div>
</div>
</div>
</section>

Related

Is it possible to grid bootstrap vertically?

It seems that bootstrap can only produce horizontal grid systems which is making it hard for me to try develop a project. Here's what I am trying to do:
My page is divided into 4 sections, the leftside height of each blocks are different to the height of the rightside blocks.
I want to make the leftside have the same equal height while the right side has a different height each width which I can do when the grid is a horizontal system.
I want box1 and box2 to be the same height (vh-50) and then box3 to have the equal height.
my desired grid layout:
I've tried doing it like this but it doesn't work and look horrible.
#infoBox {
height: 50vh;
width: 60vh;
}
#tracklistBox {
height: 60vh;
width: 40vh;
}
#playBox {
height: 50vh;
}
#episodesBox {
height: 40vh;
}
<body class="d-flex flex-column min-vh-100">
<div class="container-fluid">
<div class="row ">
<div id="infoBox" class="col border">
1 of 2
</div>
<div id="tracklistBox" class="col border">
2 of 2
</div>
</div>
<div class="row">
<div id="playBox" class="col border">
1 of 3
</div>
<div id="episodesBox" class="col border">
2 of 3
</div>
</div>
</div>
</body>
It's not bootstrap but you can use the grid system
.parent {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-template-rows: repeat(5, 1fr);
grid-column-gap: 0px;
grid-row-gap: 0px;
}
.div1 {
grid-area: 1 / 1 / 3 / 3;
background-color: aqua;
}
.div2 {
grid-area: 3 / 1 / 5 / 3;
background-color: yellow;
}
.div3 {
grid-area: 1 / 3 / 4 / 4;
background-color: violet;
}
.div4 {
grid-area: 4 / 3 / 5 / 4;
background-color: greenyellow;
}
<div class="parent">
<div class="div1">Box 1</div>
<div class="div2">Box 2</div>
<div class="div3">Box 3</div>
<div class="div4">Box 4</div>
</div>

Design a layout with one div on 2 columns and 2 rows at the left, and 4 divs on 2 columns and 2 rows at the right, similar to BBC website

I want to design a hero section similar to BBC website. I started working on this using CSS Grid which I thought could get the same design with minimal code.
I have managed to base design but I want item1 to take 50% of the container's width and rest of the items to take 25% of space each so that it looks like the image above. I can span rows but I am not sure how I can span columns correctly, I tried this :
.item1 {
grid-column-end: span 2;
}
But it broke the design and same did happen with:
.item1 {
grid-column: auto / span 2;
}
My code:
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-gap: 10px;
background-color: #2196F3;
padding: 0px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 0px 0;
font-size: 30px;
}
.item1 {
grid-row-end: span 2;
}
.grid-container div img {width:100%; max-width:600px;}
<div class="grid-container">
<div class="item1">
<img src="https://dummyimage.com/600x400/f09c00/fafafa&text=1"/>
</div>
<div class="item2">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=2"/>
</div>
<div class="item3">
<img src="https://dummyimage.com/600x400/5310f0/fafafa&text=3"/>
</div>
<div class="item4">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=4"/>
</div>
<div class="item5">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=5"/>
</div>
</div>
You could do it as below. I simplified the code by removing irrelevant code for the desired layout and added comments.
.grid-container {
background-color: #2196f3;
/* with these 3 lines below, I'm creating a grid of 4 columns and 2 rows*/
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: auto auto;
grid-gap: 10px;
}
.grid-container > div:nth-of-type(1){ /* I grab the first div */
grid-column: 1 / 3; /* I tell him to take 2 columns, 1 -> 3 with 3 excluded */
grid-row: 1 / 3; /* I tell him to take 2 rows, 1 -> 3 with 3 excluded */
}
.grid-container div img {
width: 100%;
height:100%;
object-fit:cover;
}
<div class="grid-container">
<div class="item1">
<img src="https://dummyimage.com/600x400/f09c00/fafafa&text=1" />
</div>
<div class="item2">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=2" />
</div>
<div class="item3">
<img src="https://dummyimage.com/600x400/5310f0/fafafa&text=3" />
</div>
<div class="item4">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=4" />
</div>
<div class="item5">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=5" />
</div>
</div>

How to align grid items vertically using display:grid and fill all vertical empty space? [duplicate]

This question already has answers here:
How can I make a div span multiple rows and columns in a grid?
(3 answers)
Closed 1 year ago.
I use display:grid not flex to display some divs.
Here's what my page looks like:
And here is how I would like it to look:
And here's the code:
.container {
display: grid;
grid-template-columns: 1.5fr 2fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-auto-columns: 1fr;
grid-auto-rows: 1fr;
gap: 40px 40px;
grid-auto-flow: row dense;
justify-items: stretch;
align-items: stretch;
grid-template-areas: "left1 center-div right1" "left1 center-div right2" "left2 center-div right3";
width: 100%;
height: 100%;
}
.left1 {
grid-area: left1;
}
.left2 {
grid-area: left2;
}
.center-div {
grid-area: center-div;
}
.right1 {
grid-area: right1;
}
.right2 {
grid-area: right2;
}
.right3 {
grid-area: right3;
}
<div class="container owl-carousel owl-theme">
<div class="grid-item left1">
left image 1
</div>
<div class="grid-item left2">
left image 2
</div>
<div class="grid-item center-div">
center image
</div>
<div class="grid-item right1">
right image 1
</div>
<div class="grid-item right2">
right image 2
</div>
<div class="grid-item right3">
right image 3
</div>
</div>
In short, I want to bring the divs closer so that they appear as they are in the second photo
Using CSS Grid
Following demo shows how you can achieve the desired layout using just the grid layout.
Its a 6 x 3 grid where items on the left span 3 rows each and the items on the right span 2 rows each. The item in the center spans all 6 rows.
Each grid items is adjusted in its place using the grid-row and grid-column properties.
The trick to achieving this layout is having more rows than columns.
.container {
display: grid;
grid-template-columns: 1.5fr 2fr 1fr;
grid-template-rows: repeat(6, 1fr);
grid-gap: 30px;
height: 100vh;
}
.grid-item {
background: #eee;
width: 100%;
}
.center {
grid-row: 1 / span 6;
grid-column: 2 / span 1;
}
.left1 {
grid-row: 1 / span 3;
grid-column: 1 / span 1;
}
.left2 {
grid-row: 4 / span 3;
grid-column: 1 / span 1;
}
.right1,
.right2,
.right3 {
grid-column: 3 / span 1;
}
.right1 { grid-row: 1 / span 2; }
.right2 { grid-row: 3 / span 2; }
.right3 { grid-row: 5 / span 2; }
<div class="container">
<div class="grid-item left1">left1</div>
<div class="grid-item left2">left2</div>
<div class="grid-item center">center</div>
<div class="grid-item right1">right1</div>
<div class="grid-item right2">right2</div>
<div class="grid-item right3">right3</div>
</div>
Using CSS Grid along with Flexbox
Another option is to have a 1 x 3 grid and make each grid column a flex container.
You will need to change the structure of the HTML if you use this approach.
:root {
--spacing: 30px;
}
.container {
display: grid;
grid-template-columns: 1.5fr 2fr 1fr;
grid-gap: var(--spacing);
height: 100vh;
}
.grid-item {
display: flex;
}
.grid-item div {
background: #eee;
flex-basis: 100%;
}
.col-1,
.col-3 {
flex-direction: column;
justify-content: space-between;
}
.margin-bottom {
margin-bottom: var(--spacing);
}
<div class="container">
<div class="grid-item col-1">
<div class="margin-bottom">left1</div>
<div>left2</div>
</div>
<div class="grid-item col-2">
<div>center</div>
</div>
<div class="grid-item col-3">
<div class="margin-bottom">right1</div>
<div class="margin-bottom">right2</div>
<div>right3</div>
</div>
</div>

4 image panel grid with empty space in the middle (for email template)

I'm trying to recreate this 4 image structure in the way of the image below, however, I am doing this for an email template so the margin minus doesn't work.
generally, I tried to float left the images and replaced the margin minus with:
position: relative; top: -px;
, but that also doesn't work. am I even approaching this in the right way? or is there an easier way of doing this whole thing.
(the email template is done in SendGrid)
You can try with css grid and grid areas:
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
grid-gap: 10px;
justify-content: center;
}
.item {
padding: 1em;
}
.div1 {
grid-area: 1 / 1 / 2 / 3;
border: 1px solid red;
}
.div2 {
grid-area: 2 / 1 / 4 / 2;
border: 1px solid purple;
}
.div3 {
grid-area: 1 / 3 / 3 / 4;
border: 1px solid green;
}
.div4 {
grid-area: 3 / 2 / 4 / 4;
border: 1px solid blue;
}
<div class="item">text</div>
<div class="container">
<div class="div1 item"> 1</div>
<div class="div2 item"> 2</div>
<div class="div3 item"> 3</div>
<div class="div4 item"> 4</div>
</div>

Irregular grid lines on one part of css grid

I've got a really simple grid set up in CSS, but when I look at the grid lines in the inspector there's a strange irregularity at the bottom. I can't understand why this exists when the rest of the grid lines are all regular.
There's no special styling on div-4, it's just the same as the rest. Is it something to do with the margins produced by the h3 tag?
HTML
<div class="left-sidebar-grid">
<div class="div1">
<h3>Div1</h3>
</div>
<div class="div2">
<h3>Div2</h3>
</div>
<div class="div3">
<h3>Div3</h3>
</div>
<div class="div4">
<h3>Div4</h3>
</div>
</div>
CSS
.left-sidebar-grid {
height: 100%;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(8, 1fr);
grid-gap: 16px;
}
.div1 { grid-area: 1 / 1 / 2 / 2; }
.div2 { grid-area: 2 / 1 / 4 / 2; }
.div3 { grid-area: 4 / 1 / 10 / 2; }
.div4 { grid-area: 10 / 1 / 11 / 2; }
You defined only 8 explicit rows and you have placed the div4 starting at line 10 which will create 2 extra rows so you will end up with 10 rows in total where only 8 are sized using the 1fr and 2 will have an auto size: the empty one you see and the one where you placed div4.
To avoid this use grid-auto-rows:1fr instead of your template in order to make sure all the rows are sized the same way:
.left-sidebar-grid {
height: 100%;
display: grid;
grid-template-columns: 1fr;
grid-auto-rows: 1fr;
grid-gap: 16px;
}
.div1 { grid-area: 1 / 1 / 2 / 2; }
.div2 { grid-area: 2 / 1 / 4 / 2; }
.div3 { grid-area: 4 / 1 / 10 / 2; }
.div4 { grid-area: 10 / 1 / 11 / 2; }
<div class="left-sidebar-grid">
<div class="div1">
<h3>Div1</h3>
</div>
<div class="div2">
<h3>Div2</h3>
</div>
<div class="div3">
<h3>Div3</h3>
</div>
<div class="div4">
<h3>Div4</h3>
</div>
</div>
You can also simplify your code like below:
.left-sidebar-grid {
display: grid;
grid-template-columns: 1fr;
grid-auto-rows: 1fr;
grid-gap: 16px;
}
.div1 {
grid-row:span 1;
}
.div2 {
grid-row:span 2;
}
.div3 {
grid-row:span 6;
}
.div4 {
grid-row:span 1;
}
<div class="left-sidebar-grid">
<div class="div1">
<h3>Div1</h3>
</div>
<div class="div2">
<h3>Div2</h3>
</div>
<div class="div3">
<h3>Div3</h3>
</div>
<div class="div4">
<h3>Div4</h3>
</div>
</div>