I have some span
with some words in it, like
<span id="phrase">Here are some words</span>
I need to color all the 'e'
characters red.
I think of taking the span.innerText property
, remove text node from span element and add some more spans inside (or instead), and give them necessary style.
Is it the only way, or it could be solved in a more elegant way?
Make separate elements Another way is creating a text with separate elements by using the HTML <span> tag, which is an empty container. This means that you create a multicolor text by putting each letter in a separate element to define a different color for each letter of your text.
To set the font color in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property color. HTML5 do not support the <font> tag, so the CSS style is used to add font color.
Steps to add multicolor into text: Add a simple text inside the <div> tag with the required selector. Apply the linear gradient property with any colors of your choice. Apply webkit properties that will fill the text with the gradient background and declare the color property with transparent background.
Use the <span> element instead. There's no <repeat> tag in HTML, unless you use JavaScript to create such an element in order to select that in CSS.
You can surely do it with javascript:
var span = document.getElementById('phrase'),
text = span.innerHTML.split('').map(function(el) {
return '<span class="char-' + el.toLowerCase() + '">' + el + '</span>';
}).join('');
span.innerHTML = text;
.char-e {
color: red;
}
<span id="phrase">Here are some words</span>
For convenience it wraps each character with a span with corresponding class name, which makes it easy to assign individual styles.
Warning: However I would not recommend doing this with large texts because the code above replaces innerHTML
it can break your HTML if it contains other nested elements. But for small titles with only text this is not going to be a problem.
If you want to work with more complex HTML markup (with children elements) the function needs to be improved to work recursively on child items text content.
More complex implementation (which contain dfsq code) which keep tags:
function addColor(domNode, color, letter){
if(domNode.nodeType!=1||!domNode.hasAttribute('edited')){
if(domNode.nodeType==3){
var newText=document.createElement('span');
newText.innerHTML=domNode.textContent;
newText.setAttribute('edited', true);
var text = newText.innerHTML.split('').map(function(el){
if(el==letter){
return '<i style=\"color:'+color+'\">'+el+'</i>';
}
else{
return el;
}
}).join('');
newText.innerHTML=text;
domNode.parentNode.replaceChild(newText,domNode);
}
for(var i=0; i<domNode.childNodes.length;i++){
addColor(domNode.childNodes[i], color, letter);
}
}
}
addColor(document.getElementById('phrase'), 'red', 'e');
<span id="phrase">Here are <a href="#"> test</a> some text <p> some text again </p> some woreds</span>
With a fiddle: http://jsfiddle.net/e6c3yy0j/
I think it's not so ugly to use javascript :
$(document).ready(function() {
console.log( "ready!" );
var text = $("#phrase").html().replace(/e/g, '<span class="red">e</span>');
$("#phrase").html(text)
});
jsfiddle example
You can't target specific letters in a text (except for the first one using :first-letter
). So yes, your only option is to wrap them with an element. A span
would work, the guys from FontAwesome like to use i
instead.
So, for example, you could do this:
<span id="phrase">H<i>e</i>r<i>e</i> are som<i>e</i> words</span>
And add this to your CSS:
.phrase i {
font-style: normal; /* Because I is italic by default */
color: red;
}
Split each character in the string and if the character is 'e'
, then give the red color using fontcolor
function.
var s1 = document.getElementById("phrase");
var str = s1.innerHTML;
var newText = "";
for (var i = 0; i < str.length; i++) {
if (str[i] == 'e') {
newText+= str.charAt(i).fontcolor("red");
}
else {
newText += str[i];
}
}
s1.innerHTML = newText;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With