If you just the following way of writing your code, then you can solve the issue:
body {
background-color: #fefbd8;
}
h1 {
background-color: #80ced6;
}
div {
background-color: #d5f4e6;
}
span {
background-color: #f18973;
}
<h1>Background Color</h1>
<div>Set a background color for a div element.</div>
<p>Set a <span>background </span><span>color</span> for only a part of a text.</p>
This will work if you are working on static data, if you working with dynamic data then also, you can manage the spaces using this same representation.
EDIT #1:
Or instead of the following line:
<p>Set a <span>background </span><span>color</span> for only a part of a text.</p>
You can use the following:
<p>Set a <span>background color</span> for only a part of a text.</p>
EDIT #2:
Add the following to your CSS:
span::before{
content: " ";
}
And change the following line:
<p>Set a <span>background </span> <span>color</span> for only a part of a text.</p>
to (remove space between the spans):
<p>Set a <span>background </span><span>color</span> for only a part of a text.</p>
Then, the solution would appear like the following:
body {
background-color: #fefbd8;
}
h1 {
background-color: #80ced6;
}
div {
background-color: #d5f4e6;
}
span {
background-color: #f18973;
}
span::before {
content: " ";
}
<h1>Background Color</h1>
<div>Set a background color for a div element.</div>
<p>Set a <span>background</span><span>color</span> for only a part of a text.</p>