Your answer is within the specification.
For each inline (including anonymous inlines; see [CSS2] section 9.2.2.1) within an inline formatting context, ...
Any collapsible space immediately following another collapsible space—even one outside the boundary of the inline containing that space, provided both spaces are within the same inline formatting context—is collapsed to have zero advance width. (It is invisible, but retains its soft wrap opportunity, if any.)
It's a bit complex but in the case of inline element the spaces will collapse into one space that will be inside the first inline element and not between both inline element. This is because you had a space at the end of your inline element. In other words, all the spaces will collapse into the first one and the position of the first space will decide about the visual.
Remove the space at the end of your inline elements and you will have a different result:
.div1 {
display: inline;
background-color: lightblue;
}
.div2 {
display: inline;
background-color: orange;
}
<div class="box">
<div class="div1"> Lorem ipsum </div>
<div class="div2"> Lorem ipsum </div>
</div>
<div class="box">
<div class="div1"> Lorem ipsum</div>
<div class="div2"> Lorem ipsum </div>
</div>
A configuration with the space inside the second element:
.div1 {
display: inline;
background-color: lightblue;
}
.div2 {
display: inline;
background-color: orange;
}
<div class="box">
<div class="div1"> Lorem ipsum </div>
<div class="div2"> Lorem ipsum </div>
</div>
<div class="box">
<div class="div1"> Lorem ipsum</div><div class="div2"> Lorem ipsum </div>
</div>
With inline-block
it's different because space from the outside cannot collapse with the space inside the inline-block
element.
inline-block
This value causes an element to generate an inline-level block container. The inside of an inline-block is formatted as a block box, and the element itself is formatted as an atomic inline-level box.ref
Related: CSS Spec - Atomic Inline Level Boxes
The spaces inside inline-block
element are proceeded alone and they will get trimmed following the below rules:
A sequence of collapsible spaces at the beginning of a line is removed.
A sequence at the end of a line of collapsible spaces is removed,
Then the spaces between inline-block
element will collapse into one space. So in all the cases, you will have a space between your inline-block
elements whataver the combination of the spaces inside/outside unless there is no space between them
.div1 {
display: inline-block;
background-color: lightblue;
}
.div2 {
display: inline-block;
background-color: orange;
}
<div class="box">
<div class="div1"> Lorem ipsum </div>
<div class="div2"> Lorem ipsum </div>
</div>
<div class="box">
<div class="div1"> Lorem ipsum</div>
<div class="div2"> Lorem ipsum </div>
</div>
<div class="box">
<div class="div1"> Lorem ipsum</div><div class="div2"> Lorem ipsum </div>
</div>