PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 9, 2022

[FIXED] How do I get rid of the space at the bottom of the grid elements?

 November 09, 2022     css, css-grid, html     No comments   

Issue

I have this layout made with CSS grid.

enter image description here

It's adding extra space at the bottom of each element. I'd like the red background(grid cell) to take up the minimum height of it's content.

Something like:

grid-template-rows: <minimum height> <minimum height> <minimum height>;

How do I acheive this?

I know I can just change the HTML and make it easier but since the title comes first on smaller screens, I can't change the structure.

.container {
  display: grid;
  grid-template-areas: "image image title"
                       "image image desc-1"
                       "image image desc-2";
  gap: 1rem;
  width: 500px;
  margin: 0 auto;
}
.item {
  background: tomato;
}
.title {
  grid-area: title;
}
.image {
  grid-area: image;
}
.desc-1 {
  grid-area: desc-1;
}
.desc-2 {
  grid-area: desc-2;
}

.image img {
  display: block;
}
.title, .desc-1, .desc-2 {
  margin-block: 0;
}
<section class="container">
  <h2 class="item title">Lorem ipsum</h2>
  <div class="image">
    <img src="https://picsum.photos/300/200.jpg" />
  </div>
  <p class="item desc-1">Lorem ipsum dolor sit amet.</p>
  <p class="item desc-2">Sed ut perspiciatis unde.</p>
</section>


Solution

Use 4 rows instead of 3. You can also simplify your code like below:

.container {
  display: grid;
  grid-template-columns: 2fr 1fr; /* size of columns */
  gap: 1rem;
  width: 500px;
  margin: 0 auto;
}
.item {
  background: tomato;
}
.image {
  grid-row: 1/span 4; /* placed at 1st row and takes 4 rows */
  grid-column: 1; /* places at 1st column */
}
.image img {
  display: block;
}
.title, .desc-1, .desc-2 {
  margin-block: 0;
}
<section class="container">
  <h2 class="item title">Lorem ipsum</h2>
  <div class="image">
    <img src="https://picsum.photos/300/200.jpg" />
  </div>
  <p class="item desc-1">Lorem ipsum dolor sit amet.</p>
  <p class="item desc-2">Sed ut perspiciatis unde.</p>
</section>



Answered By - Temani Afif
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing