html - 如何在类内的一个表格单元格中用背景色填充整个单元格

标签 html css html-table background-color

我在一个表中有一个表行,在那个表行中,我有四个 <td>对象:

<table id="top">
<tr>
    <td class="topitem" style="float:left; margin-left:10px;" id="topone">Home</td>
    <td class="topitem" id="toptwo">Go To Channel</td>
    <td class="topitem" id="topthree">Other things I like to do</td>
    <td class="topitem" id="about" id="topfour">About</td>
</tr>
</table>

在我的 css 中,我试图从四个中选出第一个,如下所示: #顶部 { 高度:65px; 背景色:黑色; 边距顶部:20px; } #top tr:first-child td:nth-child(1) { 背景色:#00F;

我找到了类似的答案:

但是,它们都不起作用,我什至可以在其中放置更多不起作用的答案,但我认为这就足够了。我需要 2016 年的更新。

我实际上想做的是制作那些“顶级菜单”之一,当您将鼠标悬停在其中时,单元格的背景将变为白色,而文本将变为黑色。问题是 height 是 65px,我不能让整个东西变成白色,只有突出显示的文本。

但是,就目前而言,我不会为此担心。我只需要知道如何使背景颜色适合整个表格单元格。

最佳答案

为 CSS 添加了以下规则集:

td {
  background: #ede;
  color: #112;
  transition: background 0.4s ease-out, color 0.4s ease-out;
  cursor: pointer;
}
td:first-of-type:hover {
  background: #112;
  color: #ede;
  transition: background 0.4s ease-in, color 0.4s ease-in;
}

td {
  background: #ede;
  color: #112;
  transition: background 0.4s ease-out, color 0.4s ease-out;
  cursor: pointer;
}
td:first-of-type:hover {
  background: #112;
  color: #ede;
  transition: background 0.4s ease-in, color 0.4s ease-in;
}
<table id="top">
  <tr>
    <td class="topitem" style="float:left; margin-left:10px;" id="topone">Home</td>
    <td class="topitem" id="toptwo">Go To Channel</td>
    <td class="topitem" id="topthree">Other things I like to do</td>
    <td class="topitem" id="topfour">About</td>
  </tr>
  <tr>
    <td class="miditem" style="float:left; margin-left:10px;" id="midone">Row2</td>
    <td class="miditem" id="midtwo">Go To Channel</td>
    <td class="miditem" id="midthree">Other things I like to do</td>
    <td class="miditem" id="midfour">About</td>
  </tr>
  <tr>
    <td class="lastitem" style="float:left; margin-left:10px;" id="lastone">Row 3</td>
    <td class="lastitem" id="lasttwo">Go To Channel</td>
    <td class="lastitem" id="lastthree">Other things I like to do</td>
    <td class="lastitem" id="lastfour">About</td>
  </tr>
</table>

关于html - 如何在类内的一个表格单元格中用背景色填充整个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41131318/

相关文章:

Jquery悬停添加类并在悬停时删除类

javascript - 移动优化 : Make notification menu with position:absolute scrollable

html - 如何制作CSS的高级形状?

javascript - 在 Javascript 中使用 .js 文件生成格式表

html - 如何根据相邻单元格的高度来限制表格单元格 (td) 的高度?

javascript - 在 ASP.NET 中单击提交按钮时使表单不刷新

html - 如何在没有字段集的情况下以可访问的方式对表单输入进行分组?

CSS 颜色名称 + alpha 透明度

javascript - 响应式问题 JQzoom

html - 可滚动的 tbody td 宽度在提供宽度后不占用全部空间