英文:
Avoid expanding the flex element and pusing the other elements down on hover
问题
可以使hover
元素不推动其他元素向下,从而扩展整个 flexbox,而是让它与其他元素重叠并用其主体隐藏它们吗?
我尝试了 z-index,但没有成功。
.flex-container {
display: flex;
justify-content: space-evenly;
flex: auto;
}
.flex-container > div {
background-color: #f1f1f1;
width: 100%;
margin: 10px;
line-height: 15px;
font-size: 15px;
border-radius: 10px;
overflow: hidden;
position: relative;
}
.child {
display: none;
position: relative;
}
.main:hover + .child {
display: block;
color: red;
background-color: yellow;
z-index: 1;
}
<div class="flex-container">
<div class="wrapper">
<p class="main">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
<p class="main">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
<p class="main">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
</div>
<div class="wrapper"></div>
</div>
英文:
Is it possible to make the hover
element not to push the other elements down thus expanding the whole flexbox but instead have it to overlap the other elements and hide them with its body?
I tried z-index but didn't succeed.
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
.flex-container {
display: flex;
justify-content: space-evenly;
flex: auto;
}
.flex-container>div {
background-color: #f1f1f1;
width: 100%;
margin: 10px;
line-height: 15px;
font-size: 15px;
border-radius: 10px;
overflow: hidden;
position: relative;
}
.child {
display: none;
position: relative;
}
.main:hover+.child {
display: block;
color: red;
background-color: yellow;
z-index: 1;
}
<!-- language: lang-html -->
<div class="flex-container">
<div class="wrapper">
<p class="main">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
<p class="main">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
<p class="main">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
</div>
<div class="wrapper"></div>
</div>
<!-- end snippet -->
答案1
得分: 1
在您的示例中,您必须将光标悬停在 p 元素上,子元素会展开或显示,但当您释放光标离开 p 元素时,子元素会隐藏。因此,请尝试以下代码并更改一些代码结构以满足您的要求。
.flex-container {
display: flex;
justify-content: space-evenly;
flex: auto;
}
.flex-container>div {
background-color: #f1f1f1;
width: 100%;
margin: 10px;
line-height: 15px;
font-size: 15px;
border-radius: 10px;
overflow: hidden;
position: relative;
}
.child {
display: none;
position: relative;
}
.main {
padding: 10px;
}
.main p {
margin: 0;
}
.main:hover>.child {
display: block;
color: red;
background-color: yellow;
z-index: 1;
margin-top: 10px;
}
<div class="flex-container">
<div class="wrapper">
<div class="main">
<p class="main-test">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
</div>
<div class="main">
<p class="main-test">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
</div>
<div class="main">
<p class="main-test">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
</div>
</div>
<div class="wrapper">
</div>
</div>
英文:
In your example, you must hover over the p element, and the child element expands or shows, but when you release your cursor from the p element, the child element hides, so try the below code and change some code structure to fulfill your requirement.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.flex-container {
display: flex;
justify-content: space-evenly;
flex: auto;
}
.flex-container>div {
background-color: #f1f1f1;
width: 100%;
margin: 10px;
line-height: 15px;
font-size: 15px;
border-radius: 10px;
overflow: hidden;
position: relative;
}
.child {
display: none;
position: relative;
}
.main {
padding: 10px;
}
.main p {
margin: 0;
}
.main:hover>.child {
display: block;
color: red;
background-color: yellow;
z-index: 1;
margin-top: 10px;
}
<!-- language: lang-html -->
<div class="flex-container">
<div class="wrapper">
<div class="main">
<p class="main-test">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
</div>
<div class="main">
<p class="main-test">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
</div>
<div class="main">
<p class="main-test">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</p>
<div class="child">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem, possimus, a! Accusantium temporibus culpa neque ullam libero ipsam, nesciunt id quisquam omnis, possimus error praesentium eum earum dignissimos, quaerat numquam!
</p>
</div>
</div>
</div>
<div class="wrapper">
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论