Web Site Title - Apply Difierent Color to Each LetterHow to dynamically change a web page's title?Style HTML element based on its title using CSSCSS: cascading on :hover?Is it possible to set css properties by id prefix?Can I display an inner div with an independent stylesheet?CSS selector of a parent node which has a certain descendant?Cannot display HTML stringHow to parse the data which contains Html tags like style, color,font family, fontsize and set in textview in androidWhat would be the best way to color each letter of a webpage randomly with JavaScript?Possible to set CSS font-size as a relative % of what the class is defined with using style property?
How does Query decide the order in which the functions are applied?
Using font to highlight a god's speech in dialogue
Is Borg adaptation only temporary?
How to run a command 1 out of N times in Bash
Why do fuses burn at a specific current?
Am I required to correct my opponent's assumptions about my morph creatures?
German equivalent to "going down the rabbit hole"
Calculate Landau's function
To minimize the Hausdorff distance between convex polygonal regions
Could a complex system of reaction wheels be used to propel a spacecraft?
"slights on his personal appearance were, it seemed, as nothing to the danger he had spotted" meaning
What caused the end of cybernetic implants?
What are the electrical characteristics of a PC gameport?
Cheap oscilloscope showing 16 MHz square wave
Is the equational theory of groups axiomatized by the associative law?
An alternative to "two column" geometry proofs
Displaying Time in HH:MM Format
Sum and average calculator
Why are CEOs generally fired rather being demoted?
Why does the U.S. military maintain their own weather satellites?
How to save money by shopping at a variety of grocery stores?
'spazieren' - walking in a silly and affected manner?
Do universities maintain secret textbooks?
Why do presidential pardons exist in a country having a clear separation of powers?
Web Site Title - Apply Difierent Color to Each Letter
How to dynamically change a web page's title?Style HTML element based on its title using CSSCSS: cascading on :hover?Is it possible to set css properties by id prefix?Can I display an inner div with an independent stylesheet?CSS selector of a parent node which has a certain descendant?Cannot display HTML stringHow to parse the data which contains Html tags like style, color,font family, fontsize and set in textview in androidWhat would be the best way to color each letter of a webpage randomly with JavaScript?Possible to set CSS font-size as a relative % of what the class is defined with using style property?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Attempting to apply CSS styles using javascript to the title of the web page. I can't change the overall structure of the XMHTL, but I am allowed to add CSS and Java to change the appearance.
I want to apply a different color to each letter in the site's title.
I've tried this example:
https://codepen.io/tomhodgins/pen/YJZyPr
I attempted to add a class to an existing element to use the above example. I tried adding the class using a var statement and by directly altering the XML template. I've also tried a couple of other examples here that target individual letters within an element.
I need to apply individual CSS color to each individual letter in the h1 tag inside the Header1 tag.
<div class="widget Header" data-version="2" id="Header1">
<div class="header-widget">
<div>
<h1>
MyTitle
</h1>
</div>
<p>
</p>
</div>
</div>
I want each letter of MyTitle to be a different color.
This works to change all the text to red; for example, but I haven't been able to target each letter individually yet.
#Header1 h1
color: #900;
EDIT:
The title is located with a widget which may or may not allow editing. Editing it may corrupt the template.
<b:section id='header' name='Header' showaddelement='false'>
<b:widget id='Header1' locked='true' title='MyTitle (Header)' type='Header' visible='true'>
<b:widget-settings>
<b:widget-setting name='displayUrl'/>
<b:widget-setting name='displayHeight'>0</b:widget-setting>
<b:widget-setting name='sectionWidth'>-1</b:widget-setting>
<b:widget-setting name='useImage'>false</b:widget-setting>
<b:widget-setting name='shrinkToFit'>false</b:widget-setting>
<b:widget-setting name='imagePlacement'>BEHIND</b:widget-setting>
<b:widget-setting name='displayWidth'>0</b:widget-setting>
</b:widget-settings>
<b:includable id='main' var='this'>
<div class='header-widget'>
<b:include cond='data:imagePlacement in "REPLACE", "BEFORE_DESCRIPTION"' name='image'/>
<b:include cond='data:imagePlacement not in "REPLACE", "BEFORE_DESCRIPTION"' name='title'/>
<b:include cond='data:imagePlacement != "REPLACE"' name='description'/>
</div>
<b:include cond='data:imagePlacement == "BEHIND"' name='behindImageStyle'/>
</b:includable>
<b:includable id='behindImageStyle'>
<b:if cond='data:sourceUrl'>
<b:include cond='data:this.image' data=' image: data:this.image, selector: ".header-widget" ' name='responsiveImageStyle'/>
<style type='text/css'>
.header-widget
background-position: <data:blog.locale.languageAlignment/>;
background-repeat: no-repeat;
</style>
</b:if>
</b:includable>
<b:includable id='description'>
<p>
<data:this.description/>
</p>
</b:includable>
<b:includable id='image'>
<b:include name='super.image'/>
<!-- If we are replacing the title, force it to render anyway, and it'll be hidden in CSS. -->
<b:include cond='data:this.imagePlacement == "REPLACE"' name='title'/>
</b:includable>
<b:includable id='title'>
<div>
<b:class cond='data:this.imagePlacement == "REPLACE"' name='replaced'/>
<b:include name='super.title'/>
</div>
</b:includable>
</b:widget>
</b:section>
ADDITIONAL EDIT:
I forgot to mention that the site's main title is also a link to the home page. The original answer below did not take this link into account thus the link was removed. I modified the original answer by reinserting the link to the home page. The code below uses a dummy URL.
<script>
const h1 = document.querySelector('#Header1 h1'),
colors = ['#E53238', '#0064D3', '#F5AF02', '#86B817'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = "<a href="https://mybigcoolsitetosee.com/">" + html + "</a>"
</script>
javascript html css
add a comment |
Attempting to apply CSS styles using javascript to the title of the web page. I can't change the overall structure of the XMHTL, but I am allowed to add CSS and Java to change the appearance.
I want to apply a different color to each letter in the site's title.
I've tried this example:
https://codepen.io/tomhodgins/pen/YJZyPr
I attempted to add a class to an existing element to use the above example. I tried adding the class using a var statement and by directly altering the XML template. I've also tried a couple of other examples here that target individual letters within an element.
I need to apply individual CSS color to each individual letter in the h1 tag inside the Header1 tag.
<div class="widget Header" data-version="2" id="Header1">
<div class="header-widget">
<div>
<h1>
MyTitle
</h1>
</div>
<p>
</p>
</div>
</div>
I want each letter of MyTitle to be a different color.
This works to change all the text to red; for example, but I haven't been able to target each letter individually yet.
#Header1 h1
color: #900;
EDIT:
The title is located with a widget which may or may not allow editing. Editing it may corrupt the template.
<b:section id='header' name='Header' showaddelement='false'>
<b:widget id='Header1' locked='true' title='MyTitle (Header)' type='Header' visible='true'>
<b:widget-settings>
<b:widget-setting name='displayUrl'/>
<b:widget-setting name='displayHeight'>0</b:widget-setting>
<b:widget-setting name='sectionWidth'>-1</b:widget-setting>
<b:widget-setting name='useImage'>false</b:widget-setting>
<b:widget-setting name='shrinkToFit'>false</b:widget-setting>
<b:widget-setting name='imagePlacement'>BEHIND</b:widget-setting>
<b:widget-setting name='displayWidth'>0</b:widget-setting>
</b:widget-settings>
<b:includable id='main' var='this'>
<div class='header-widget'>
<b:include cond='data:imagePlacement in "REPLACE", "BEFORE_DESCRIPTION"' name='image'/>
<b:include cond='data:imagePlacement not in "REPLACE", "BEFORE_DESCRIPTION"' name='title'/>
<b:include cond='data:imagePlacement != "REPLACE"' name='description'/>
</div>
<b:include cond='data:imagePlacement == "BEHIND"' name='behindImageStyle'/>
</b:includable>
<b:includable id='behindImageStyle'>
<b:if cond='data:sourceUrl'>
<b:include cond='data:this.image' data=' image: data:this.image, selector: ".header-widget" ' name='responsiveImageStyle'/>
<style type='text/css'>
.header-widget
background-position: <data:blog.locale.languageAlignment/>;
background-repeat: no-repeat;
</style>
</b:if>
</b:includable>
<b:includable id='description'>
<p>
<data:this.description/>
</p>
</b:includable>
<b:includable id='image'>
<b:include name='super.image'/>
<!-- If we are replacing the title, force it to render anyway, and it'll be hidden in CSS. -->
<b:include cond='data:this.imagePlacement == "REPLACE"' name='title'/>
</b:includable>
<b:includable id='title'>
<div>
<b:class cond='data:this.imagePlacement == "REPLACE"' name='replaced'/>
<b:include name='super.title'/>
</div>
</b:includable>
</b:widget>
</b:section>
ADDITIONAL EDIT:
I forgot to mention that the site's main title is also a link to the home page. The original answer below did not take this link into account thus the link was removed. I modified the original answer by reinserting the link to the home page. The code below uses a dummy URL.
<script>
const h1 = document.querySelector('#Header1 h1'),
colors = ['#E53238', '#0064D3', '#F5AF02', '#86B817'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = "<a href="https://mybigcoolsitetosee.com/">" + html + "</a>"
</script>
javascript html css
add a comment |
Attempting to apply CSS styles using javascript to the title of the web page. I can't change the overall structure of the XMHTL, but I am allowed to add CSS and Java to change the appearance.
I want to apply a different color to each letter in the site's title.
I've tried this example:
https://codepen.io/tomhodgins/pen/YJZyPr
I attempted to add a class to an existing element to use the above example. I tried adding the class using a var statement and by directly altering the XML template. I've also tried a couple of other examples here that target individual letters within an element.
I need to apply individual CSS color to each individual letter in the h1 tag inside the Header1 tag.
<div class="widget Header" data-version="2" id="Header1">
<div class="header-widget">
<div>
<h1>
MyTitle
</h1>
</div>
<p>
</p>
</div>
</div>
I want each letter of MyTitle to be a different color.
This works to change all the text to red; for example, but I haven't been able to target each letter individually yet.
#Header1 h1
color: #900;
EDIT:
The title is located with a widget which may or may not allow editing. Editing it may corrupt the template.
<b:section id='header' name='Header' showaddelement='false'>
<b:widget id='Header1' locked='true' title='MyTitle (Header)' type='Header' visible='true'>
<b:widget-settings>
<b:widget-setting name='displayUrl'/>
<b:widget-setting name='displayHeight'>0</b:widget-setting>
<b:widget-setting name='sectionWidth'>-1</b:widget-setting>
<b:widget-setting name='useImage'>false</b:widget-setting>
<b:widget-setting name='shrinkToFit'>false</b:widget-setting>
<b:widget-setting name='imagePlacement'>BEHIND</b:widget-setting>
<b:widget-setting name='displayWidth'>0</b:widget-setting>
</b:widget-settings>
<b:includable id='main' var='this'>
<div class='header-widget'>
<b:include cond='data:imagePlacement in "REPLACE", "BEFORE_DESCRIPTION"' name='image'/>
<b:include cond='data:imagePlacement not in "REPLACE", "BEFORE_DESCRIPTION"' name='title'/>
<b:include cond='data:imagePlacement != "REPLACE"' name='description'/>
</div>
<b:include cond='data:imagePlacement == "BEHIND"' name='behindImageStyle'/>
</b:includable>
<b:includable id='behindImageStyle'>
<b:if cond='data:sourceUrl'>
<b:include cond='data:this.image' data=' image: data:this.image, selector: ".header-widget" ' name='responsiveImageStyle'/>
<style type='text/css'>
.header-widget
background-position: <data:blog.locale.languageAlignment/>;
background-repeat: no-repeat;
</style>
</b:if>
</b:includable>
<b:includable id='description'>
<p>
<data:this.description/>
</p>
</b:includable>
<b:includable id='image'>
<b:include name='super.image'/>
<!-- If we are replacing the title, force it to render anyway, and it'll be hidden in CSS. -->
<b:include cond='data:this.imagePlacement == "REPLACE"' name='title'/>
</b:includable>
<b:includable id='title'>
<div>
<b:class cond='data:this.imagePlacement == "REPLACE"' name='replaced'/>
<b:include name='super.title'/>
</div>
</b:includable>
</b:widget>
</b:section>
ADDITIONAL EDIT:
I forgot to mention that the site's main title is also a link to the home page. The original answer below did not take this link into account thus the link was removed. I modified the original answer by reinserting the link to the home page. The code below uses a dummy URL.
<script>
const h1 = document.querySelector('#Header1 h1'),
colors = ['#E53238', '#0064D3', '#F5AF02', '#86B817'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = "<a href="https://mybigcoolsitetosee.com/">" + html + "</a>"
</script>
javascript html css
Attempting to apply CSS styles using javascript to the title of the web page. I can't change the overall structure of the XMHTL, but I am allowed to add CSS and Java to change the appearance.
I want to apply a different color to each letter in the site's title.
I've tried this example:
https://codepen.io/tomhodgins/pen/YJZyPr
I attempted to add a class to an existing element to use the above example. I tried adding the class using a var statement and by directly altering the XML template. I've also tried a couple of other examples here that target individual letters within an element.
I need to apply individual CSS color to each individual letter in the h1 tag inside the Header1 tag.
<div class="widget Header" data-version="2" id="Header1">
<div class="header-widget">
<div>
<h1>
MyTitle
</h1>
</div>
<p>
</p>
</div>
</div>
I want each letter of MyTitle to be a different color.
This works to change all the text to red; for example, but I haven't been able to target each letter individually yet.
#Header1 h1
color: #900;
EDIT:
The title is located with a widget which may or may not allow editing. Editing it may corrupt the template.
<b:section id='header' name='Header' showaddelement='false'>
<b:widget id='Header1' locked='true' title='MyTitle (Header)' type='Header' visible='true'>
<b:widget-settings>
<b:widget-setting name='displayUrl'/>
<b:widget-setting name='displayHeight'>0</b:widget-setting>
<b:widget-setting name='sectionWidth'>-1</b:widget-setting>
<b:widget-setting name='useImage'>false</b:widget-setting>
<b:widget-setting name='shrinkToFit'>false</b:widget-setting>
<b:widget-setting name='imagePlacement'>BEHIND</b:widget-setting>
<b:widget-setting name='displayWidth'>0</b:widget-setting>
</b:widget-settings>
<b:includable id='main' var='this'>
<div class='header-widget'>
<b:include cond='data:imagePlacement in "REPLACE", "BEFORE_DESCRIPTION"' name='image'/>
<b:include cond='data:imagePlacement not in "REPLACE", "BEFORE_DESCRIPTION"' name='title'/>
<b:include cond='data:imagePlacement != "REPLACE"' name='description'/>
</div>
<b:include cond='data:imagePlacement == "BEHIND"' name='behindImageStyle'/>
</b:includable>
<b:includable id='behindImageStyle'>
<b:if cond='data:sourceUrl'>
<b:include cond='data:this.image' data=' image: data:this.image, selector: ".header-widget" ' name='responsiveImageStyle'/>
<style type='text/css'>
.header-widget
background-position: <data:blog.locale.languageAlignment/>;
background-repeat: no-repeat;
</style>
</b:if>
</b:includable>
<b:includable id='description'>
<p>
<data:this.description/>
</p>
</b:includable>
<b:includable id='image'>
<b:include name='super.image'/>
<!-- If we are replacing the title, force it to render anyway, and it'll be hidden in CSS. -->
<b:include cond='data:this.imagePlacement == "REPLACE"' name='title'/>
</b:includable>
<b:includable id='title'>
<div>
<b:class cond='data:this.imagePlacement == "REPLACE"' name='replaced'/>
<b:include name='super.title'/>
</div>
</b:includable>
</b:widget>
</b:section>
ADDITIONAL EDIT:
I forgot to mention that the site's main title is also a link to the home page. The original answer below did not take this link into account thus the link was removed. I modified the original answer by reinserting the link to the home page. The code below uses a dummy URL.
<script>
const h1 = document.querySelector('#Header1 h1'),
colors = ['#E53238', '#0064D3', '#F5AF02', '#86B817'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = "<a href="https://mybigcoolsitetosee.com/">" + html + "</a>"
</script>
javascript html css
javascript html css
edited Mar 28 at 6:18
user5671178
asked Mar 28 at 0:41
user5671178user5671178
33 bronze badges
33 bronze badges
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
Using an array of colors and adding them to inline style
const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html<div id="Header1">
<h1>
MyTitle
</h1>
</div>
Very similar to the answer I was considering, but (IMO), it would be more fun to have the colors assigned randomly.
– elbrant
Mar 28 at 1:19
Just saw this solution from @charlietfl, it its very similar the one I included in my answer, and actually, to make the colors randomly you could change the names of the collors inside the array for variables like: var color01 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); var color02 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); that would do the job...
– Adriano Marra
Mar 28 at 1:41
Thanks, that works.The script did not work when placed inside the head tag. I needed to place it below all of the content of template just above the </body></html> tags for it to override the template's code.
– user5671178
Mar 28 at 1:46
add a comment |
Using this example as you mentioned: https://codepen.io/tomhodgins/pen/YJZyPr
But instead of setting
h1.letter[--nth-letter="1"] background: red;
h1.letter[--nth-letter="2"] background: orange;
set it as
h1.letter[--nth-letter="1"] color: red;
h1.letter[--nth-letter="2"] color: orange;
and so on...
However, if you can't change the html in anyway you could do it with pure javascript like:
var title = document.querySelector('h1'),
options = ['blue', 'orange', 'yellow', 'green', 'brown', 'red', 'yellow'];
var result = title.textContent.trim().split('').map((color, i)=>
return `<span style="color:$options[i % options.length]">$ color </span>`
).join('')
title.innerHTML = result```
<div class="widget Header" data-version="2" id="Header1">
<div class="header-widget">
<div>
<h1>
MyTitle
</h1>
</div>
<p>
</p>
</div>
</div>
```
1
In order for that to work I would need to apply a new class of "letter" to the h1 tag inside the XHTML template, which doe snot appear possible. I attempted to add the class using element.classList.add but that did not work. I also added this class to area of the template for the title and that did not work.
– user5671178
Mar 28 at 1:01
Just added a solution including spans inside the H1 tag with pure JS... Maybe that is gonna work for you.
– Adriano Marra
Mar 28 at 1:25
add a comment |
You can put this code to your source, and change it what you want
const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html
And also this code:
<div id="Header1">
<h1>
MyTitle
</h1>
</div>
Thanks for your reply!
– user5671178
Mar 28 at 13:11
add a comment |
You're going to want to take advantage of the fact that you can easily convert a String into an Array.
In your case you're going to use jQuery (or vanilla js such as getElementById() ) to grab the text you want to style, and then use javascript to iterate through the string and assign a new color to each one.
For example:
var a = $("h1").text();
$("h1").empty();
for(var i = 0; i < a.length; i++)
$("h1").append("<span class = 'color"+i+"'>"+a[i]+"</span>");
https://jsfiddle.net/crt829fp/2/
1
Question isn't tagged jQuery
– charlietfl
Mar 28 at 1:01
add a comment |
Try to use the tag only.
<h1>
<span style='color: blue'>T</span>
<span style='color: green'>E</span>
<span style='color: yellow'>S</span>
<span style='color: blue'>T</span>
<span style='color: black'>E</span>
<span style='color: red'>R</span>
</h1>
OP clearly states "I can't change the overall structure of the XMHTL"
– Robby Cornelissen
Mar 28 at 1:03
1
I don't think that is possible as the title is inside a widget inside the XHTML template so I need to override it after the fact.
– user5671178
Mar 28 at 1:12
1
Simple solutions are usually the best, but I'm downvoting this because it doesn't answer the quesion. Good try though.
– elbrant
Mar 28 at 1:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388544%2fweb-site-title-apply-difierent-color-to-each-letter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using an array of colors and adding them to inline style
const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html<div id="Header1">
<h1>
MyTitle
</h1>
</div>
Very similar to the answer I was considering, but (IMO), it would be more fun to have the colors assigned randomly.
– elbrant
Mar 28 at 1:19
Just saw this solution from @charlietfl, it its very similar the one I included in my answer, and actually, to make the colors randomly you could change the names of the collors inside the array for variables like: var color01 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); var color02 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); that would do the job...
– Adriano Marra
Mar 28 at 1:41
Thanks, that works.The script did not work when placed inside the head tag. I needed to place it below all of the content of template just above the </body></html> tags for it to override the template's code.
– user5671178
Mar 28 at 1:46
add a comment |
Using an array of colors and adding them to inline style
const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html<div id="Header1">
<h1>
MyTitle
</h1>
</div>
Very similar to the answer I was considering, but (IMO), it would be more fun to have the colors assigned randomly.
– elbrant
Mar 28 at 1:19
Just saw this solution from @charlietfl, it its very similar the one I included in my answer, and actually, to make the colors randomly you could change the names of the collors inside the array for variables like: var color01 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); var color02 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); that would do the job...
– Adriano Marra
Mar 28 at 1:41
Thanks, that works.The script did not work when placed inside the head tag. I needed to place it below all of the content of template just above the </body></html> tags for it to override the template's code.
– user5671178
Mar 28 at 1:46
add a comment |
Using an array of colors and adding them to inline style
const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html<div id="Header1">
<h1>
MyTitle
</h1>
</div>Using an array of colors and adding them to inline style
const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html<div id="Header1">
<h1>
MyTitle
</h1>
</div>const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html<div id="Header1">
<h1>
MyTitle
</h1>
</div>const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html<div id="Header1">
<h1>
MyTitle
</h1>
</div>edited Mar 28 at 1:05
answered Mar 28 at 0:57
charlietflcharlietfl
147k13 gold badges98 silver badges129 bronze badges
147k13 gold badges98 silver badges129 bronze badges
Very similar to the answer I was considering, but (IMO), it would be more fun to have the colors assigned randomly.
– elbrant
Mar 28 at 1:19
Just saw this solution from @charlietfl, it its very similar the one I included in my answer, and actually, to make the colors randomly you could change the names of the collors inside the array for variables like: var color01 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); var color02 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); that would do the job...
– Adriano Marra
Mar 28 at 1:41
Thanks, that works.The script did not work when placed inside the head tag. I needed to place it below all of the content of template just above the </body></html> tags for it to override the template's code.
– user5671178
Mar 28 at 1:46
add a comment |
Very similar to the answer I was considering, but (IMO), it would be more fun to have the colors assigned randomly.
– elbrant
Mar 28 at 1:19
Just saw this solution from @charlietfl, it its very similar the one I included in my answer, and actually, to make the colors randomly you could change the names of the collors inside the array for variables like: var color01 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); var color02 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); that would do the job...
– Adriano Marra
Mar 28 at 1:41
Thanks, that works.The script did not work when placed inside the head tag. I needed to place it below all of the content of template just above the </body></html> tags for it to override the template's code.
– user5671178
Mar 28 at 1:46
Very similar to the answer I was considering, but (IMO), it would be more fun to have the colors assigned randomly.
– elbrant
Mar 28 at 1:19
Very similar to the answer I was considering, but (IMO), it would be more fun to have the colors assigned randomly.
– elbrant
Mar 28 at 1:19
Just saw this solution from @charlietfl, it its very similar the one I included in my answer, and actually, to make the colors randomly you could change the names of the collors inside the array for variables like: var color01 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); var color02 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); that would do the job...
– Adriano Marra
Mar 28 at 1:41
Just saw this solution from @charlietfl, it its very similar the one I included in my answer, and actually, to make the colors randomly you could change the names of the collors inside the array for variables like: var color01 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); var color02 = '#'+(Math.random()*0xFFFFFF<<0).toString(16); that would do the job...
– Adriano Marra
Mar 28 at 1:41
Thanks, that works.The script did not work when placed inside the head tag. I needed to place it below all of the content of template just above the </body></html> tags for it to override the template's code.
– user5671178
Mar 28 at 1:46
Thanks, that works.The script did not work when placed inside the head tag. I needed to place it below all of the content of template just above the </body></html> tags for it to override the template's code.
– user5671178
Mar 28 at 1:46
add a comment |
Using this example as you mentioned: https://codepen.io/tomhodgins/pen/YJZyPr
But instead of setting
h1.letter[--nth-letter="1"] background: red;
h1.letter[--nth-letter="2"] background: orange;
set it as
h1.letter[--nth-letter="1"] color: red;
h1.letter[--nth-letter="2"] color: orange;
and so on...
However, if you can't change the html in anyway you could do it with pure javascript like:
var title = document.querySelector('h1'),
options = ['blue', 'orange', 'yellow', 'green', 'brown', 'red', 'yellow'];
var result = title.textContent.trim().split('').map((color, i)=>
return `<span style="color:$options[i % options.length]">$ color </span>`
).join('')
title.innerHTML = result```
<div class="widget Header" data-version="2" id="Header1">
<div class="header-widget">
<div>
<h1>
MyTitle
</h1>
</div>
<p>
</p>
</div>
</div>
```
1
In order for that to work I would need to apply a new class of "letter" to the h1 tag inside the XHTML template, which doe snot appear possible. I attempted to add the class using element.classList.add but that did not work. I also added this class to area of the template for the title and that did not work.
– user5671178
Mar 28 at 1:01
Just added a solution including spans inside the H1 tag with pure JS... Maybe that is gonna work for you.
– Adriano Marra
Mar 28 at 1:25
add a comment |
Using this example as you mentioned: https://codepen.io/tomhodgins/pen/YJZyPr
But instead of setting
h1.letter[--nth-letter="1"] background: red;
h1.letter[--nth-letter="2"] background: orange;
set it as
h1.letter[--nth-letter="1"] color: red;
h1.letter[--nth-letter="2"] color: orange;
and so on...
However, if you can't change the html in anyway you could do it with pure javascript like:
var title = document.querySelector('h1'),
options = ['blue', 'orange', 'yellow', 'green', 'brown', 'red', 'yellow'];
var result = title.textContent.trim().split('').map((color, i)=>
return `<span style="color:$options[i % options.length]">$ color </span>`
).join('')
title.innerHTML = result```
<div class="widget Header" data-version="2" id="Header1">
<div class="header-widget">
<div>
<h1>
MyTitle
</h1>
</div>
<p>
</p>
</div>
</div>
```
1
In order for that to work I would need to apply a new class of "letter" to the h1 tag inside the XHTML template, which doe snot appear possible. I attempted to add the class using element.classList.add but that did not work. I also added this class to area of the template for the title and that did not work.
– user5671178
Mar 28 at 1:01
Just added a solution including spans inside the H1 tag with pure JS... Maybe that is gonna work for you.
– Adriano Marra
Mar 28 at 1:25
add a comment |
Using this example as you mentioned: https://codepen.io/tomhodgins/pen/YJZyPr
But instead of setting
h1.letter[--nth-letter="1"] background: red;
h1.letter[--nth-letter="2"] background: orange;
set it as
h1.letter[--nth-letter="1"] color: red;
h1.letter[--nth-letter="2"] color: orange;
and so on...
However, if you can't change the html in anyway you could do it with pure javascript like:
var title = document.querySelector('h1'),
options = ['blue', 'orange', 'yellow', 'green', 'brown', 'red', 'yellow'];
var result = title.textContent.trim().split('').map((color, i)=>
return `<span style="color:$options[i % options.length]">$ color </span>`
).join('')
title.innerHTML = result```
<div class="widget Header" data-version="2" id="Header1">
<div class="header-widget">
<div>
<h1>
MyTitle
</h1>
</div>
<p>
</p>
</div>
</div>
```Using this example as you mentioned: https://codepen.io/tomhodgins/pen/YJZyPr
But instead of setting
h1.letter[--nth-letter="1"] background: red;
h1.letter[--nth-letter="2"] background: orange;
set it as
h1.letter[--nth-letter="1"] color: red;
h1.letter[--nth-letter="2"] color: orange;
and so on...
However, if you can't change the html in anyway you could do it with pure javascript like:
var title = document.querySelector('h1'),
options = ['blue', 'orange', 'yellow', 'green', 'brown', 'red', 'yellow'];
var result = title.textContent.trim().split('').map((color, i)=>
return `<span style="color:$options[i % options.length]">$ color </span>`
).join('')
title.innerHTML = result```
<div class="widget Header" data-version="2" id="Header1">
<div class="header-widget">
<div>
<h1>
MyTitle
</h1>
</div>
<p>
</p>
</div>
</div>
```var title = document.querySelector('h1'),
options = ['blue', 'orange', 'yellow', 'green', 'brown', 'red', 'yellow'];
var result = title.textContent.trim().split('').map((color, i)=>
return `<span style="color:$options[i % options.length]">$ color </span>`
).join('')
title.innerHTML = result```
<div class="widget Header" data-version="2" id="Header1">
<div class="header-widget">
<div>
<h1>
MyTitle
</h1>
</div>
<p>
</p>
</div>
</div>
```var title = document.querySelector('h1'),
options = ['blue', 'orange', 'yellow', 'green', 'brown', 'red', 'yellow'];
var result = title.textContent.trim().split('').map((color, i)=>
return `<span style="color:$options[i % options.length]">$ color </span>`
).join('')
title.innerHTML = result```
<div class="widget Header" data-version="2" id="Header1">
<div class="header-widget">
<div>
<h1>
MyTitle
</h1>
</div>
<p>
</p>
</div>
</div>
```edited Mar 28 at 1:20
answered Mar 28 at 0:47
Adriano MarraAdriano Marra
1196 bronze badges
1196 bronze badges
1
In order for that to work I would need to apply a new class of "letter" to the h1 tag inside the XHTML template, which doe snot appear possible. I attempted to add the class using element.classList.add but that did not work. I also added this class to area of the template for the title and that did not work.
– user5671178
Mar 28 at 1:01
Just added a solution including spans inside the H1 tag with pure JS... Maybe that is gonna work for you.
– Adriano Marra
Mar 28 at 1:25
add a comment |
1
In order for that to work I would need to apply a new class of "letter" to the h1 tag inside the XHTML template, which doe snot appear possible. I attempted to add the class using element.classList.add but that did not work. I also added this class to area of the template for the title and that did not work.
– user5671178
Mar 28 at 1:01
Just added a solution including spans inside the H1 tag with pure JS... Maybe that is gonna work for you.
– Adriano Marra
Mar 28 at 1:25
1
1
In order for that to work I would need to apply a new class of "letter" to the h1 tag inside the XHTML template, which doe snot appear possible. I attempted to add the class using element.classList.add but that did not work. I also added this class to area of the template for the title and that did not work.
– user5671178
Mar 28 at 1:01
In order for that to work I would need to apply a new class of "letter" to the h1 tag inside the XHTML template, which doe snot appear possible. I attempted to add the class using element.classList.add but that did not work. I also added this class to area of the template for the title and that did not work.
– user5671178
Mar 28 at 1:01
Just added a solution including spans inside the H1 tag with pure JS... Maybe that is gonna work for you.
– Adriano Marra
Mar 28 at 1:25
Just added a solution including spans inside the H1 tag with pure JS... Maybe that is gonna work for you.
– Adriano Marra
Mar 28 at 1:25
add a comment |
You can put this code to your source, and change it what you want
const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html
And also this code:
<div id="Header1">
<h1>
MyTitle
</h1>
</div>
Thanks for your reply!
– user5671178
Mar 28 at 13:11
add a comment |
You can put this code to your source, and change it what you want
const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html
And also this code:
<div id="Header1">
<h1>
MyTitle
</h1>
</div>
Thanks for your reply!
– user5671178
Mar 28 at 13:11
add a comment |
You can put this code to your source, and change it what you want
const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html
And also this code:
<div id="Header1">
<h1>
MyTitle
</h1>
</div>
You can put this code to your source, and change it what you want
const h1 = document.querySelector('#Header1 h1'),
colors = ['red', 'green', 'blue', 'orange', 'purple', 'pink', 'yellow'];
const html = h1.textContent.trim().split('').map((s, i)=>
return `<span style="color:$colors[i % colors.length]">$s</span>`
).join('')
h1.innerHTML = html
And also this code:
<div id="Header1">
<h1>
MyTitle
</h1>
</div>
answered Mar 28 at 1:33
user11195047
Thanks for your reply!
– user5671178
Mar 28 at 13:11
add a comment |
Thanks for your reply!
– user5671178
Mar 28 at 13:11
Thanks for your reply!
– user5671178
Mar 28 at 13:11
Thanks for your reply!
– user5671178
Mar 28 at 13:11
add a comment |
You're going to want to take advantage of the fact that you can easily convert a String into an Array.
In your case you're going to use jQuery (or vanilla js such as getElementById() ) to grab the text you want to style, and then use javascript to iterate through the string and assign a new color to each one.
For example:
var a = $("h1").text();
$("h1").empty();
for(var i = 0; i < a.length; i++)
$("h1").append("<span class = 'color"+i+"'>"+a[i]+"</span>");
https://jsfiddle.net/crt829fp/2/
1
Question isn't tagged jQuery
– charlietfl
Mar 28 at 1:01
add a comment |
You're going to want to take advantage of the fact that you can easily convert a String into an Array.
In your case you're going to use jQuery (or vanilla js such as getElementById() ) to grab the text you want to style, and then use javascript to iterate through the string and assign a new color to each one.
For example:
var a = $("h1").text();
$("h1").empty();
for(var i = 0; i < a.length; i++)
$("h1").append("<span class = 'color"+i+"'>"+a[i]+"</span>");
https://jsfiddle.net/crt829fp/2/
1
Question isn't tagged jQuery
– charlietfl
Mar 28 at 1:01
add a comment |
You're going to want to take advantage of the fact that you can easily convert a String into an Array.
In your case you're going to use jQuery (or vanilla js such as getElementById() ) to grab the text you want to style, and then use javascript to iterate through the string and assign a new color to each one.
For example:
var a = $("h1").text();
$("h1").empty();
for(var i = 0; i < a.length; i++)
$("h1").append("<span class = 'color"+i+"'>"+a[i]+"</span>");
https://jsfiddle.net/crt829fp/2/
You're going to want to take advantage of the fact that you can easily convert a String into an Array.
In your case you're going to use jQuery (or vanilla js such as getElementById() ) to grab the text you want to style, and then use javascript to iterate through the string and assign a new color to each one.
For example:
var a = $("h1").text();
$("h1").empty();
for(var i = 0; i < a.length; i++)
$("h1").append("<span class = 'color"+i+"'>"+a[i]+"</span>");
https://jsfiddle.net/crt829fp/2/
answered Mar 28 at 0:58
matthewjguntonmatthewjgunton
211 bronze badge
211 bronze badge
1
Question isn't tagged jQuery
– charlietfl
Mar 28 at 1:01
add a comment |
1
Question isn't tagged jQuery
– charlietfl
Mar 28 at 1:01
1
1
Question isn't tagged jQuery
– charlietfl
Mar 28 at 1:01
Question isn't tagged jQuery
– charlietfl
Mar 28 at 1:01
add a comment |
Try to use the tag only.
<h1>
<span style='color: blue'>T</span>
<span style='color: green'>E</span>
<span style='color: yellow'>S</span>
<span style='color: blue'>T</span>
<span style='color: black'>E</span>
<span style='color: red'>R</span>
</h1>
OP clearly states "I can't change the overall structure of the XMHTL"
– Robby Cornelissen
Mar 28 at 1:03
1
I don't think that is possible as the title is inside a widget inside the XHTML template so I need to override it after the fact.
– user5671178
Mar 28 at 1:12
1
Simple solutions are usually the best, but I'm downvoting this because it doesn't answer the quesion. Good try though.
– elbrant
Mar 28 at 1:25
add a comment |
Try to use the tag only.
<h1>
<span style='color: blue'>T</span>
<span style='color: green'>E</span>
<span style='color: yellow'>S</span>
<span style='color: blue'>T</span>
<span style='color: black'>E</span>
<span style='color: red'>R</span>
</h1>
OP clearly states "I can't change the overall structure of the XMHTL"
– Robby Cornelissen
Mar 28 at 1:03
1
I don't think that is possible as the title is inside a widget inside the XHTML template so I need to override it after the fact.
– user5671178
Mar 28 at 1:12
1
Simple solutions are usually the best, but I'm downvoting this because it doesn't answer the quesion. Good try though.
– elbrant
Mar 28 at 1:25
add a comment |
Try to use the tag only.
<h1>
<span style='color: blue'>T</span>
<span style='color: green'>E</span>
<span style='color: yellow'>S</span>
<span style='color: blue'>T</span>
<span style='color: black'>E</span>
<span style='color: red'>R</span>
</h1>Try to use the tag only.
<h1>
<span style='color: blue'>T</span>
<span style='color: green'>E</span>
<span style='color: yellow'>S</span>
<span style='color: blue'>T</span>
<span style='color: black'>E</span>
<span style='color: red'>R</span>
</h1> <h1>
<span style='color: blue'>T</span>
<span style='color: green'>E</span>
<span style='color: yellow'>S</span>
<span style='color: blue'>T</span>
<span style='color: black'>E</span>
<span style='color: red'>R</span>
</h1> <h1>
<span style='color: blue'>T</span>
<span style='color: green'>E</span>
<span style='color: yellow'>S</span>
<span style='color: blue'>T</span>
<span style='color: black'>E</span>
<span style='color: red'>R</span>
</h1>answered Mar 28 at 0:51
Renato AlmeidaRenato Almeida
12 bronze badges
12 bronze badges
OP clearly states "I can't change the overall structure of the XMHTL"
– Robby Cornelissen
Mar 28 at 1:03
1
I don't think that is possible as the title is inside a widget inside the XHTML template so I need to override it after the fact.
– user5671178
Mar 28 at 1:12
1
Simple solutions are usually the best, but I'm downvoting this because it doesn't answer the quesion. Good try though.
– elbrant
Mar 28 at 1:25
add a comment |
OP clearly states "I can't change the overall structure of the XMHTL"
– Robby Cornelissen
Mar 28 at 1:03
1
I don't think that is possible as the title is inside a widget inside the XHTML template so I need to override it after the fact.
– user5671178
Mar 28 at 1:12
1
Simple solutions are usually the best, but I'm downvoting this because it doesn't answer the quesion. Good try though.
– elbrant
Mar 28 at 1:25
OP clearly states "I can't change the overall structure of the XMHTL"
– Robby Cornelissen
Mar 28 at 1:03
OP clearly states "I can't change the overall structure of the XMHTL"
– Robby Cornelissen
Mar 28 at 1:03
1
1
I don't think that is possible as the title is inside a widget inside the XHTML template so I need to override it after the fact.
– user5671178
Mar 28 at 1:12
I don't think that is possible as the title is inside a widget inside the XHTML template so I need to override it after the fact.
– user5671178
Mar 28 at 1:12
1
1
Simple solutions are usually the best, but I'm downvoting this because it doesn't answer the quesion. Good try though.
– elbrant
Mar 28 at 1:25
Simple solutions are usually the best, but I'm downvoting this because it doesn't answer the quesion. Good try though.
– elbrant
Mar 28 at 1:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388544%2fweb-site-title-apply-difierent-color-to-each-letter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown