Is there any clean CSS method to make each letter in a word a different color?
Here is some JavaScript.
var message = "The quick brown fox.";var colors = new Array("#ff0000","#00ff00","#0000ff"); // red, green, blue
for (var i = 0; i < message.length; i++){ document.write("<span style=\"color:" + colors[(i % colors.length)] + ";\">" + message[i] + "</span>");}
Specific letters that have each specific colors?
Assuming you are using UILabel
named myLabel
:
let str = "This is a string"
let colors:[UIColor] = [
.blackColor(), // T
.darkGrayColor(), // h
.lightGrayColor(), // i
.grayColor(), // s
.clearColor(), //
.redColor(), // i
.greenColor(), // s
.clearColor(), //
.blueColor(), // a
.clearColor(), //
.cyanColor(), // S
.yellowColor(), // t
.magentaColor(), // r
.orangeColor(), // i
.purpleColor(), // n
.brownColor(), // g
]
let attrStr = NSMutableAttributedString()
for (chr, color) in Zip2(str, colors) {
attrStr.appendAttributedString(
NSAttributedString(string: String(chr), attributes: [NSForegroundColorAttributeName:color])
)
}
myLabel.attributedText = attrStr
How to random color half of the words within h2 tag?
here is the clean html code that generated by slideshow
<div class="slides-container">
<h2>
<span>he secret of getting ahead is</span>
getting started.
<span>-Mark Twain </span>
</h2>
<h2>
<span>You are never too old</span>
to set another goal or to dream a new dream.
<span>-C.S.Lewis</span>
</h2>
<h2>
<span>
If you can dream it</span>, you can do it.
<span>-Walt Disney</span>
</h2>
</div>
this array defined colors
var colors = ['red', 'green', 'blue', 'orange', 'yellow'];
Change the first Span
color in each H2
Tags :
$('.slides-container h2 span:first-child').each(function(){
$(this).css(
'color',
colors[Math.floor(Math.random() * colors.length)]);
});
JSFIDDLE V2
Style certain characters with CSS
That's not possible in CSS. Your only option would be first-letter
and that's not going to cut it. Either use JavaScript or (as you stated in your comments), your server-side language.
Related Topics
Use Custom Fonts with Wkhtmltopdf
Css: How to Change Colour of Active Navigation Page Menu
Compiling and Maintaining Ie-Specific Stylesheets
How to Create a Sticky Footer That Plays Well with Bootstrap 3
Change New Google Recaptcha (V2) Width
Chrome & Safari Error::Not Allowed to Load Local Resource: File:///D:/Css/Style.Css
Linear-Gradient to Transparent Bug in Latest Safari
Fix Custom Font Line-Height with CSS
Is Vertical Text-Overflow Possible with CSS3
How to Add Multiple CSS Gradient as a Multiple Background
CSS Background Image Url Failing to Load
Fonts Are Not Rendered Correctly in Release Mode, But Is Working on Debug Mode in Asp Net Webforms
Difference Between CSS3 Transitions' Ease-In and Ease-Out