/    Sign up×
Community /Pin to ProfileBookmark

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

I asked this question on another site and have had zero responses. Is it such a bad question?
Anyway, I thought I would try here for at least a look at my question.

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:
https://medium.com/better-programming/make-a-responsive-grid-with-only-3-lines-of-css-5e2f3837ccc
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
–>
<style>
.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;
}
</style>

</head><body>

<h2>Grid – DIV</h2>
<div class=”grid”>
<div class=”Ditem”>Product 1 <br>
This contains a longer number of words within the item.
</div>
<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>
</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>
<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>
</div>

</body></html>`

to post a comment
CSS

5 Comments(s)

Copy linkTweet thisAlerts:
@cootheadOct 05.2020 — Hi there JMRKER,

here is the solution and the answer all in one...
<i>
</i>.grid pre {
margin: 0;
}


[i]coothead[/i]
Copy linkTweet thisAlerts:
@KeverOct 05.2020 — The pre element gets a default "margin: 1em 0px" from the user agent stylesheet.
Copy linkTweet thisAlerts:
@JMRKERauthorOct 05.2020 — Thank you both. I was not aware of any default margin settings!

Never heard of an 'user agent stylesheet' or its initialization.

Where do you guys fine this obscure information ... crazy! :)
Copy linkTweet thisAlerts:
@cootheadOct 05.2020 — Hi there JMRKER,

>[color=#069]Where do you guys fine this obscure information[/color]

I normally use a [i]search engine[/i]. :)

Here are two useful sheets...

  • 1. [url=https://hell.meiert.org/core/css/firefox-20190331.css][u]Firefox (March 31, 2019)[/u][/url]

  • 2. [url=https://chromium.googlesource.com/chromium/blink/+/master/Source/core/css/html.css][u]Chrome (latest)[/u][/url]


  • ...that I found a couple of minutes ago,

    [i]coothead[/i]
    Copy linkTweet thisAlerts:
    @KeverOct 05.2020 — When you inspect an element in the developer tools, it shows all the applied styles and the stylesheets they originate from. In some browsers you might have to go to the dev-tool settings and turn on "Show browser styles".

    Here is some information on the types of stylesheets and their cascading order.

    https://developer.mozilla.org/en-US/docs/Web/CSS/Cascade
    ×

    Success!

    Help @JMRKER spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.26,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...