1

I'm a bit confused as to how I can go about this. I tried: h1:nth-child(odd) { color: orange; }
but that didn't work.

Here's my html:

<body>
  <div class="header">
    <header>
      <h1> Simple & Responsive Flexbox Website! </h1>
    </header>
  </div>
</body>
Sam
  • 895
  • 1
  • 8
  • 26
codfish555
  • 96
  • 1
  • 15

4 Answers4

3

var myColors = ["red", "green"];
$('.text-content').find('h1').each(function(){
  var $el = $(this),
    text = $el.text(),
    len = text.length,
    coLen = myColors.length,
    newCont = '';
  for(var i=0; i<len; i++){
    newCont += '<span style="color:'+ myColors[i%coLen] +'">'+ text.charAt(i) +'</span>';
  }
  $el.html(newCont);    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="text-content">
    <h1>Simple & Responsive Flexbox Website!</h1>
</span>
2

See below. You can used http://letteringjs.com/ if you like to wrap characters in span tags.

h1>span {
color: blue;
}
h1>span:nth-child(odd) {
color: red;
}
<h1>
  <span>S</span>
  <span>i</span>
  <span>m</span>
  <span>p</span>
  <span>l</span>
  <span>e</span>
</h1>
Gerard
  • 15,418
  • 5
  • 30
  • 52
0

You would need to wrap each letter in individual spans (if you wanted to only use HTML), then target the :odd spans.

<style> h1 span:odd { color: orange; } </style>

<h1>

<span>S</span>

<span>i</span>

<span>m</span>

<span>p</span>

<span>l</span>

<span>e</span>

</h1>

Using javascript might open up some other opportunities to make this more efficient, or simpler to write, but will likely still use the span method to delegate the color property to individual characters.

Jimmy Amash
  • 106
  • 6
0

Here is an idea that will only work with monospace font:

h1 {
  font-size: 20px;
  font-family: monospace;
  background: repeating-linear-gradient(to right, orange 0, orange 1ch, black 1ch, black 2ch);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
<h1> Simple & Responsive Flexbox Website!</h1>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415