Highlight the first character of each line using css or JS

I have a requirement to highlight the first character in each line, as shown in the figure below, all content is in one paragraph, based on a change in resolution, it should also behave the same.

enter image description here

I tried using flex box, and the first child using a script also could not find a solution

Any help would be appreciated.

+4
source share
4 answers

Only responsive CSS solution using mix-blend-modewhich is not supported by IE and Edge.

The idea is to use color overlay with a fixed width (largest letter width) and mix-blend-mode: screen;for color only the 1st letter of each line.

, , .

p {
  position: relative;
  font-family: monospace; /** to have constant character width **/
  font-size: 28px;
  background: white;
}

p::before {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 1;
  background: red;
  mix-blend-mode: screen;
  pointer-events: none; /** to enable selected of the text under the pseudo element **/
  color: transparent; /** to hide the character **/
  content: 'W'; /** a character in your font to use as width **/
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis quis euismod orci. Morbi ullamcorper augue ipsum, at consectetur turpis dictum sed. Phasellus bibendum tellus eu aliquet congue. Morbi ultrices aliquam purus, id semper diam aliquet eget. Nulla
  at tellus lacus. Curabitur massa lectus, auctor et lobortis vel, sodales non diam. Pellentesque gravida tortor ac ex porta congue. Ut vestibulum arcu ex, ac lacinia tortor pulvinar id. Nulla sagittis, lectus at luctus dignissim, orci nulla interdum
  ex, at euismod nibh ipsum et lacus. Suspendisse potenti. Quisque ac eros nunc. Suspendisse sit amet dolor diam. Phasellus blandit commodo mi, at aliquam mi facilisis ut. Vestibulum lacinia enim tempor nunc elementum suscipit. Praesent ligula ipsum,
  venenatis et tellus at, imperdiet tempus lectus. Pellentesque consequat magna augue, at vestibulum sem facilisis nec.</p>
Hide result
+3

:

p::first-letter {
  font-weight: bold;
  color: #8A2BE2;
}
<p>Hi there</p>
<p>Color first!</p>
Hide result

EDIT: , , script.

var allLines = document.querySelector("p").innerHTML.split("\n");
allLines = allLines.filter(function(x){return x !== ""}).map(function(x){
return "<span>" + x.charAt(0) + "</span>" + x.substr(1);
}).join("<br/>");
document.querySelector("p").innerHTML = allLines;
span {
  color: red;
}
<p>
TEXT
SOME MORE TEXT
</p>
Hide result
+5

(, , ):

var content = document.getElementById('content').innerHTML
var doYourThing = function(){
  var formattedContent = content.replace(/(\w)/g,'<span>$1</span>')
  document.getElementById('formatted').innerHTML = formattedContent

  var characters = document.getElementsByTagName('span')
  var refPosition = characters[0].getBoundingClientRect().left

  for (character of characters){
    if(character.getBoundingClientRect().left <= refPosition){
      character.style.textTransform = "capitalize"
      character.style.color = "red"
    }
  }
}
doYourThing()
window.onresize = doYourThing
<p id="content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque nisi minima veritatis ipsa magnam culpa ipsam dolores. Deserunt rerum, quod unde consequatur consectetur harum omnis sequi, voluptatum quaerat quasi illo?
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repudiandae, esse, repellendus sed voluptate facere magni quis beatae corrupti odio id! Labore voluptatem soluta harum esse, at atque deserunt nesciunt ipsa.
</p>
<p id="formatted" style="background-color:#fee"></p>
Hide result

. . JSFiddle

+3

split("\n"), , , <span> , , join("<br/>") .

var lines = document.querySelector("p").innerHTML.split("\n");
for(var key in lines)
 lines[key] = "<span class='first-letter'>" + lines[key].substr(0, 1) + "</span>" + lines[key].substr(1);
document.querySelector("p").innerHTML = lines.join("<br/>");
span.first-letter {
  font-weight: bold;
  color: red;
}
<p>This is a line 
This is another line 
This is yet another line</p>
Hide result

( )

Another option is split("\n"), as before, but actually replace each line with yourself, enclosed in a tag <p>. Then join()line and replace the original parent with the lines themselves. This allows you to use the ::first-letterCSS pseudo-element for the styles of these classes and is more semantically correct than the previous one.

var lines = document.querySelector("p").innerHTML.split("\n");
for(var key in lines)
 lines[key] = "<p>" + lines[key] + "</p>";
document.querySelector("p").outerHTML = lines.join("");
p::first-letter {
  font-weight: bold;
  color: red;
}
/* This is a quick fix to make paragraphs display same as if the were all part of the same one, alter this based on your page style and your needs. */
p + p {
  margin-top: -16px;
}
<p>This is a line 
This is another line 
This is yet another line</p>
Run codeHide result
+2
source

Source: https://habr.com/ru/post/1680752/


All Articles