It’s not that there’s additional hidden code in a gallery, it’s just how the theme styles the code. There are two main ways for the theme to output a gallery. If the theme does nothing special, WordPress outputs a style tag for the gallery and then something like
<style>...</style>
<div id="gallery-1" class="gallery galleryid-555 gallery-columns-3 gallery-size-thumbnail">
<dl class="gallery-item">
<dt class="gallery-icon landscape">
<a href="...">
<img ...>
</a>
</dt>
<dd id="gallery-1-611" class="wp-caption-text gallery-caption">
</dl>
<dl class="gallery-item">
...
But if the theme uses add_theme_support( 'html5', array('gallery') )
, then it looks more like this
<div id="gallery-1" class="gallery galleryid-555 gallery-columns-3 gallery-size-thumbnail">
<figure class="gallery-item">
<div class="gallery-icon landscape">
<a href="...">
<img ...>
</a>
</div>
<figcaption id="gallery-1-611" class="wp-caption-text gallery-caption">
</figure>
<figure class="gallery-item">
...
The gallery shortcode can also be overridden, using totally different HTML tags, but that is for plugins to do. (themes shouldn’t be doing that)
Regardless of the HTML, the style is what makes the spacing between things.
In your example, was your title a paragraph (it would have the bottom margin of a paragraph) or a heading (it would have the bottom margin of a heading)?
You can always go into the HTML view of the post and either put an inline style or a class that you use on multiple posts.
<h3 style="bottom-margin: 1ex">Gallery Title</h3>
or
<h3 class="my-gallery-title">Gallery TItle</h3>
and then in Additional CSS put something like
.my-gallery-title {bottom-margin: 1ex;}
.my-gallery-title + .gallery {top-margin: 1ex;}
My example uses 1ex
as the amount of margin. It’s a small amount (the height of an x in whatever font you have), which you could adjust as needed.