How to make a buttons with colors of the rainbow in my cycle in html?
My project is made on jango framework.
{% for tag in tags %}
<div class="btngroup" role="group" aria-label="tags">
<h3><a href="{{ tag.get_absolute_url }}" class="btn btn-primary">{{tag.title}}</a></h3>
</div>
{% endfor %}
I will explain a little. In this cycle, I get link tags from the database, which I want to wrap in buttons of different colors of the rainbow. But how to do that ?
javascript python html css django
add a comment |
My project is made on jango framework.
{% for tag in tags %}
<div class="btngroup" role="group" aria-label="tags">
<h3><a href="{{ tag.get_absolute_url }}" class="btn btn-primary">{{tag.title}}</a></h3>
</div>
{% endfor %}
I will explain a little. In this cycle, I get link tags from the database, which I want to wrap in buttons of different colors of the rainbow. But how to do that ?
javascript python html css django
add a comment |
My project is made on jango framework.
{% for tag in tags %}
<div class="btngroup" role="group" aria-label="tags">
<h3><a href="{{ tag.get_absolute_url }}" class="btn btn-primary">{{tag.title}}</a></h3>
</div>
{% endfor %}
I will explain a little. In this cycle, I get link tags from the database, which I want to wrap in buttons of different colors of the rainbow. But how to do that ?
javascript python html css django
My project is made on jango framework.
{% for tag in tags %}
<div class="btngroup" role="group" aria-label="tags">
<h3><a href="{{ tag.get_absolute_url }}" class="btn btn-primary">{{tag.title}}</a></h3>
</div>
{% endfor %}
I will explain a little. In this cycle, I get link tags from the database, which I want to wrap in buttons of different colors of the rainbow. But how to do that ?
javascript python html css django
javascript python html css django
asked Nov 20 at 21:03
DieMauer
11
11
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use HSL to easily create a color based on the item index and the total items in your array. H is for Hue, which is from 0 to 360. 0 is red, 120 is green, 240 is blue. You can simply calculate the percentage of 360 based on the index of the item in the loop.
So first, you need to create a filter because you won't be able to do calculations directly in a Django template. Create a folder named templatetags
with two files inside: an empty __init__.py
and rainbow.py
with the following content:
from django import template
register = template.Library()
@register.filter()
def rainbow(i, array):
return 360 * i / len(array)
Then, modify your code like this, so the background is changed with CSS, defining HSL colors. Make sure to include the filter:
{% load rainbow %}
{% for tag in tags %}
<div class="btngroup" role="group" aria-label="tags">
<h3>
<a href="{{ tag.get_absolute_url }}" style="background:hsl({{ forloop.counter|rainbow:tags }},80%,50%)" class="btn btn-primary">{{tag.title}}</a>
</h3>
</div>
{% endfor %}
forloop.counter
in Django will get the index of the current item in the loop. 80% is for S(saturation) and 50% for L(lightness). You might want to fine-tune this to match your design.
Live example: https://repl.it/repls/AbleOtherBytecode
Good day! Can you explain in more detail where to create this file with an array and whether it is necessary to specify it in applications ?
– DieMauer
Nov 21 at 21:54
Hi, updated the answer with a live example and details how to create a filter.
– passatgt
Nov 22 at 9:59
Bingo !!! it's work! Thank you very much ! you helped me!
– DieMauer
Nov 22 at 16:12
add a comment |
You can use background-image: linear-gradient on your .btngroup class. Set the height and width to what you prefer:
.btngroup {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 50px;
background-image: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
border-radius:4px;
color: white;
}
<div class="btngroup">Button<div>
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%2f53401492%2fhow-to-make-a-buttons-with-colors-of-the-rainbow-in-my-cycle-in-html%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use HSL to easily create a color based on the item index and the total items in your array. H is for Hue, which is from 0 to 360. 0 is red, 120 is green, 240 is blue. You can simply calculate the percentage of 360 based on the index of the item in the loop.
So first, you need to create a filter because you won't be able to do calculations directly in a Django template. Create a folder named templatetags
with two files inside: an empty __init__.py
and rainbow.py
with the following content:
from django import template
register = template.Library()
@register.filter()
def rainbow(i, array):
return 360 * i / len(array)
Then, modify your code like this, so the background is changed with CSS, defining HSL colors. Make sure to include the filter:
{% load rainbow %}
{% for tag in tags %}
<div class="btngroup" role="group" aria-label="tags">
<h3>
<a href="{{ tag.get_absolute_url }}" style="background:hsl({{ forloop.counter|rainbow:tags }},80%,50%)" class="btn btn-primary">{{tag.title}}</a>
</h3>
</div>
{% endfor %}
forloop.counter
in Django will get the index of the current item in the loop. 80% is for S(saturation) and 50% for L(lightness). You might want to fine-tune this to match your design.
Live example: https://repl.it/repls/AbleOtherBytecode
Good day! Can you explain in more detail where to create this file with an array and whether it is necessary to specify it in applications ?
– DieMauer
Nov 21 at 21:54
Hi, updated the answer with a live example and details how to create a filter.
– passatgt
Nov 22 at 9:59
Bingo !!! it's work! Thank you very much ! you helped me!
– DieMauer
Nov 22 at 16:12
add a comment |
You can use HSL to easily create a color based on the item index and the total items in your array. H is for Hue, which is from 0 to 360. 0 is red, 120 is green, 240 is blue. You can simply calculate the percentage of 360 based on the index of the item in the loop.
So first, you need to create a filter because you won't be able to do calculations directly in a Django template. Create a folder named templatetags
with two files inside: an empty __init__.py
and rainbow.py
with the following content:
from django import template
register = template.Library()
@register.filter()
def rainbow(i, array):
return 360 * i / len(array)
Then, modify your code like this, so the background is changed with CSS, defining HSL colors. Make sure to include the filter:
{% load rainbow %}
{% for tag in tags %}
<div class="btngroup" role="group" aria-label="tags">
<h3>
<a href="{{ tag.get_absolute_url }}" style="background:hsl({{ forloop.counter|rainbow:tags }},80%,50%)" class="btn btn-primary">{{tag.title}}</a>
</h3>
</div>
{% endfor %}
forloop.counter
in Django will get the index of the current item in the loop. 80% is for S(saturation) and 50% for L(lightness). You might want to fine-tune this to match your design.
Live example: https://repl.it/repls/AbleOtherBytecode
Good day! Can you explain in more detail where to create this file with an array and whether it is necessary to specify it in applications ?
– DieMauer
Nov 21 at 21:54
Hi, updated the answer with a live example and details how to create a filter.
– passatgt
Nov 22 at 9:59
Bingo !!! it's work! Thank you very much ! you helped me!
– DieMauer
Nov 22 at 16:12
add a comment |
You can use HSL to easily create a color based on the item index and the total items in your array. H is for Hue, which is from 0 to 360. 0 is red, 120 is green, 240 is blue. You can simply calculate the percentage of 360 based on the index of the item in the loop.
So first, you need to create a filter because you won't be able to do calculations directly in a Django template. Create a folder named templatetags
with two files inside: an empty __init__.py
and rainbow.py
with the following content:
from django import template
register = template.Library()
@register.filter()
def rainbow(i, array):
return 360 * i / len(array)
Then, modify your code like this, so the background is changed with CSS, defining HSL colors. Make sure to include the filter:
{% load rainbow %}
{% for tag in tags %}
<div class="btngroup" role="group" aria-label="tags">
<h3>
<a href="{{ tag.get_absolute_url }}" style="background:hsl({{ forloop.counter|rainbow:tags }},80%,50%)" class="btn btn-primary">{{tag.title}}</a>
</h3>
</div>
{% endfor %}
forloop.counter
in Django will get the index of the current item in the loop. 80% is for S(saturation) and 50% for L(lightness). You might want to fine-tune this to match your design.
Live example: https://repl.it/repls/AbleOtherBytecode
You can use HSL to easily create a color based on the item index and the total items in your array. H is for Hue, which is from 0 to 360. 0 is red, 120 is green, 240 is blue. You can simply calculate the percentage of 360 based on the index of the item in the loop.
So first, you need to create a filter because you won't be able to do calculations directly in a Django template. Create a folder named templatetags
with two files inside: an empty __init__.py
and rainbow.py
with the following content:
from django import template
register = template.Library()
@register.filter()
def rainbow(i, array):
return 360 * i / len(array)
Then, modify your code like this, so the background is changed with CSS, defining HSL colors. Make sure to include the filter:
{% load rainbow %}
{% for tag in tags %}
<div class="btngroup" role="group" aria-label="tags">
<h3>
<a href="{{ tag.get_absolute_url }}" style="background:hsl({{ forloop.counter|rainbow:tags }},80%,50%)" class="btn btn-primary">{{tag.title}}</a>
</h3>
</div>
{% endfor %}
forloop.counter
in Django will get the index of the current item in the loop. 80% is for S(saturation) and 50% for L(lightness). You might want to fine-tune this to match your design.
Live example: https://repl.it/repls/AbleOtherBytecode
edited Nov 22 at 9:58
answered Nov 20 at 22:09
passatgt
1,6492641
1,6492641
Good day! Can you explain in more detail where to create this file with an array and whether it is necessary to specify it in applications ?
– DieMauer
Nov 21 at 21:54
Hi, updated the answer with a live example and details how to create a filter.
– passatgt
Nov 22 at 9:59
Bingo !!! it's work! Thank you very much ! you helped me!
– DieMauer
Nov 22 at 16:12
add a comment |
Good day! Can you explain in more detail where to create this file with an array and whether it is necessary to specify it in applications ?
– DieMauer
Nov 21 at 21:54
Hi, updated the answer with a live example and details how to create a filter.
– passatgt
Nov 22 at 9:59
Bingo !!! it's work! Thank you very much ! you helped me!
– DieMauer
Nov 22 at 16:12
Good day! Can you explain in more detail where to create this file with an array and whether it is necessary to specify it in applications ?
– DieMauer
Nov 21 at 21:54
Good day! Can you explain in more detail where to create this file with an array and whether it is necessary to specify it in applications ?
– DieMauer
Nov 21 at 21:54
Hi, updated the answer with a live example and details how to create a filter.
– passatgt
Nov 22 at 9:59
Hi, updated the answer with a live example and details how to create a filter.
– passatgt
Nov 22 at 9:59
Bingo !!! it's work! Thank you very much ! you helped me!
– DieMauer
Nov 22 at 16:12
Bingo !!! it's work! Thank you very much ! you helped me!
– DieMauer
Nov 22 at 16:12
add a comment |
You can use background-image: linear-gradient on your .btngroup class. Set the height and width to what you prefer:
.btngroup {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 50px;
background-image: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
border-radius:4px;
color: white;
}
<div class="btngroup">Button<div>
add a comment |
You can use background-image: linear-gradient on your .btngroup class. Set the height and width to what you prefer:
.btngroup {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 50px;
background-image: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
border-radius:4px;
color: white;
}
<div class="btngroup">Button<div>
add a comment |
You can use background-image: linear-gradient on your .btngroup class. Set the height and width to what you prefer:
.btngroup {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 50px;
background-image: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
border-radius:4px;
color: white;
}
<div class="btngroup">Button<div>
You can use background-image: linear-gradient on your .btngroup class. Set the height and width to what you prefer:
.btngroup {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 50px;
background-image: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
border-radius:4px;
color: white;
}
<div class="btngroup">Button<div>
.btngroup {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 50px;
background-image: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
border-radius:4px;
color: white;
}
<div class="btngroup">Button<div>
.btngroup {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 50px;
background-image: linear-gradient(to right, red,orange,yellow,green,blue,indigo,violet);
border-radius:4px;
color: white;
}
<div class="btngroup">Button<div>
answered Nov 20 at 21:47
MichaelvE
1,1531310
1,1531310
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53401492%2fhow-to-make-a-buttons-with-colors-of-the-rainbow-in-my-cycle-in-html%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