Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem with vertical spacing with css grid

Note div.text first two div always will be top (also second div will be sticky div) and other div will be add dynamically from the bottom by js insertAdjacentHTML()

I am getting an extra space between two div.text vertically

is there is possible way to resolve with css grid ?

Final Result

  *{
box-sizing: border-box;
margin:0;
padding:0;
}
h1,h2,h3,h4,h5,h6{
    margin:0;
padding:0;
}
.grid-container {
  display: grid;
  grid-template-columns: repeat(2,1fr)  ;
  grid-gap: 1px;
  background-color: #f7ca18;
  padding: 10px;
   color:#000;
  height:300px;
  
}

.grid-container > div.item {
   display: grid;
  grid-template-columns: 1fr ;
  grid-template-rows: auto;
  background-color: #f4d03f;
  text-align: center;
  font-size: 14px;
  overflow-y:auto;
  overflow-x:hidden;
}
.grid-container > div.item div.text {
   align-self: end;
   display:grid;
   grid-template-columns: 1fr max-content ;
}
.grid-container > div.item div.text:nth-child(2){
  position: -webkit-sticky; 
  position: sticky;
  top: 0;
  background:red;
  align-self: start;
}
<div class="grid-container">
  <div class="item">
    <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

</p>
     
  </div>
  <div class="item">
    
    <div class="text">
        <span>Web App</span>
    </div>
     <div class="text">
        <h1>Sticky Dive</h1>
    </div>
    <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
  <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
      <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
     <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
    
  </div>
</div>

Initial problem with vertical space when two or three div.text added

  *{
box-sizing: border-box;
margin:0;
padding:0;
}
h1,h2,h3,h4,h5,h6{
    margin:0;
padding:0;
}
.grid-container {
  display: grid;
  grid-template-columns: repeat(2,1fr)  ;
  grid-gap: 1px;
  background-color: #f7ca18;
  padding: 10px;
   color:#000;
  height:300px;
  
}

.grid-container > div.item {
   display: grid;
  grid-template-columns: 1fr ;
  grid-template-rows: auto;
  background-color: #f4d03f;
  text-align: center;
  font-size: 14px;
  overflow-y:auto;
  overflow-x:hidden;
}
.grid-container > div.item div.text {
   align-self: end;
   display:grid;
   grid-template-columns: 1fr max-content ;
}
.grid-container > div.item div.text:nth-child(2){
  position: -webkit-sticky; 
  position: sticky;
  top: 0;
  background:red;
  align-self: start;
}
<div class="grid-container">
  <div class="item">
    <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

</p>
     
  </div>
  <div class="item">
    
    <div class="text">
        <span>Web App</span>
    </div>
     <div class="text">
        <h1>Sticky Dive</h1>
    </div>
    <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
  <div class="text">
        <h6>Title One</h6>
        <span>Hello - <i>X</i></span>
    </div>
   
    
    
    
  </div>
</div>

What it looks like initially when two divs are added

enter image description here

like image 903
نور Avatar asked Oct 03 '20 05:10

نور


People also ask

Why are there gaps in my grid CSS?

The vertical gaps are caused by the images not filling the vertical space in the grid items. The problem is made worse with align-items: center on the container, which removes the align-items: stretch default. Essentially, there are no gaps between grid items.

Is CSS Grid gap deprecated?

'grid-gap' has been deprecated in favor of 'gap'. In a similar vein 'grid-row-gap' and 'grid-column-gap' have been deprecated in favor of 'row-gap' and 'column-gap'. Unfortunately, the new features aren't recognized by Visual Studio yet either. As far as May 2020, 'grid-gap' is shown as "Obsolete" in VSC.

How do I make my grid gap responsive?

It helped me to find the following solution: Instead of setting grid-gap, set grid-column-gap and grid-row-gap separately, with grid-row-gap equal to grid-column-gap / (1 - grid-column-gap). Set padding-bottom of the container equal to grid-column-gap.


1 Answers

You can use grid-template-rows: auto 1fr; and it will fix your issue.

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  margin: 0;
  padding: 0;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 1px;
  background-color: #f7ca18;
  padding: 10px;
  color: #000;
  height: 300px;
}

.grid-container>div.item {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto 1fr;
  background-color: #f4d03f;
  text-align: center;
  font-size: 14px;
  overflow-y: auto;
  overflow-x: hidden;
}

.grid-container>div.item div.text {
  align-self: end;
  display: grid;
  grid-template-columns: 1fr max-content;
}

.grid-container>div.item div.text:nth-child(2) {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  background: red;
  align-self: start;
}
<div class="grid-container">
  <div class="item">
    <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content
      here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.
      Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
      The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem
      Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    </p>
  </div>
  <div class="item">
    <div class="text">
      <span>Web App</span>
    </div>
    <div class="text">
      <h1>Sticky Dive</h1>
    </div>
    <div class="text">
      <h6>Title One</h6>
      <span>Hello - <i>X</i></span>
    </div>
    <div class="text">
      <h6>Title One</h6>
      <span>Hello - <i>1</i></span>
    </div>
    <div class="text">
      <h6>Title One</h6>
      <span>Hello - <i>2</i></span>
    </div>
    <div class="text">
      <h6>Title One</h6>
      <span>Hello - <i>3</i></span>
    </div>
    <div class="text">
      <h6>Title One</h6>
      <span>Hello - <i>4</i></span>
    </div>
  </div>
</div>
like image 183
Dipen Shah Avatar answered Oct 19 '22 22:10

Dipen Shah