简体   繁体   English

在 CSS 中,在父元素悬停时显示子元素

[英]Show child element on parent hover in CSS

Is it possible to toggle the Delete button when mouse hovers onto the parent div in pure CSS?当鼠标悬停在纯 CSS 中的父div上时,是否可以切换删除按钮?

HTML HTML

<div id="parent">
    <button class="hidden-child">delete</button>
</div>

CSS CSS

#parent:hover{
    /* Some CSS here */
}

if you have styled hide like this (the page will be displayed as if the element is there but not seen):如果您将hide设置为这样(页面将显示为好像该元素存在但未被看到):

#parent .hidden-child{
    visibility: hidden;
}

you may do it like this to just hide it:你可以这样做只是为了隐藏它:

#parent:hover .hidden-child{
    visibility: visible;
}

and if you have styled it like this (the page will be displayed as if the element is not there):并且如果您将其设置为这样(页面将显示为好像该元素不存在):

#parent .hidden-child{
    display: none;
}

you may do it like this:你可以这样做:

#parent:hover .hidden-child{
    display: block;
}

In Action!在行动!

 #parent { width: 10rem; height: 10rem; background-color: #ababab; } #parent.hidden-child{ visibility: hidden; } #parent:hover.hidden-child{ visibility: visible; }
 <div id="parent"> <button class="hidden-child">Delete</button> </div>

Without knowing how you've styled your controls,if you are using any css-js related libraries, the class ".hide" is most likely defined there and will be called by default which will then hide your button thus you manipulate it using JQuery or Javascript.在不知道您如何设置控件样式的情况下,如果您使用任何与 css-js 相关的库,类“.hide”很可能在那里定义并且默认情况下将被调用,然后将隐藏您的按钮,因此您可以使用 JQuery 对其进行操作或Javascript。

However, if you have vanillarised your code and the hidden state of your button is display:none;但是,如果您修改了代码并且按钮的隐藏状态为显示:无; users will be left with a blank page not knowing what or where to hover so you must give them something to hover on:用户将留下一个空白页面,不知道要悬停什么或在哪里,所以你必须给他们一些悬停的东西:

Sol 1:

HTML:
<div id="something">
     Hover Here
    <button class="hides">delete</button>
</div>

CSS:
#something .hides{

    display:none;
}

#something:hover .hides{

    display:inline-block;
}

If the hidden state of your button is visibility:hidden ;this will hide the button but will still reserve its space on the screen thus leaving an awkward interface unless you make it fancy with styling.如果按钮的隐藏状态是visibility:hidden ;这将隐藏按钮,但仍会保留其在屏幕上的空间,从而留下一个尴尬的界面,除非你喜欢它的样式。

Sol 2:

HTML:

<div id="something">
    <button class="hides">delete</button>
</div>

CSS:
#something{

    /*My fancy style;*/
}
#something .hides{

    visibility: hidden;
}

#something:hover .hides{

        visibility: visible;
}

There are two states in your situation, the first one is when the mouse hover the element the second is when it doesn't as a result leave您的情况有两种状态,第一种是鼠标hover元素时,第二种是它没有因此leave

here you have to target the element .hide when the pointer is on div then then disable any click event that may occur.在这里,当指针位于 div 上时,您必须将元素.hide设为目标,然后禁用可能发生的任何点击事件。

code snippet:代码片段:

div#something:hover .hide{

   pointer-events: none;
   color: GrayText;
   background-color: #ddd;
   background: #ddd;
}

working example:工作示例:

jsfiddle小提琴

cheers干杯

to hide element initially最初隐藏元素


#something > .hide {
  display: none;
}

display element on hover悬停时显示元素

#something:hover {
  .hide {
    display: inline;
  }
}

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

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