CSS ::之前在Table Cell上

Ada*_*dam 9 html css pseudo-element

我想::before在一些表格单元格上添加一个选择器position:absolute,但它失败了:

table{ border:1px solid #ccc; padding:10px; }
table td{ border:1px solid #ccc; padding:5px; }

.useBefore::before{
  content:'before';
  position:absolute;
}
Run Code Online (Sandbox Code Playgroud)
<table>
       <tbody>
         <tr>
           <td>bird</td>
           <td>animal</td>
           <td>nature</td>
        </tr>
        <tr class='useBefore'>
           <td>building</td>
           <td>robot</td>
           <td>city</td>
        </tr>
      </tbody>
  </table>
Run Code Online (Sandbox Code Playgroud)

我注意到如果我将其添加::before到所有的那个tr,那么它的工作原理:

table{ 
    border:1px solid #ccc; 
    padding:10px;
  }
  table td{
    border:1px solid #ccc;
    padding:5px;
  }
  
  tr::before{
    content:'before';
    position:absolute;
  }
Run Code Online (Sandbox Code Playgroud)
<table>
    <tbody>
      <tr>
        <td>bird</td>
        <td>animal</td>
        <td>nature</td>
      </tr>
      <tr class='useBefore'>
        <td>building</td>
        <td>robot</td>
        <td>city</td>
      </tr>
    </tbody>
  </table>
Run Code Online (Sandbox Code Playgroud)

但这不是我想要的,因为我只想在其中一些上添加它.

kap*_*apa 17

我不确定它为什么会完全失败,但你可以将其添加到第一个表格单元格上.

  .useBefore td:first-child:before{
    content:'before';
    position:absolute;
  }
Run Code Online (Sandbox Code Playgroud)

  • 我不确定你的代码为什么可以工作,但它可以工作:))我尝试了“table tr .useBefore”,但失败了,嘿,非常感谢!:) 你的名字在匈牙利语中听起来像一个肮脏的词,你是匈牙利语吗 :P ? (2认同)
  • @CIRK 是的,其他人之前告诉我我的匈牙利语名字不好听:)。 (2认同)
  • @AdamHalasz 当您编写“table tr .useBefore”时,它意味着“tr”*内部*具有类名“.useBefore”的元素。如果您想要影响同时也是“.useBefore”的“tr”,那么您可以输入“table tr.useBefore”。 (2认同)