Understanding why two DIVS behave a certain way can be quite complex, but it ultimately comes down to their display property being set as display:inline-block
. This causes them to act like words in a sentence within an inline formatting context. The presence of spaces in the HTML code is crucial for rendering proper spacing between elements. When inspecting the elements in Chrome Inspector, the formatting may obscure this fact. However, manipulating the HTML within Chrome Inspector can reveal how spaces affect the layout.
To see a demonstration of this concept, you can visit this CodePen example: http://codepen.io/MarkAtRamp51/pen/XKYNpZ
A comparison between two snippets of HTML code highlights the impact of whitespace on layout:
First, the snippet displaying whitespaces between form groups:
<input type="text" class="form-control" id="exampleInputName2"
placeholder="Jane Doe"> </div>
<div class="form-group">
<label for="exampleInputEmail2">Email</label>
And next, the snippet with no whitespaces between form groups:
<input type="text" class="form-control" id="exampleInputName2" placeholder="Jane Doe">
</div><div class="form-group">
<label for="exampleInputEmail2">Email</label>
The difference is evident when comparing the two examples where the second set of DIVs are positioned adjacent to each other.
For further insights on this topic, refer to these resources:
https://css-tricks.com/fighting-the-space-between-inline-block-elements/ and
https://www.w3.org/TR/CSS2/visuren.html