1

https://codepen.io/Deepu55555/pen/YBQxYd

HTML

<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>

CSS

body {
  background-color: #fefbd8;
}

h1 {
  background-color: #80ced6;
}

div {
  background-color: #d5f4e6;
}

span {
  background-color: #f18973;
}

I have two spans and assigned background color for both of them, is there any way I could fill the extra space with the same background color as the previous one? so that the background color doesn't separate between two spans?

Sundeep Pidugu
  • 2,377
  • 2
  • 21
  • 43

4 Answers4

3

You Can use One span for that two words so it will fill the color between space

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 color</span> for only a part of a text.</p>
Asad
  • 124
  • 1
  • 9
  • I am not looking for a solution which as a single span as that's not my requirement :( sorry. –  Feb 04 '19 at 06:30
  • 1
    you can also do the same by removing space between the span tag like the following code

    Set a background color for only a part of a text.

    – Asad Feb 04 '19 at 06:58
1

Hope you need to avoid the empty space between "background" and "color". If so you can wrap those two "span" element with a parent "span".

<p>Set a <span>
<span>background</span> <span>color</span>    
</span> for only a part of a text.</p>
Rajesh
  • 254
  • 1
  • 6
1

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>
Code_Ninja
  • 1,729
  • 1
  • 14
  • 38
  • I think this is an interesting solution to solve such simple problem btw thanks will try doing this, do you have any other solution for the same instead of using a parent span ? –  Feb 04 '19 at 06:28
  • Do you want to use a coding solution for this issue? I can provide you with one, but it would be wastage of effort, since there is no point in making a simple solution complex. – Code_Ninja Feb 04 '19 at 06:30
  • If that uses javascript and CSS together please give let me know the alternative way of doing the same as I am dealing with block elements and this might not work in some cases. –  Feb 04 '19 at 06:33
  • Wait let me edit my answer, I have a way – Code_Ninja Feb 04 '19 at 06:34
  • Please check, I have added another solution – Code_Ninja Feb 04 '19 at 06:37
1

You can also do the same thing by removing space between span

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>
Asad
  • 124
  • 1
  • 9