Jump to content

Vertical gap between DIV vs PRE using css grid. Why?


Recommended Posts

The only intended difference between the grid displays in the following are the use of DIV versus PRE tags.

I don't understand why there is a large vertical gap between the displays.

Not a major problem, just would like to know why the difference or where it is coming from.

<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=yes"/>
<title>css Simple Grid compare</title>

<!-- From:
  see also: https://css-tricks.com/auto-sizing-columns-css-grid-auto-fill-vs-auto-fit/
  see also: https://gridbyexample.com/examples/example37/
  see also: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Auto-placement_in_CSS_Grid_Layout
 .grid {
   display: grid;             /* auto-fill is the same */
   grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
   grid-gap: 4px;
  .Ditem { border: 1px solid blue; }
  .Pitem { border: 1px solid blue; white-space: pre-wrap;       


 <h2>Grid - DIV</h2>
<div class="grid">
  <div class="Ditem">Product 1 <br>
This contains a longer number of words within the item.
  <div class="Ditem">Product 2</div>
  <div class="Ditem">Product 3</div>
  <div class="Ditem">Product 4</div>
  <div class="Ditem">Product 5</div>
  <div class="Ditem">Product 6</div>
  <div class="Ditem">Product 7 - no vertical gap!</div>

 <h2>Grid - PRE</h2>
<div class="grid">
  <pre class="Pitem">Product A <br>
This contains a longer number of words within the item.
  <pre class="Pitem">Product B</pre>
  <pre class="Pitem">Product C</pre>
  <pre class="Pitem">Product D</pre>
  <pre class="Ditem">Product E</pre>
  <pre class="Ditem">Product F</pre>
  <pre class="Pitem">Product G - why such a large vertical gap?</pre>



Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...