+ 1
Writing a sentence with random letter color in html
How Writing a sentence with random letter color?? Or Can i just use <span> tag for every letter??😊
5 ответов
+ 3
Use this JS
let sentence = "Your sentence"
let colors = ['#ff0000', '#00ff00', '#0000ff'];
for(let letter in sentence){
element.innerHTML += `<span color="#${colors[Math.floor(Math.random() * colors.length)]}">${letter}</span>`;
}
If you need explanation feel free to ask
Also please comment if not working
+ 3
Yeah, you can but it is soooo bulky and manually hard and boring to do. You have to do
<span color="color1">A</span><span color="color2">B</span><span color="color3">C</span>
Etc.
+ 1
Thank you, whenever I've tested the code, I report to you
-html alone can't do this?
+ 1
Oh, that's right
0
hmm better way is
client-side JavaScript,
var colors = ['#ff0000', '#00ff00', '#0000ff'];
var random_color = colors[Math.floor(Math.random() * colors.length)];
document.getElementById('title').style.color = random_color;
JQuery would be along the lines of
$('#title').css('color', random_color);