简体   繁体   English

需要表格单元格中的背景图像和链接文本颜色以将鼠标悬停在表格单元格上(使用CSS)进行更改

[英]Need both background image and link text color in a table cell to change on mouse hover over the cell (with CSS)

Sorry for a novice's question, but I am about to give up having spent whole day on this issue... I have searched this forum for a few hours, but still can't get the thing to work. 对一个新手的问​​题很抱歉,但是我将放弃一整天在这个问题上的工作...我在这个论坛上搜索了几个小时,但仍然无法正常工作。

Would anyone assist me with CSS solution to the following problem please? 有人可以为我提供CSS解决以下问题的方法吗?

Basically, I have a table cell with an image background and a text link. 基本上,我有一个带有图像背景和文本链接的表格单元。 I need the image and link color to change on mouse hover over this table cell at the same time. 我需要图像和链接颜色来同时将鼠标悬停在此表单元格上。 My cumbersome code looks about like this: 我的繁琐代码看起来像这样:

<table>
  <tr>
    <td width="160" height="36" align="center" valign="middle" class="menuitem1">
       <a href="http://www.samplesite.com">About us</a>
    </td>
  </tr>
</table>`

Then I tried all this in a CSS file: 然后我在CSS文件中尝试了所有这些方法:

.menuitem1 {
   border-right-width: 1px;
   border-right-style: solid;   
   border-right-color: #000;
   background-image: url("images/sample.png");
   background-repeat: no-repeat;
   background-position: left;
   padding-left: 17px;
}
.menuitem1 a:link{
   display: block;
   text-decoration: none;
   color: #fff;
}
.menuitem1 a:visited {
   text-decoration: none;
   color: #fff;
}
.menuitem1:hover {
   text-decoration: none;
   background-image: url("images/sample2.png");
   background-repeat: no-repeat;
   background-position: left;
   color: #000;
}
.menuitem1 a:hover {
   text-decoration: none;
   color: #000;
}
.menuitem1 a:active{
   color: #000;
}

The problem is that the text link doesn't follow block attribute. 问题在于文本链接没有遵循block属性。 So the the text color changes only when mouse is over it, not over the rest of the cell. 因此,仅当鼠标悬停在文本上方时,文本颜色才会更改,而单元格的其余部分则不会更改。 Adding width: 100% to .menuitem1 a still leaves 17px on the left ( padding-left ) that is not in the block. 增加width: 100% .menuitem1 a width: 100%.menuitem1 a仍在不在块中的左侧( padding-left )留下17px And it is even worse with the height: 100% has no effect and fixed height in px aligns the text vertically on top without possibility to change it. height: 100%更糟height: 100%无效,并且固定高度(以px为单位)会将文本垂直对齐在顶部,而无法更改。

Funny that if I don't have a hyperlink in the cell and have just text there, then this simple piece of code is sufficient for everything to work like a dream: 有趣的是,如果我在单元格中没有超链接,而在其中只有文本,那么这段简单的代码就足以让一切像梦一样工作:

.menuitem1:hover {
   text-decoration: none;
   background-image: url("images/sample2.png");
   background-repeat: no-repeat;
   background-position: left;
   color: #000;
}

Isn't there a simple solution like that to the problem that occurs after adding a link? 对于添加链接后出现的问题,没有像这样的简单解决方案吗?

Thank you very much in advance. 提前非常感谢您。

What many beginning javascript programmers don't at first realize, is that any list of selectors can be strung together to new selector. 许多刚开始的javascript程序员一开始并没有意识到的是, 任何选择器列表都可以串在一起成为新的选择器。 So to change the background image, you are right in putting: 因此,要更改背景图片,请正确放置:

.menuitem1:hover {
    background-image: url("images/sample2.png");
    background-repeat: no-repeat;
    background-position: left;

But to select the a element, you should use the following format: 但是要选择a元素,您应该使用以下格式:

.menuitem1:hover a {
    color: #000;
    text-decoration: none;
    ...
}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用CSS,我如何根据我悬停的单元格更改表格行的背景颜色? - Using CSS, how can I change the background color of a table row depending on the cell I hover over? CSS表格单元格背景图片文字重叠 - CSS Table Cell Background Image Text Overlap CSS hover 不适用于具有特定背景颜色的表格单元格 - CSS hover not works on a table cell if it has a specific background color 引导表,悬停单元格以更改所有单元格的背景色 - Bootstrap table,hover cell to change ALL cells background color CSS:将鼠标悬停在块上以更改文本颜色和链接 - CSS: hover over block to change text color and link 使用CSS / HTML / Python / SASS(但不使用JS)根据值更改表格单元格背景的颜色? 无需动态 - Change the color of the background of a table cell based on the value using CSS/HTML/Python/SASS (but no JS)? No need to be dynamically CSS改变在图象背景老鼠翱翔的背景 - CSS change background on image background mouse hover CSS clear:两者都不能用于带有背景图像的表格单元吗? - CSS clear:both not working for table cell with a background image? 将鼠标悬停在表格单元格上时更改BODY背景颜色 - Change BODY background color when hovering over a table cell 根据文本值更改表格单元格的文本和背景颜色 - Change Text And Background Color Of Table Cell Based On Text Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM