使用CSS选择器定位具有两个表行的表的第一个表行,如何?

问题描述 投票:1回答:2

上下文:我正在尝试编辑第一个只有两行的表的第一行。它目前是所有tr的目标。我在CSS下面的底部包含了所有HTML。

问题1:第一个代码块当前以两行中的第一个表头为目标。我需要它只针对第一行中的第一个表头。

问题2:第二个代码块当前以每行中的每个表头为目标,除了每行的第一个。我需要第二个代码块来仅定位第一个表行的最后4个表头。


这是我的HTML CSS。

thead > tr th:first-of-type{
  font-size: 2em;
  }
thead > tr th:not(:first-of-type){
  background-color: transparent;
  color: black;
  }

这是我的HTML

    <!DOCTYPE html>
    <html>
    <head>
           <meta charset="utf-8" />
           <meta name="viewport" content="width=device-width, initial-scale=1" />
           <title>Marlin Internet</title>
           <link href="mi_reset.css" rel="stylesheet" />
           <link href="mi_styles.css" rel="stylesheet" />
           <link href="mi_tables.css" rel="stylesheet" />
        </head>

        <body>
           <header>
              <img src="mi_logo.png" alt="Marlin Internet" id="logoimg" />
              <nav>
                 <a id="navicon" href="#"><img src="mi_navicon.png" alt="" /></a>
                 <ul>
                    <li><a href="#">Internet</a></li>
                    <li><a href="#">Home Networking</a></li>
                    <li><a href="#">My Account</a></li>
                    <li><a href="#">Shop</a></li>
                    <li><a href="#">Support</a></li>
                 </ul>
              </nav>
           </header>

           <article>
              <h1>Accelerate with No Speed Traps</h1>
              <p>How do you use the web? Whether it's just you on your PC or your extended
                 family simultaneously accessing the Internet for gaming, movie watching,
                 surfing, or music; we have the speed for you &mdash; with no hidden fees,
                 service charges, or other traps. <strong>Marlin Internet</strong> provides a broad variety
                 of access plans at affordable prices with our exclusive ITIS 2.0 technology. 
                 We provide more than fast access. Take advantage of our 24/7 customer support
                 (personal help is <em>always</em> on the line), free security tools, email accounts
                 for the whole family, and exclusive online content.</p>

          <table id="pricing">
            <colgroup>
              <col id="firstCol" />
              <col span="4" class="dataCols" />
            </colgroup>
            <thead>
              <tr>   
/*I am trying to change this header with the first block of code*/
                <th rowspan="2"> select a plan </th> 
/* I am tying to change these four headers below with the second block of code*/
                <th> Starter    </th>
                <th> Prime      </th>
                <th> Prime Plus </th>
                <th> Ultra      </th>
              </tr>
              <tr>
                <th> $19.95 <br/> per month </th>
                <th> $29.95 <br/> per month </th>
                <th> $49.95 <br/> per month </th>
                <th> $69.95 <br/> per month </th>
              </tr>
            </thead>
            <tfoot>
              <tr>
                <th> Summary </th>
                <td>
                  Just the speed you need to send emails, download map directions, and search the Internet for restaurant reviews.
                </td>
                <td>
                  A great speed at a great price for the family: kids can play games, adults can pay bills, and everyone can surf at the same time.
                </td>
                <td>
                  Super speeds for multiple tasks that require more broadband capacity; ideal for gamers who need fast response times.
                </td>
                <td>
                  Perfect for a small business running multiple media streams, demanding reduced lag time and fast data delivery.
                </td>  
              </tr>
             </tfoot>
             <tbody>
              <tr>
                <th>Dowload Speed</th>
                <td> 3 Mbps  </td>
                <td> 15 Mbps </td>
                <td> 25 Mbps </td>
                <td> 50 Mbps </td>  
              </tr>
              <tr>
                <th> Upload Speed </th>
                <td> 512 Kbps </td>
                <td> 6 Mbps </td>
                <td> 10 Mbps </td>
                <td> 20 Mbps </td>  
              </tr>
              <tr>
                <th> Cloud Storage </th>
                <td> 2 GB  </td>
                <td> 5 GB  </td>
                <td> 15 GB </td>
                <td> 40 GB </td>  
              </tr>
              <tr>
                <th> E-mail Accounts </th>
                <td> 2 Accounts  </td>
                <td> 3 Accounts  </td>
                <td> 5 Accounts  </td>
                <td> 10 Accounts </td>  
              </tr>
              <tr>
                <th> 24/7 Support </th>
                <td> Yes </td>
                <td> Yes </td>
                <td> Yes </td>
                <td> Yes </td>  
              </tr>
            </tbody>
           </table>
           </article>
           <footer>
              <nav>
                 <ul>
                    <li><a href="#">About Us</a></li>
                    <li><a href="#">Careers</a></li>
                    <li><a href="#">News</a></li>
                    <li><a href="#">Parental Controls</a></li>
                    <li><a href="#">Our Locations</a></li>
                 </ul>
                 <ul>
                    <li><a href="#">Internet</a></li>
                    <li><a href="#">TV</a></li>
                    <li><a href="#">TiVo</a></li>
                    <li><a href="#">Home Security</a></li>
                    <li><a href="#">Phone</a></li>
                 </ul>   
                 <ul>
                    <li><a href="#">Web Mail</a></li>
                    <li><a href="#">My Account</a></li>
                    <li><a href="#">Support</a></li>
                    <li><a href="#">Contact Us</a></li>
                 </ul>                
              </nav>
              <img src="mi_art.png" alt="" class="clipart" />
           </footer>
        </body>

        </html>
css css-selectors
2个回答
2
投票

我相信这就是诀窍......

thead > tr:first-of-type th:first-of-type{
  font-size: 2em;
  color:red;
  }
thead > tr:first-of-type th:not(:first-of-type){
  background-color: transparent;
  color: blue;
  }
          <table id="pricing">
            <colgroup>
              <col id="firstCol" />
              <col span="4" class="dataCols" />
            </colgroup>
            <thead>
              <tr>   
/*I am trying to change this header with the first block of code*/
                <th rowspan="2"> select a plan </th> 
/* I am tying to change these four headers below with the second block of code*/
                <th> Starter    </th>
                <th> Prime      </th>
                <th> Prime Plus </th>
                <th> Ultra      </th>
              </tr>
              <tr>
                <th> $19.95 <br/> per month </th>
                <th> $29.95 <br/> per month </th>
                <th> $49.95 <br/> per month </th>
                <th> $69.95 <br/> per month </th>
              </tr>
            </thead>
            <tfoot>
              <tr>
                <th> Summary </th>
                <td>
                  Just the speed you need to send emails, download map directions, and search the Internet for restaurant reviews.
                </td>
                <td>
                  A great speed at a great price for the family: kids can play games, adults can pay bills, and everyone can surf at the same time.
                </td>
                <td>
                  Super speeds for multiple tasks that require more broadband capacity; ideal for gamers who need fast response times.
                </td>
                <td>
                  Perfect for a small business running multiple media streams, demanding reduced lag time and fast data delivery.
                </td>  
              </tr>
             </tfoot>
             <tbody>
              <tr>
                <th>Dowload Speed</th>
                <td> 3 Mbps  </td>
                <td> 15 Mbps </td>
                <td> 25 Mbps </td>
                <td> 50 Mbps </td>  
              </tr>
              <tr>
                <th> Upload Speed </th>
                <td> 512 Kbps </td>
                <td> 6 Mbps </td>
                <td> 10 Mbps </td>
                <td> 20 Mbps </td>  
              </tr>
              <tr>
                <th> Cloud Storage </th>
                <td> 2 GB  </td>
                <td> 5 GB  </td>
                <td> 15 GB </td>
                <td> 40 GB </td>  
              </tr>
              <tr>
                <th> E-mail Accounts </th>
                <td> 2 Accounts  </td>
                <td> 3 Accounts  </td>
                <td> 5 Accounts  </td>
                <td> 10 Accounts </td>  
              </tr>
              <tr>
                <th> 24/7 Support </th>
                <td> Yes </td>
                <td> Yes </td>
                <td> Yes </td>
                <td> Yes </td>  
              </tr>
            </tbody>
           </table>

1
投票

问题是你在:first-of-type元素(th)上使用thead > tr th:first-of-type,这意味着它将在每个th元素中应用第一个tr元素的样式。

解决方案是在你的:first-of-type元素上应用tr,然后它将针对你的第一个tr然后将仅应用于第一个类型的样式:)。另外,为了设置tr的第一个元素,你可以使用CSS Selector :fisrt-child

thead > tr:first-of-type th{
  background-color: transparent;
  color: black;
}
thead > tr:first-of-type th:first-child {
  font-size: 2em;
}

thead > tr:first-of-type th{
  background-color: transparent;
  color: black;
}
thead > tr:first-of-type th:first-child {
  font-size: 2em;
}
<table id="pricing">
  <colgroup>
    <col id="firstCol" />
    <col span="4" class="dataCols" />
  </colgroup>
  <thead>
    <tr>
      <th rowspan="2"> select a plan </th> 
      <th> Starter    </th>
      <th> Prime      </th>
      <th> Prime Plus </th>
      <th> Ultra      </th>
    </tr>
    <tr>
      <th> $19.95 <br/> per month </th>
      <th> $29.95 <br/> per month </th>
      <th> $49.95 <br/> per month </th>
      <th> $69.95 <br/> per month </th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th> Summary </th>
      <td>
        Just the speed you need to send emails, download map directions, and search the Internet for restaurant reviews.
      </td>
      <td>
        A great speed at a great price for the family: kids can play games, adults can pay bills, and everyone can surf at the same time.
      </td>
      <td>
        Super speeds for multiple tasks that require more broadband capacity; ideal for gamers who need fast response times.
      </td>
      <td>
        Perfect for a small business running multiple media streams, demanding reduced lag time and fast data delivery.
      </td>  
    </tr>
   </tfoot>
   <tbody>
    <tr>
      <th>Dowload Speed</th>
      <td> 3 Mbps  </td>
      <td> 15 Mbps </td>
      <td> 25 Mbps </td>
      <td> 50 Mbps </td>  
    </tr>
    <tr>
      <th> Upload Speed </th>
      <td> 512 Kbps </td>
      <td> 6 Mbps </td>
      <td> 10 Mbps </td>
      <td> 20 Mbps </td>  
    </tr>
    <tr>
      <th> Cloud Storage </th>
      <td> 2 GB  </td>
      <td> 5 GB  </td>
      <td> 15 GB </td>
      <td> 40 GB </td>  
    </tr>
    <tr>
      <th> E-mail Accounts </th>
      <td> 2 Accounts  </td>
      <td> 3 Accounts  </td>
      <td> 5 Accounts  </td>
      <td> 10 Accounts </td>  
    </tr>
    <tr>
      <th> 24/7 Support </th>
      <td> Yes </td>
      <td> Yes </td>
      <td> Yes </td>
      <td> Yes </td>  
    </tr>
  </tbody>
 </table>
© www.soinside.com 2019 - 2024. All rights reserved.