Place all items in the center of the screen using CSS Grid - css

So basically what i want to do is to have 9 Boxes in the middle of the screen.
This is what i tried:
main {
border: 4px solid black;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
width: 100vw;
height: 100vh;
align-items: center;
justify-items: center;
}
.box1 {
background: red;
width: 50%;
}
.box2 {
background: blue;
width: 50%;
}
.box3 {
background: green;
width: 50%;
}
<main>
<div class="box1">
<h1>Box 1</h1>
</div>
<div class="box2">
<h1>Box 2</h1>
</div>
<div class="box3">
<h1>Box 3</h1>
</div>
<div class="box1">
<h1>Box 4</h1>
</div>
<div class="box2">
<h1>Box 5</h1>
</div>
<div class="box3">
<h1>Box 6</h1>
</div>
<div class="box1">
<h1>Box 7</h1>
</div>
<div class="box2">
<h1>Box 8</h1>
</div>
<div class="box3">
<h1>Box 9</h1>
</div>
</main>
This almost does what i want.
So what i get is this:
-b-b-b-
-b-b-b-
-b-b-b-
with - being free space.
What i want is this:
-bbb-
-bbb-
-bbb-
so that the 9 boxes actually touch each other.
Is there a way to do that?
(i want to hold on to the grid-template-columns: 1fr 1fr 1fr and grid-template-rows: 1fr 1fr 1fr)
tyvm :)

Is there a way to do that? (i want to hold on to the grid-template-columns: 1fr 1fr 1fr and grid-template-rows: 1fr 1fr 1fr)
No. There is no way to do this.
Because you've set your columns to 1fr each, they will spread across the width of the container in equal lengths. Therefore, the columns are not centered.
You would have to do something like this: grid-template-columns: 1fr auto auto auto 1fr, with the 1fr columns having no content and used solely for spacing. This set up would pin the three inner columns to the center.

Related

css grid fit rows heights

I want to remove the worthless margin between rows, so I want every div takes the content height without giving margin to his side div, I tried everything but nothing works.
.grids {
width: 90%;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: minmax(min-content, max-content);
margin: auto;
grid-gap: 32px;
}
.grid {
position: relative;
width: 95%;
height: max-content;
margin: 10px;
padding: 20px;
background: black;
border-radius: 10px;
}
<div class="grids">
<div class="grid"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="grid"></div>
</div>
Edit: To make a masonry layout I have wrapped grid items in div tag
so you can nest as many tags as you want.
grid items overflow the content because of the width and height properties.
you're using a grid gap for both rows and columns.
So I guess this might help you out.
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
grid-template-rows: masonry;
grid-gap: 10px;
}
.grid-item {
padding: 20px;
background: red;
border-radius: 10px;
margin-bottom: 24px;
}
<div class="grid">
<div>
<div class="grid-item">
<h1>Hello world</h1>
</div>
<div class="grid-item">
<h1>Hello world</h1>
</div>
</div>
<div>
<div class="grid-item"></div>
<div class="grid-item"></div>
<div class="grid-item"></div>
</div>
<div>
<div class="grid-item">
<h1>Hello
<br>
friend
</h1>
</div>
<div class="grid-item"></div>
</div>
</div>
Also, I renamed the classes for naming purposes only.
MDN docs grid-row: row-gap
MDN docs masonry layout: masonry layout
You can try to set grid-gap: 32px to grid-gap: 0 32px, it will remove the margin between grid rows;

Separate line text in CSS grid

I want to make a grid in footer where the text is above the grid but also staying in the grid. Position: absolute don't work because it effects both grid and text, and i want to move them independently of each other.
How it looks:
https://i.imgur.com/norbzp1.png
How i want it to be:
https://i.imgur.com/1fYoQIF.png
Code:
<div class="footer">Footer
<div class="icon"></div>
<div class="icon"></div>
<div class="icon"></div>
<div class="icon"></div>
<div class="icon"></div>
<div class="icon"></div>
</div>
And css:
.footer{
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-auto-rows: 50px;
grid-gap: 10px;
}
.icon{
display: flex;
background: rgb(160, 84, 84);
}
You can achieve this by wrapping the grid and the footer with a container.
.footer-header {
text-align: center;
padding:20px;
}
.footer-grid {
grid-column: span 3 / auto;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-auto-rows: 50px;
grid-gap: 10px;
}
.icon{
display: flex;
background: rgb(160, 84, 84);
}
<div class="footer">
<div class="footer-header">
Footer
</div>
<div class="footer-grid">
<div class="icon">1</div>
<div class="icon">2</div>
<div class="icon">3</div>
<div class="icon">4</div>
<div class="icon">5</div>
<div class="icon">6</div>
</div>
</div>
Also you can use grid-template-areas, for example
.footer{
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-areas: "footer-head footer-head footer-head";
grid-auto-rows: 50px;
grid-gap: 10px;
}
.icon{
display: flex;
background: rgb(160, 84, 84);
}
.footer-head {
text-align: center;
grid-area: footer-head;
}
<div class="footer">
<div class="footer-head">Footer</div>
<div id="icon1" class="icon"></div>
<div class="icon"></div>
<div class="icon"></div>
<div class="icon"></div>
<div class="icon"></div>
<div class="icon"></div>
</div>

Bootstrap Column Width Ratios

The Bootstrap grid system is of course 12 columns.
I want to have and use all the 12 columns across the page.
But I want the first 6 left-hand columns to be half the width (i.e 50%) of the right-hand columns width.
So that the right 6 column will always be double width of the ones on the left.
This width ratio should remain across changing page/window resizing.
How can I do that? Thanks.
This task is not for a boostrap grid system. 12 parts mean 12 equal width parts. And you want not equal 12 block. This is a task for display: grid;.
grid-template-columns: repeat(6, 2fr) repeat(6, 1fr); this part means that we take 6 times for 2fr and then 6 times for 1fr. fr is a new measure for grid. It means all grid is devides on a sum all frs in one row. So 2fr is a block twice bigger then 1fr. In our case we have 2fr 2fr 2fr 2fr 2fr 2fr 1fr 1fr 1fr 1fr 1fr 1fr = 18 parts in a row. It's just for you to know hove does fr works.
Links for some CSS Grid documentation:
Basic Concepts of grid layout
grid-template-columns
.grid-row {
display: grid;
grid-template-columns: repeat(6, 2fr) repeat(6, 1fr);
grid-auto-rows: auto;
grid-row-gap: .5rem;
grid-column-gap: .5rem;
}
.grid-col {
background: #000;
min-height: 50px;
}
/* example of adaptivity for smaller screens */
#media (max-width: 500px) {
.grid-row {
grid-template-columns: repeat(3, 2fr) repeat(3, 1fr);
}
}
<div class="grid-row">
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
</div>
Not sure, but if you want not twice, but 1.5 times wider cols, use next one. Updated only grid-template-columns: repeat(6, 3fr) repeat(6, 2fr);
.grid-row {
display: grid;
grid-template-columns: repeat(6, 3fr) repeat(6, 2fr);
grid-auto-rows: auto;
grid-row-gap: .5rem;
grid-column-gap: .5rem;
}
.grid-col {
background: #000;
min-height: 50px;
}
/* example of adaptivity for smaller screens */
#media (max-width: 500px) {
.grid-row {
grid-template-columns: repeat(3, 3fr) repeat(3, 2fr);
}
}
<div class="grid-row">
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
<div class="grid-col"></div>
</div>

IE11 Grid Template

Iam working on a simple layout. It works on edge and co, but in IE11 it places anything in the first row.
<body>
<div class="sidebar1">
<nav>
<ul>
</ul>
</nav>
</div>
<article class="content">
</article>
</body>
body {
display: -ms-grid;
display: grid;
-ms-grid-columns: 250px 1fr;
-ms-grid-rows: 1fr;
grid-template-columns: 250px 1fr;
}
.sidebar1 {
-ms-grid-row:1;
-ms-grid-column:1;
}
.content {
-ms-grid-row:1;
-ms-grid-column:2;
}
Greetings
Ron

CSS Grid not conforming

Red and Blue sections are next to each other but the yellow section is not on the same line and under the blue section, they are also both the same height as the red section, I can not work out where I have gone wrong
.contact-information {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
grid-auto-rows: minmax(90px, auto);
width: 100%;
overflow: hidden;
}
.contact-left-section {
grid-column: 1/3;
grid-row: 1/2;
background-color: red;
}
.contact-middle-section {
grid-column: 3/6;
grid-row: 1/2;
background-color: blue;
}
.contact-right-section {
grid-column: 6/9;
grid-row: 1/2;
background-color: yellow;
}
<section class="about light contact-information">
<div class="contact-left-section">
<div class="section-title">Phone</div>
<div class="section-content">
<p>Lorem ipsum</p>
</div>
</div>
<div class="contact-middle-section">
<div class="section-title">Services</div>
<div class="section-content">
<p>Lorem ipsum</p>
</div>
<div class="contact-right-section">
<div class="section-title">Email</div>
<div class="section-content">
<p>Lorem ipsum</p>
</div>
</div>
</section>
Firstly, you had only defined eight columns instead of nine but more importantly, you had not closed the .contact-middle-section div leaving the layout broken.
.contact-information {
display: grid;
grid-template-columns: repeat(9, 1fr);
grid-auto-rows: minmax(90px, auto);
width: 100%;
overflow: hidden;
}
.contact-left-section {
grid-column: 1/3;
grid-row: 1/2;
background-color: red;
}
.contact-middle-section {
grid-column: 3/6;
grid-row: 1/2;
background-color: blue;
}
.contact-right-section {
grid-column: 6/9;
grid-row: 1/2;
background-color: yellow;
}
<section class="about light contact-information">
<div class="contact-left-section">
<div class="section-title">Phone</div>
<div class="section-content">
<p>Lorem ipsum</p>
</div>
</div>
<div class="contact-middle-section">
<div class="section-title">Services</div>
<div class="section-content">
<p>Lorem ipsum</p>
</div>
</div>
<div class="contact-right-section">
<div class="section-title">Email</div>
<div class="section-content">
<p>Lorem ipsum</p>
</div>
</div>
</section>
You can try to replace:
grid-template-columns:1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
for:
grid-template-columns: repeat(9,1fr);
I hope this solution will help...

Resources