How to add random class name to an element in blogger template

639 Views Asked by At

I want to add a random class name from (class1, class2, class3, class4) to #tagcat element.

How can I do that? Is it possible to do this without javascript?

<b:if cond='data:top.showPostLabels and data:post.labels'>
  <b:loop index='i' values='data:post.labels' var='label'>
    <b:if cond='data:i == 0'>
      <div class='alt-cat blurple-gradient' id='tagcat' title='ds'>
        <a expr:href='data:label.url' rel='tag'>
          <data:label.name/>
        </a>
      </div>
    </b:if>
  </b:loop>
</b:if>
</div>

EDIT

That is the complete code that shows the recent 8 posts on the home page of my blog:

<div class='entry-content' expr:id='&quot;post-body-&quot; + data:post.id' expr:itemprop='(data:blog.metaDescription ? &quot;&quot; : &quot;description &quot;) + &quot;articleBody&quot;'>

  <b:if cond='data:blog.pageType == &quot;item&quot;'>
    <data:post.body/>
    <b:else/>
    <b:if cond='data:blog.pageType == &quot;static_page&quot;'>
      <data:post.body/>
      <b:else/>
      <div class='post-media'>
        <b:if cond='data:post.firstImageUrl'>

          <div class='image-wrap'>
            <!-- <div class='mask'/> -->
            <a expr:href='data:post.url'>
              <img expr:alt='data:post.title' expr:src='data:post.thumbnailUrl' />
            </a>

          </div> <b:else/>
          <div class='needo' /> </b:if>


        <script class='java' expr:src='&quot;/feeds/posts/default/&quot; + data:post.id + &quot;?alt=json-in-script&amp;callback=labelset&quot;' type='text/javascript' />


        <b:if cond='data:top.showPostLabels and data:post.labels'>
          <b:loop index='i' values='data:post.labels' var='label'>
            <b:if cond='data:i == 0'>

              <div class='alt-cat blurple-gradient' id='tagcat' title='ds'>
                <a expr:href='data:label.url' rel='tag'>
                  <data:label.name/>
                </a>
              </div>
            </b:if>
          </b:loop>
        </b:if>
      </div>
      <div class='post-body'>

        <div class='post-title'>
          <h2>
            <a expr:href='data:post.url'><data:post.title/></a>
          </h2>
        </div>
        <div class='post-entry'>
          <p><data:post.snippet/></p>
        </div>

        <div class='post-author'>
          <!--
                                  <div class='image-thumb'>
                                    <img alt='Post Author' expr:src='data:post.authorPhoto.url'/>
                                  </div>
-->
          <div class='name-author'>
            <data:post.author/>
            <div class='time-stamp'>
              <data:post.timestamp/>
            </div>
          </div>
        </div>

The element #tagcat displays the label. I want to add different classes on element #tagcat for different posts so that they appear in different colors, so I need different CSS for different post snippets label.

1

There are 1 best solutions below

2
On BEST ANSWER

Why not using post label name in class and style each label with css

<div expr:class='data:label.name' id='tagcat' title='ds'>

CSS

.label-name { color: your color }