For example, there is such a construction:

div{ width:300px; } p span{ background:#cc0000; padding:0 10px; border-radius:8px; word-wrap:break-word; color:#cc0000; } ::selection{ color:transparent; background:transparent; } ::-webkit-selection{ color:transparent; background:transparent; } ::-moz-selection{ color:transparent; background:transparent; } p{ line-height:22px; } 
 <div> <p> <span> Praesent </span><span>sapien</span> <span>massa,</span> <span>convallis</span><span> a pellentesque</span> <span>nec,</span><span> egestas non nisi. </span><span>Vestibulum</span><span> ac diam </span><span>sit amet quam</span> <span>vehicula</span><span> elementum sed</span><span> sit amet dui.</span><span> Praesent sapien</span> <span>massa, convallis</span><span> a </span><span>pellentesque nec,</span><span> egestas non nisi.</span> </p> </div> 

But if you put each word in the <p></p> without <span></span> then personally I don’t have anything, that is, you need to make each word not visible as in the example above, but at the same time that the text was located just in the <p></p> . How to achieve the same without nested <span></span> ?

  • one
    This is hardly possible, the text node does not have a form and the display options do not apply to it - Konstantin Basharkevich
  • one
    maybe there is a Shadow DOM? - Konstantin Basharkevich
  • one
    as I suppose, the styles remain the same, only with the help of JS the text is broken into an array of words, and then glued together, but already with the words wrapped around the span. And in order not to spoil the content, it seems to me that the result can be put in the Shadow DOM. I hadn’t worked with him myself before (there was no precedent), but the question is interesting, I’ll try to figure out an example later) - Konstantin Basharkevich
  • one
    ruseller.com/lessons/les2094/demo/index2.html here it is this example - user33274
  • 2
    Ahaha, everything is much simpler there =) This is a font like this =) - Konstantin Basharkevich

1 answer 1

A similar result can be achieved by replacing the font on BLOKK


UPD : And here is the solution using the Shadow DOM, which I wrote about in the comments (it was interesting to try to implement it):

 window.addEventListener('DOMContentLoaded', function() { var target = document.querySelector('p'), wordSet = target.textContent.match(/[^\s]+/g), shadowHTML = '', shadowRoot = target.createShadowRoot(); wordSet.forEach(function(word) { shadowHTML += ` <span>${word}</span> `; }); shadowRoot.innerHTML = shadowHTML; /* Как видим, содержимое элемента не изменилось ;D */ console.log(target.innerHTML); }); 
 p { width: 300px; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none; } p::shadow span { margin: 2px; padding: 0 5px; display: inline-block; font-size: .8em; color: transparent; background: #cc0000; border-radius: 8px; word-wrap: break-word; } 
 <p> Praesent <i>sapien massa</i>, convallis a pellentesque nec, egestas non nisi. Vestibulum ac diam sit amet <b>quam</b> vehicula elementum sed sit amet dui. Praesent sapien massa, convallis a pellentesque nec, egestas non nisi. </p> 

Here is what helped me achieve it:

  • if we say not to use this font, then there are options? preferably an example with 10 words - user33274
  • @Geyan I suggested an alternative with JS. The truth is that we must take into account that not every browser will live on the Shadow DOM, and the template lines will have to be changed to regular ones, but again, the new feature is not supported by all. - Konstantin Basharkevich
  • Kostya - can you explain something to me ?? - user33274
  • @Geyan chat.stackexchange.com/rooms/43848 - Konstantin Basharkevich