简体   繁体   English

css悬停时更改属性

[英]css change property on hover

Is there a way to change the css property for example of a box when an element (inside the ) is hover? 当元素(位于内)悬停时,是否可以更改框等CSS属性?

for example if I have: 例如,如果我有:

<table>
<tr><td><a>.....</td></tr>
</table>

I want to change the property of the container td when the link a has the mouse over. 当链接a悬停在鼠标上方时,我想更改容器td的属性。 Is it possible? 可能吗?

Sorry, I have not explained well. 抱歉,我没有很好地解释。 I have a , not a table...it was only an example.... I have 我有一个,不是桌子...这只是一个例子...。

<ul>
<li><a>.....

in my css I have: 在我的CSS中,我有:

#navigation li a {
text-decoration: none;
color: #333;
text-transform: none;
}
#navigation li:hover {
color: white;
background-color: #333;
}
#navigation li a:hover {
color: white;
background-color: #333;
}

but It does not works because if I go on the link it's ok, but if I go with the mouse in the li but off the link the color of the text does not change... 但是它不起作用,因为如果我点击链接就可以了,但是如果我将鼠标放在li上但关闭了链接,则文本的颜色不会改变...

Instead of 代替

#navigation li a:hover {

try 尝试

#navigation li:hover a {

but It does not works because if I go on the link it's ok, but if I go with the mouse in the li but off the link the color of the text does not change... 但是它不起作用,因为如果我点击链接就可以了,但是如果我将鼠标放在li上但关闭了链接,则文本的颜色不会改变...

That's because you're putting the hover on the link itself. 那是因为您将鼠标悬停在链接本身上。 If you want the link to react when you mouse over the li, simply change where you put the hover pseudo-class: 如果您希望链接在将鼠标悬停在li上时做出反应,只需更改放置悬停伪类的位置即可:

li:hover a {
    color: #d2d2d2;
}

This basically says "when the li is hovered, the link inside it should change to these styles." 这基本上是说:“当li悬停时,它内部的链接应更改为这些样式。”

Alternatively, you can add padding to the link (ex - padding: 5px ), making its reaction field larger. 或者,您可以向链接中添加填充(例如padding: 5px ),以使其响应字段更大。 So: 所以:

li a {
  display: block; /* Required to make it honor padding. */
  padding: 10px;
}

li a:hover {
  color: #d2d2d2;
}

As long as you don't have your li elements set to a larger size than the a element (via height, width, margin, and/or padding), then the li will "shrink-wrap" the a and be the same size as the total size of the link. 只要您未将li元素设置为大于a元素的大小(通过高度,宽度,边距和/或填充),那么li就会“收缩包装” a并保持相同大小作为链接的总大小。

You cant change a property of a parent element, but you can trigger a hover event on the parent td itself: 您不能更改父元素的属性,但是可以在父td本身上触发悬停事件:

table td:hover {
    background-color: red;
}

您可以在锚元素上添加display:块,这将使锚填满li ...取决于ul等的缩进等。

li a { display: block; }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM