[英]p tag inside of a div tag
This is a two part problem. 这是一个两部分的问题。 The first part is I want the image fade and the text fade to happen at the same time (on a hover over).
第一部分是我希望图像褪色和文本褪色同时发生(将鼠标悬停在上面)。
The second part is, I thought if I had the <p>
tag inside of the <div>
then it would "inherit" (I may not be using the term properly) the div properties. 第二部分是,我想如果我在
<div>
内有<p>
标记,那么它将“继承” div属性(我可能未正确使用该术语)。 The text is supposed to remain fixed center of the image so anytime the image resizes or moves it takes the text with it. 文本应该保持在图像的固定中心,因此,只要图像调整大小或移动,它就会随文本一起移动。
Clearly neither one of those things are happening. 显然,这些事情都没有发生。
.image-wrapper p { position: absolute; left: 250px; top: 130px; padding: 10px; border: 1px solid #FFF; opacity: 0; } .image-wrapper p:hover { position: absolute; left: 250px; top: 130px; padding: 10px; border: 1px solid #FFF; opacity: 100; transition: opacity .25s ease-in-out; -moz-transition: opacity .25s ease-in-out; -webkit-transition: opacity .25s ease-in-out; } .fade { opacity: 1; transition: opacity .25s ease-in-out; -moz-transition: opacity .25s ease-in-out; -webkit-transition: opacity .25s ease-in-out; } .fade:hover { opacity: 0.5; }
<div class="image-wrapper"> <img src="http://placehold.it/150x80?text=IMAGE" class="fade" style="width:100%" alt="Image"> <p>This text here</p> </div>
for first one you can use like that 对于第一个,你可以这样使用
.image-wrapper:hover{
opacity :0.7;
}
Both 1.) and 2.) - Try this code: 1.)和2.)-尝试以下代码:
<div class="image-wrapper"> <img src="http://placehold.it/150x80?text=IMAGE" class="fade" style="width:100%" alt="Image"> <p class="fade">This text here</p> </div>
Adding the class="fade"
to the <p>
will make the transition smoothly for the text as well. 在
<p>
添加class="fade"
也会使文本的过渡也顺畅。
CSS: CSS:
.image-wrapper p { display: none; color: #F00; position: absolute; left: 250px; top: 130px; padding: 10px; border: 1px solid #FFF; opacity: 0; } .image-wrapper p:hover, .image-wrapper:hover p { display: block; position: absolute; left: 250px; top: 130px; padding: 10px; border: 1px solid #FFF; opacity: 100; transition: opacity .25s ease-in-out; -moz-transition: opacity .25s ease-in-out; -webkit-transition: opacity .25s ease-in-out; } // When either hovering the p or the image, apply the transition. .fade { opacity: 1; transition: opacity .25s ease-in-out; -moz-transition: opacity .25s ease-in-out; -webkit-transition: opacity .25s ease-in-out; } .fade:hover { opacity: 0.5; }
Check this out and let me know your feedback. 检查一下,让我知道您的反馈。 Thanks!
谢谢!
- The first part is I want the image fade and the text fade to happen at the same time (on a hover over).
第一部分是我希望图像褪色和文本褪色同时发生(将鼠标悬停在上面)。
For this to happen, add hover
to image-wrapper
rather than individually to p
and the img
like the below for instance: 为此,请将
hover
添加到image-wrapper
而不是将其单独添加到p
和img
中,例如以下所示:
.image-wrapper:hover p {
opacity: 1;
}
.image-wrapper:hover .fade {
opacity: 0.5;
}
- The text is supposed to remain fixed center of the image so anytime the image resizes or moves it takes the text with it.
文本应该保持在图像的固定中心,因此,只要图像调整大小或移动,它就会随文本一起移动。
For this you can use the transform
to center align the text over the image responsively. 为此,您可以使用
transform
使文本在图像上居中对齐。
.image-wrapper p {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
snippet below: 下面的代码段:
.image-wrapper{ position: relative; } .image-wrapper p { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); padding: 10px; border: 1px solid #FFF; opacity: 0; transition: opacity .25s ease-in-out; -moz-transition: opacity .25s ease-in-out; -webkit-transition: opacity .25s ease-in-out; } .image-wrapper:hover p { opacity: 1; } .fade { opacity: 1; transition: opacity .25s ease-in-out; -moz-transition: opacity .25s ease-in-out; -webkit-transition: opacity .25s ease-in-out; } .image-wrapper:hover .fade { opacity: 0.5; }
<div class="image-wrapper"> <img src="http://placehold.it/150x80?text=IMAGE" class="fade" style="width:100%" alt="Image"> <p>This text here</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.