This is a sentence.
fills 100% of ...">

How to change background color of TEXT in css

Take for example:

<div id="div1">This is a sentence.</div>

      

<div>

fills 100% of the line.

The text itself is just a small part of it.

I want the text to be the background color WITHOUT giving the whole line the background color.

I can change the html and add of span

course. (And use this range with background-color

)

<div id="div1"><span>This is a sentence.</span></div>

      

But is it possible in pure css without changing the html?

PS: I am doing css code for terminal hyper

.

SOLUTION (for all divs):

div{display:table;}

      

This completely ruins the behavior tmux

(internally hyper

), but maybe I'm asking too much ...

echo -e "\n\n"

also becomes a problem .: P

SOLUTION FOR THIS PROBLEM: (empty div does not take up space)

div:after{content: " ";}

      

+3


source to share


4 answers


you can use display:table-cell

table-cell: let element behave like element <td>

- W3Schools

Also, if you don't want to go back to dark times, this is well supported



#div1 {
  background: red;
  color: white;
  display: table-cell;
}
      

<div id="div1">This is a sentence.</div>Lorem ipsum dolor sit amet, pri cu quod audiam molestie, sit an modo probo conceptam, vim nemore quodsi no. Postea possit ne pro. Ne mel mollis oportere laboramus. Eu dico eius omnes ius. Id vis nibh adipiscing, maiorum suscipit ius eu. Sonet viris antiopam
nec in, est id equidem omnesque cotidieque, tritani detraxit qui cu.
      

Run codeHide result


+1


source


You can use :before

to apply CSS on

#div1 {
  color: blue; // hide original text
  background-color: blue;
  position: relative;
}

#div1:before {
  content: 'This is a sentence.';
  background-color: purple;
  display: inline;
  color: white;
  position: absolute;
}
      

<div id="div1">This is a sentence.</div>
      

Run codeHide result





If you don't want the #div1

full width than just apply to it #div1 {display: inline-block}

and it will act like span

element

+3


source


[The block element always takes the width as 100%, and the inline block takes the width of the element itself. so try using inline css block for element inside div] Display:inline-block;

1 Like for example:

[https://codepen.io/nazarbecks/pen/GEMdaG][1]

      

Codepen link

+1


source


you can install #div1

as inline-block

.

#div1 {
  display: inline-block;
  background-color: color;
}

      

-2


source







All Articles