How do I remove grid gaps when columns are not present? - css

I have a section of html that needs to be displayed in a certain way but some elements may be optional and I'm not sure if I can do it with display:grid.
I need to have 3 columns, but the first and last one are optional and I need to remove the gap when they are not present.
Note that the markup needs to be this one without extra wrapper :
.grid {
display: grid;
grid-template-columns: auto 1fr auto;
grid-gap: 0 20px;
align-items: center;
background-color: lightgrey;
}
.grid > .image {
grid-column: 1;
grid-row: 1 / span 2;
background-color: red;
}
.grid > .title {
grid-column: 2;
background-color: blue;
}
.grid > .description {
grid-column: 2;
background-color: purple;
}
.grid > .button {
grid-column: 3;
grid-row: 1 / span 2;
background-color: green;
}
<div class="grid">
<div class="image">image</div>
<div class="title">title</div>
<div class="description">description</div>
<div class="button">button</div>
</div>
<p> </p>
<p>Unwanted gap when no image :</p>
<div class="grid">
<div class="title">title</div>
<div class="description">description</div>
<div class="button">button</div>
</div>
<p> </p>
<p>Unwanted gap when no image or button :</p>
<div class="grid">
<div class="title">title</div>
<div class="description">description</div>
</div>

Rely on implicit column creation and keep only one explicit column
.grid {
display: grid;
grid-template-columns: 1fr; /* one column here */
grid-gap: 0 20px;
align-items: center;
background-color: lightgrey;
}
.grid > .image {
grid-column: -3; /* this will create an implicit column at the start */
grid-row: span 2;
background-color: red;
}
.grid > .title {
background-color: blue;
}
.grid > .description {
background-color: purple;
}
.grid > .button {
grid-column: 2; /* this will create an implicit column at the end */
grid-row:1/ span 2;
background-color: green;
}
<div class="grid">
<div class="image">image</div>
<div class="title">title</div>
<div class="description">description</div>
<div class="button">button</div>
</div>
<p> </p>
<div class="grid">
<div class="title">title</div>
<div class="description">description</div>
<div class="button">button</div>
</div>
<p> </p>
<div class="grid">
<div class="image">image</div>
<div class="title">title</div>
<div class="description">description</div>
</div>
<p> </p>
<div class="grid">
<div class="title">title</div>
<div class="description">description</div>
</div>

Instead of using grid gaps, use margins on the individual elements.
.grid {
display: grid;
grid-auto-columns: auto 1fr auto;
/* grid-gap: 0 20px; */
align-items: center;
background-color: lightgrey;
}
.grid > .image {
margin-right: 20px; /* new */
grid-column: 1;
grid-row: 1 / span 2;
background-color: red;
}
.grid > .button {
margin-left: 20px; /* new */
grid-column: 3;
grid-row: 1 / span 2;
background-color: green;
}
.grid > .title {
grid-column: 2;
background-color: blue;
}
.grid > .description {
grid-column: 2;
background-color: purple;
}
<div class="grid">
<div class="image">image</div>
<div class="title">title</div>
<div class="description">description</div>
<div class="button">button</div>
</div>
<p> </p>
<p>NO unwanted gap when no image :</p>
<div class="grid">
<div class="title">title</div>
<div class="description">description</div>
<div class="button">button</div>
</div>
<p> </p>
<p>NO unwanted gap when no image or button :</p>
<div class="grid">
<div class="title">title</div>
<div class="description">description</div>
</div>

Related

How to create layout with grid

I want to create layout where the last element is centered. I should do with grids, but cant center the last element
Desired view
.box {
min-width: 50px;
min-height:50px;
background: red;
margin: 5px;
}
.container {
width: 200px;
display: grid;
grid-template-columns: auto auto auto;
}
.box7 {
grid-column-start: 2;
grid-row-start: 3;
grid-column-end: 4;
}
<div class='container'>
<div class='box'>1</div>
<div class='box'>2</div>
<div class='box'>3</div>
<div class='box'>4</div>
<div class='box'>5</div>
<div class='box'>6</div>
<div class='box box7'>7</div>
<div>
Desired view
.box {
min-height: 50px;
background: red;
margin: 5px;
grid-column: span 2;
}
.container {
width: 200px;
display: grid;
grid-template-columns: repeat(6, 1fr);
}
.box7 {
grid-column-start: 2;
grid-column-end: 6;
}
<div class='container'>
<div class='box'>1</div>
<div class='box'>2</div>
<div class='box'>3</div>
<div class='box'>4</div>
<div class='box'>5</div>
<div class='box'>6</div>
<div class='box box7'>7</div>
<div>
You can also find multiple ways on this discussion
I hope this approach of mine will help you:
.box {
min-width: 50px;
min-height:50px;
background: red;
margin: 5px;
}
.container {
width: 200px;
display: grid;
grid-template-rows: repeat(3, [row] auto );
grid-template-columns: repeat(3, [col] auto) ;
}
.subgrid {
grid-column: col / span 3;
grid-row: row 3;
}
.box7
{
width:66%;
margin-left:auto;
margin-right:auto;
}
<div class='container'>
<div class='box'>1</div>
<div class='box'>2</div>
<div class='box'>3</div>
<div class='box'>4</div>
<div class='box'>5</div>
<div class='box'>6</div>
<div class="subgrid">
<div class='box box7'>7</div>
</div>
<div>

Cannot adjust padding of elements of inner CSS Grid in style sheet but works inline

So I am testing on CSS Grid, I have made a 4 column grid and in one of the rows I added a 3 column grid. I am having trouble having the elements of the inner grid fill out.
Changing the style="padding:0px" of the InnerGrid1 div works, but if i add that same line of styling in the dedicated CSS section it does not respond. This tells me that I am over-writing something in my inline css but i would like to know what it is
This is all my code
<head>
<style>
* {
text-align: center;
}
.MainGrid {
display: grid;
grid-template-columns: repeat(4, 1fr);
}
.MainGrid > div {
padding: 1em;
grid-gap: 1em;
border-style: solid;
}
.Unchangeables {
background-color: rgb(121, 212, 248);
}
.Header {
grid-column: 1/5;
}
.Gridtitle {
grid-column: 1/5;
background-color: rgb(121, 212, 248);
}
.QR {
grid-column: span 2;
grid-row: span 2;
}
.PicQR {
grid-row: span 2;
background-color: rgb(121, 212, 248);
}
.TypeOfAccountText {
grid-column: 1/4;
border-style: solid;
}
.Footer {
grid-column: 1/5;
}
.AccountName {
grid-column: 2/4;
}
.PictureUpload {
grid-row: span 6;
}
.CivilIDEntry {
grid-column: span 2;
}
.NationalityEntry {
grid-column: span 2;
}
.AddressEntry {
grid-column: span 2;
}
.DOBEntry {
grid-column: span 2;
}
.MovementEntry {
grid-column: span 2;
}
.TextOrQrCodeEntry {
grid-column: span 3;
}
.JudgementText {
grid-column: span 3;
}
.Tweet {
border-style: solid;
}
.TypeOfActivity {
border-style: solid;
}
.InnerGrid1 {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-column: span 2;
}
.InnerGrid1 > div {
border-style: solid;
padding: 1px;
}
</style>
</head>
<body>
<div class="MainGrid">
<div class="Header">HEADER IMAGE HERE</div>
<div class="GridTitle">
بطاقة طلب مرئيات لمنشور في موقع للتواصل الإجتماعي
</div>
<div class="Unchangeables">*******#</div>
<div class="Unchangeables">إسم الحساب</div>
<div class="Unchangeables">Twitter</div>
<div class="Unchangeables">الموقع</div>
<span contenteditable="true" class="TypeOfActivity">نوع النشاط</span>
<div class="QR">Pic-QR CODE</div>
<div class="PicQR">صورة المنشور/ التغريدة</div>
<span class="Tweet" contenteditable="true">تغريد</span>
<span contenteditable="true" class="TypeOfAccountText">***text***</span>
<div class="Unchangeables">طبيعة نشاط الحساب</div>
<div>صورة شخصية</div>
<div class="AccountName">********</div>
<div class="Unchangeables">إسم صاحب الحساب</div>
<div class="PictureUpload">BIG PICTURE</div>
<div class="CivilIDEntry">*****25</div>
<div class="Unchangeables">الرقم المدني</div>
<div class="NationalityEntry">كويتي الجنسية / إسم الأم / خزنة مسير</div>
<div class="Unchangeables">الجنسية</div>
<div class="DOBEntry">٥٦ سنة مواليد 1966/10/18</div>
<div class="Unchangeables">العمر</div>
<div class="InnerGrid1">
<span contenteditable="true">مدرس</span>
<div class="Unchangeables">الحالة الوظيفية</div>
<span contenteditable="true">متزوج </span>
</div>
<!--End of InnerGrid1-->
<div class="Unchangeables">الحالة الاجتماعية</div>
<div class="AddressEntry">
صليبيخات، قطعة (*) جادة (*) منزل (*) <br />
هاتف رقم *******
</div>
<div class="Unchangeables">العنوان</div>
<div class="MovementEntry">
داخل البلاد قادما من إيران بتاريخ ٢٣/٧/ ٢٠٢١و لديه الكثير من الحركة إلي
العراق و إيران
</div>
<div class="Unchangeables">الحركة</div>
<div class="TextOrQrCodeEntry">Text or qr code</div>
<div class="Unchangeables">السوابق الجنائية</div>
<div class="JudgementText">text</div>
<div class="Unchangeables">الرأي القانوني</div>
<div class="Footer">Footer</div>
</div>
<!--MainGrid End-->
</body>
</html>
the class "InnerGrid1" is the grid within the "MainGrid" class.
This is how it looks like with the problem:
This is how it looks like after its fixed by adding "padding:0px" inline css at InnerGrid1 div:
The problem was that I had set a padding already for all divs within my "MainGrid" class here:
.MainGrid > div {
padding: 1em;
grid-gap: 1em;
border-style: solid;
}
Remove padding from Maingrid class and give to Unchangeables class.
* {
text-align: center;
}
.MainGrid {
display: grid;
grid-template-columns: repeat(4, 1fr);
}
.MainGrid>div {
grid-gap: 1em;
border-style: solid;
}
.Unchangeables {
padding: 1rem;
background-color: rgb(121, 212, 248);
}
.Unchangeables {
}
.Header {
grid-column: 1/5;
}
.Gridtitle {
grid-column: 1/5;
background-color: rgb(121, 212, 248);
}
.QR {
grid-column: span 2;
grid-row: span 2;
}
.PicQR {
grid-row: span 2;
background-color: rgb(121, 212, 248);
}
.TypeOfAccountText {
grid-column: 1/4;
border-style: solid;
}
.Footer {
grid-column: 1/5;
}
.AccountName {
grid-column: 2/4;
}
.PictureUpload {
grid-row: span 6;
}
.CivilIDEntry {
grid-column: span 2;
}
.NationalityEntry {
grid-column: span 2;
}
.AddressEntry {
grid-column: span 2;
}
.DOBEntry {
grid-column: span 2;
}
.MovementEntry {
grid-column: span 2;
}
.TextOrQrCodeEntry {
grid-column: span 3;
}
.JudgementText {
grid-column: span 3;
}
.Tweet {
border-style: solid;
}
.TypeOfActivity {
border-style: solid;
}
.InnerGrid1 {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-column: span 2;
}
.InnerGrid1>div {
border-style: solid;
padding: 1px;
}
<div class="MainGrid">
<div class="Header">HEADER IMAGE HERE</div>
<div class="GridTitle">
بطاقة طلب مرئيات لمنشور في موقع للتواصل الإجتماعي
</div>
<div class="Unchangeables">*******#</div>
<div class="Unchangeables">إسم الحساب</div>
<div class="Unchangeables">Twitter</div>
<div class="Unchangeables">الموقع</div>
<span contenteditable="true" class="TypeOfActivity">نوع النشاط</span>
<div class="QR">Pic-QR CODE</div>
<div class="PicQR">صورة المنشور/ التغريدة</div>
<span class="Tweet" contenteditable="true">تغريد</span>
<span contenteditable="true" class="TypeOfAccountText">***text***</span>
<div class="Unchangeables">طبيعة نشاط الحساب</div>
<div>صورة شخصية</div>
<div class="AccountName">********</div>
<div class="Unchangeables">إسم صاحب الحساب</div>
<div class="PictureUpload">BIG PICTURE</div>
<div class="CivilIDEntry">*****25</div>
<div class="Unchangeables">الرقم المدني</div>
<div class="NationalityEntry">كويتي الجنسية / إسم الأم / خزنة مسير</div>
<div class="Unchangeables">الجنسية</div>
<div class="DOBEntry">٥٦ سنة مواليد 1966/10/18</div>
<div class="Unchangeables">العمر</div>
<div class="InnerGrid1">
<span contenteditable="true">مدرس</span>
<div class="Unchangeables">الحالة الوظيفية</div>
<span contenteditable="true">متزوج </span>
</div>
<!--End of InnerGrid1-->
<div class="Unchangeables">الحالة الاجتماعية</div>
<div class="AddressEntry">
صليبيخات، قطعة (*) جادة (*) منزل (*) <br /> هاتف رقم *******
</div>
<div class="Unchangeables">العنوان</div>
<div class="MovementEntry">
داخل البلاد قادما من إيران بتاريخ ٢٣/٧/ ٢٠٢١و لديه الكثير من الحركة إلي العراق و إيران
</div>
<div class="Unchangeables">الحركة</div>
<div class="TextOrQrCodeEntry">Text or qr code</div>
<div class="Unchangeables">السوابق الجنائية</div>
<div class="JudgementText">text</div>
<div class="Unchangeables">الرأي القانوني</div>
<div class="Footer">Footer</div>
</div>
Make it flex and align its items in center using align-items: center;
.Unchangeables {
display: flex;
justify-content:center;
align-items: center;
}

Make Grid Item Auto Fill

sorry I'm very new to grid-layout
I have a layout that I believe it is really easy for grid system
but I have no idea how to modify it for my layout:
the layout has three type:
if only one item: full width, full height
if two items, left and right; 50% width, full height
if three items, correct code is below
.container {
display: grid;
width: 200px;
height: 200px;
grid-template-columns: 1fr 1fr;
grid-template-rows: 50% 50%;
grid-gap: 8px;
}
.a {
grid-column: 1;
grid-row: 1 / 3;
background: red;
}
.b {
grid-column: 2;
grid-row: 1;
background: blue;
}
.c {
grid-column: 2;
grid-row: 2;
background: green;
}
<div class="container">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
</div>
code above is for type 3 and is really correct
how should I modify it let it fit type 1 and 2 ??
You can do it like below:
.container {
display: inline-grid;
margin:5px;
vertical-align:top;
width: 200px;
height: 200px;
grid-gap: 8px;
}
.a { background: red;}
.b { background: blue;}
.c { background: green;}
/* when 2 items we add another column*/
.container :nth-child(2):nth-last-child(1) {
grid-column:2;
}
/* when 3 items we add another row as well */
.container :nth-child(3):nth-last-child(1) {
grid-row:2;
grid-column:2;
}
/* when 3 items, the first one will span 2 rows*/
.container :first-child:nth-last-child(3) {
grid-row:span 2;
}
<div class="container">
<div class="a"></div>
</div>
<div class="container">
<div class="b"></div>
</div>
<div class="container">
<div class="c"></div>
</div>
<div class="container">
<div class="a"></div>
<div class="b"></div>
</div>
<div class="container">
<div class="b"></div>
<div class="c"></div>
</div>
<div class="container">
<div class="a"></div>
<div class="c"></div>
</div>
<div class="container">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
</div>

CSS Grid inconsistent formatting when a top level item is not a div

what is the reason that a CSS Grid layout is rendered differently when a top level child item is an input rather than a div? and how could I resolve this so that they are rendered consistently?
CSS:
.container {
display: grid;
grid-template-rows: 1fr;
grid-template-columns: auto;
grid-auto-flow: column;
grid-row-gap: 1rem;
grid-column-gap: 1rem;
margin-bottom: 1rem;
}
.container > * {
border: 2px dashed blue;
}
.item-1 {
grid-row: 1;
grid-column: 1 / span 5;
}
.item-2 {
grid-row: 1;
grid-column: 6;
}
.item-3 {
grid-row: 1;
grid-column: 7 / span 4;
}
.item-4 {
grid-row: 1;
grid-column: 11 / span 1;
}
CSS Grid with divs as children:
<div class="container">
<div class="item-1">Item 1</div>
<div class="item-2">Item 2</div>
<div class="item-3">Item 3</div>
<div class="item-4">Item 4</div>
</div>
CSS Grid with one child instead as an input:
<div class="container">
<div class="item-1">Item 1</div>
<div class="item-2">Item 2</div>
<input type="text" class="item-3" value="item 3" />
<div class="item-4">Item 4</div>
</div>
Always define the size explicitely otherwise the auto size will do the job and an input is not the same as a div
.container {
display: grid;
grid-template-rows: 1fr;
grid-auto-columns: 1fr; /* the size of 1 column */
grid-auto-flow: column;
grid-row-gap: 1rem;
grid-column-gap: 1rem;
margin-bottom: 1rem;
}
.container>* {
border: 2px dashed blue;
}
.item-1 {
grid-column: 1 / span 5;
}
.item-2 {
grid-column: 6;
}
.item-3 {
grid-column: 7 / span 4;
}
.item-4 {
grid-column: 11 / span 1;
}
CSS Grid with divs as children:
<div class="container">
<div class="item-1">Item 1</div>
<div class="item-2">Item 2</div>
<div class="item-3">Item 3</div>
<div class="item-4">Item 4</div>
</div>
CSS Grid with one child instead as an input:
<div class="container">
<div class="item-1">Item 1</div>
<div class="item-2">Item 2</div>
<input type="text" class="item-3" value="item 3" />
<div class="item-4">Item 4</div>
</div>

Is there a way to collapse certain grid-gaps?

I've setup a grid-template but a problem has arisen in the front-end template layer. Basically there comes a time when divs with assigned grid-areas conditionally render onto the page. Being that I have a grid-row-gap declaration in place, it maintains gaps of grid template areas that aren't present on the page. I was wondering if there was a way to collapse these gaps when this sort of thing occurs.
.grid {
display: grid;
grid-template-areas:
'a b'
'a c'
'a d'
'a e'
'a f';
grid-gap: 25px;
}
.cell {
display: flex;
align-items: center;
justify-content: center;
padding: 15px;
border: 1px solid #000;
}
.a {
grid-area: a;
}
.b {
grid-area: b;
}
.c {
grid-area: c;
}
.d {
grid-area: d;
}
.e {
grid-area: e;
}
.f {
grid-area: f;
}
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<!-- <div class="c cell">C</div> -->
<!-- <div class="d cell">D</div> -->
<div class="e cell">E</div>
<div class="f cell">F</div>
</div>
You can think it otherwise, use template and span instead area , and margin instead grid-gap.
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
margin: 1em;
background: #bee;
}
.cell {
display: flex;
align-items: center;
justify-content: center;
padding: 15px;
border: 1px solid #000;
grid-column: 2;
}
.a {
grid-column: 1;
grid-row: 1 / span 10;
/* here what you think is enough */
}
.cell:nth-child(1)~.cell {
margin-left: 24px;
}
.cell:nth-child(2)~.cell {
margin-top: 24px;
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<div class="c cell">C</div>
<div class="d cell">D</div>
<div class="e cell">E</div>
<div class="f cell">F</div>
</div>
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<div class="d cell">D</div>
<div class="f cell">F</div>
</div>
Don't use template-areas and reply on auto placement:
.grid {
display: grid;
grid-template-columns: 1fr 1fr; /* 2 columns */
column-gap: 25px;
margin:5px;
}
.cell {
display: flex;
align-items: center;
justify-content: center;
padding: 15px;
border: 1px solid #000;
}
.grid .a {
grid-row: span 5; /* take 5 rows */
}
/* to replace vertical gap, don't apply to "a" and last-child */
.cell:not(.a):not(:last-child) {
margin-bottom:25px;
}
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<!-- <div class="c cell">C</div> -->
<!-- <div class="d cell">D</div> -->
<div class="e cell">E</div>
<div class="f cell">F</div>
</div>
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<div class="c cell">C</div>
<!-- <div class="d cell">D</div> -->
<div class="e cell">E</div>
<div class="f cell">F</div>
</div>
<div class="grid">
<div class="a cell">A</div>
<div class="b cell">B</div>
<div class="c cell">C</div>
</div>

Resources