Writing a sentence with random letter color in html | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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??😊

10th Feb 2019, 2:57 PM
<Amirhs/>
<Amirhs/> - avatar
5 Answers
+ 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
10th Feb 2019, 5:04 PM
Maneren
Maneren - avatar
+ 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.
10th Feb 2019, 6:40 PM
Maneren
Maneren - avatar
+ 1
Thank you, whenever I've tested the code, I report to you -html alone can't do this?
10th Feb 2019, 6:37 PM
<Amirhs/>
<Amirhs/> - avatar
+ 1
Oh, that's right
10th Feb 2019, 6:43 PM
<Amirhs/>
<Amirhs/> - avatar
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);
10th Feb 2019, 3:04 PM
peter
peter - avatar