skip to Main Content

I’m working on creating HTML documents of songs (specifically kPop), and colour-coding the lyrics by the singer.

Originally, I was adding classes to the <tr> elements, which did work. But I was wanting to see if I could further simplify the amount of typing needed. The <tr> elements were already nested within <div> elements, organizing them by what part of the song they were (verse1, chorus1, etc).

For the life of me, I cannot figure out how to get the css selectors to apply the styling to the <td> elements when I am selecting the class of the <div> element.

This is what I currently have, and does work to colour the lyrics correctly:

<table>
   <thead>
      <th>Romanization</th>
      <th>Hangul</th>
      <th>English</th>
   </thead>
   <tbody>
      <--! Verse 1-->
      <div class='v1'>
         <tr class='liz'>
            <td>Jipjunghae nun gamgo daeum sungan</td>
            <td>집중해 눈 감고 다음 순간</td>
            <td>Focus, the moment after you close your eyes</td>
         </tr>
         <tr class='liz'>
            <td>Meorissogeul maemdoneun merry go round</td>
            <td>머릿속을 맴도는 merry go round</td>
            <td>A merry go round spinning in your head</td>
         </tr>
         <tr class='leeseo'>
            <td>Muishik sogeuro fallin’ down</td>
            <td>무의식 속으로 fallin’ down</td>
            <td>Fallin’ down into the unconscious</td>
         </tr>
         <tr class='leeseo'>
            <td>Dansume ppajyeodeulji</td>
            <td>단숨에 빠져들지</td>
            <td>You’ll fall for me instantly</td>
         </tr>
      </div>
   </tbody>
</table>

What I ideally would like to accomplish is something like this:

Edit: Thank you for telling me about how <div> cannot be used inside tables, I did not know they wouldn’t be parsed. Is there any better method of organizing <tr> elements so I can reduce the amount of classes my <tr> elements contain?

<table>
   <thead>
      <th>Romanization</th>
      <th>Hangul</th>
      <th>English</th>
   </thead>
   <tbody>
      <--! Verse 1-->
      <div class='v1'>
         <div class='liz'>
            <tr>
               <td>Jipjunghae nun gamgo daeum sungan</td>
               <td>집중해 눈 감고 다음 순간</td>
               <td>Focus, the moment after you close your eyes</td>
            </tr>
            <tr>
               <td>Meorissogeul maemdoneun merry go round</td>
               <td>머릿속을 맴도는 merry go round</td>
               <td>A merry go round spinning in your head</td>
            </tr>
         </div>
         <div class='leeseo'>
            <tr>
               <td>Muishik sogeuro fallin’ down</td>
               <td>무의식 속으로 fallin’ down</td>
               <td>Fallin’ down into the unconscious</td>
            </tr>
            <tr>
               <td>Dansume ppajyeodeulji</td>
               <td>단숨에 빠져들지</td>
               <td>You’ll fall for me instantly</td>
            </tr>
         </div>
      </div>
   </tbody>
</table>

My current CSS looks like this, its incredibly barebones but works:

.liz {
    color: teal
}

.wonyoung {
    color: red
}

.rei {
    color: green
}

.gaeul {
    color: blue
}

.yujin {
    color: pink
}

.leeseo {
    color: yellow
}

I’ve tried the following CSS selectors:

.liz {
    color: pink
}
.liz td {
    color: pink
}
.liz tr td {
    color: pink
}

I’ve done some research into inheritance and seeing if I could get the <td> elements to inherit the colour property from the <div> elements, but what mainly pops up is how having inheritence chain like that goes against W3 best practices.

Since this is purely for myself (I will be storing them in Obsidian, which displays notes in markup and HTML), I wanted to see if what I want to accomplish is even possible. Ideally, without using Javascript.

2

Answers


  1. You can give the tr classes that will affect the TDs, you can also directly refer to the tds if you wanted.

    Div’s aren’t allowed to be the children of tr,tbody,table nor thead.

    .liz {
        color: pink
    }
    
    .leeseo {
        color: red
    }
    <table>
       <thead>
          <th>Romanization</th>
          <th>Hangul</th>
          <th>English</th>
       </thead>
       <tbody>
                <tr class="liz v1">
                   <td>Jipjunghae nun gamgo daeum sungan</td>
                   <td>집중해 눈 감고 다음 순간</td>
                   <td>Focus, the moment after you close your eyes</td>
                </tr>
                <tr class="liz v1">
                   <td>Meorissogeul maemdoneun merry go round</td>
                   <td>머릿속을 맴도는 merry go round</td>
                   <td>A merry go round spinning in your head</td>
                </tr>
                <tr class='leeseo v1'>
                   <td>Muishik sogeuro fallin’ down</td>
                   <td>무의식 속으로 fallin’ down</td>
                   <td>Fallin’ down into the unconscious</td>
                </tr>
                <tr class='leeseo v1'>
                   <td>Dansume ppajyeodeulji</td>
                   <td>단숨에 빠져들지</td>
                   <td>You’ll fall for me instantly</td>
                </tr>
       </tbody>
    </table>
    Login or Signup to reply.
  2. First, a note, as stated in comments:

    You can’t nest <div>s directly in a <tbody>. They will be stripped off by the parser.

    <table>s have quite a strict structure, and are parsed by the HTML reader as:

    <table>
        <thead>
            <th>
                <tr>
                    <td>
        <tbody>
            <tr>
               <td>
    

    There many be variations on the above exact layout but the point is you can’t add non-table markup within a table structure, only inside the end point element; the <td>.

    You can also read about CSS Inheritance on MDN.


    Now, to answer your question; you can use HTML5 data- elements to mark which sections should be given which colours.

    table tbody {
      background-color: #ccc;
    }
    
    tr[data-singer='liz'] {
      color: teal;
    }
    
    tr[data-singer='leeseo'] {
      color:yellow;
    }
    <table>
    <thead>
      <th>Romanization</th>
      <th>Hangul</th>
      <th>English</th></thead>
    <tbody>
         <tr data-singer='liz'>
                   <td>Jipjunghae nun gamgo daeum sungan</td>
                   <td>집중해 눈 감고 다음 순간</td>
                   <td>Focus, the moment after you close your eyes</td>
                </tr>
                <tr data-singer='liz'>
                   <td>Meorissogeul maemdoneun merry go round</td>
                   <td>머릿속을 맴도는 merry go round</td>
                   <td>A merry go round spinning in your head</td>
                </tr>
                <tr data-singer='leeseo'>
                   <td>Muishik sogeuro fallin’ down</td>
                   <td>무의식 속으로 fallin’ down</td>
                   <td>Fallin’ down into the unconscious</td>
                </tr>
                <tr data-singer='leeseo'>
                   <td>Dansume ppajyeodeulji</td>
                   <td>단숨에 빠져들지</td>
                   <td>You’ll fall for me instantly</td>
                </tr>
    </tbody>
    </table>

    You can add as many data- attributes as you need, such as for which verse there is (eg data-verse='1') and you can reference these a similar way with CSS alone.

    Please see This related Q&A for more details on working with data- attributes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search