简体   繁体   English

div标签内的p标签

[英]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> 

Here is a link just in case 这是一个链接,以防万一

for first one you can use like that 对于第一个,你可以这样使用

.image-wrapper:hover{
  opacity :0.7;
}

Both 1.) and 2.) - Try this code: 1.)和2.)-尝试以下代码:


HTML: HTML:

 <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! 谢谢!

  1. 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而不是将其单独添加到pimg中,例如以下所示:

.image-wrapper:hover p {
  opacity: 1;
}

.image-wrapper:hover .fade {
  opacity: 0.5;
}
  1. 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> 

如何访问`<div> `标签内的`<p> `标签? </p></div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $("p div").click(function() { $(this).css({ "color": "#F00", "font": "bold 20px verdana", "background-color": "#0FF" }); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;p&gt;Hello&lt;div&gt; World!&lt;/div&gt;&lt;/p&gt;</pre></div></div><p></p><p> 当我单击“世界”文本时,什么也没有发生,当我检查元素时,它说:</p><pre class="lang-html prettyprint-override"> &lt;p&gt;Hello&lt;/p&gt; &lt;div&gt; World&lt;/div&gt; &lt;p&gt;&lt;/p&gt;</pre><p> 注意那些&lt;p&gt;标签。 怎么会这样?</p></div> - How to access a `<div>` tag inside a `<p>` tag?

为什么不能<p>标签包含一个</p><div>标签里面呢?</div><div id="text_translate"><p> 据我所知,这是正确的:</p><pre> &lt;div&gt; &lt;p&gt;some words&lt;/p&gt; &lt;/div&gt;</pre><p> 但这是错误的:</p><pre> &lt;p&gt; &lt;div&gt;some words&lt;/div&gt; &lt;/p&gt;</pre><p> 第一个可以通过<a href="http://validator.w3.org" rel="nofollow noreferrer">W3C 验证器</a>(XHTML 1.0),但第二个不能。 我知道没有人会像第二个那样编写代码。 我只想知道为什么。</p><p> 那么其他标签的包含关系呢?</p></div> - Why can't the <p> tag contain a <div> tag inside it?

暂无
暂无

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

相关问题 如何访问`<div> `标签内的`<p> `标签? </p></div><div id="text_translate"><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $("p div").click(function() { $(this).css({ "color": "#F00", "font": "bold 20px verdana", "background-color": "#0FF" }); });</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;p&gt;Hello&lt;div&gt; World!&lt;/div&gt;&lt;/p&gt;</pre></div></div><p></p><p> 当我单击“世界”文本时,什么也没有发生,当我检查元素时,它说:</p><pre class="lang-html prettyprint-override"> &lt;p&gt;Hello&lt;/p&gt; &lt;div&gt; World&lt;/div&gt; &lt;p&gt;&lt;/p&gt;</pre><p> 注意那些&lt;p&gt;标签。 怎么会这样?</p></div> - How to access a `<div>` tag inside a `<p>` tag? <p></p>在div中标记 - <p> </p> tag in div 造型<p>里面的标签<div>通过使用 CSS - Styling <p> tag inside <div> by using CSS div 或 p 标记在 flex box 包装内 - div or p tag inside of flex box wrapping 在帖子中将鼠标悬停在div内时p标签的不透明度 - opacity of p tag inside div on hover in the post 怎么做 <p> 标签和<a>标签里面</a> <div> <a>排队</a> - how to make <p> tag and <a> tag inside a <div> inline 为什么不能<p>标签包含一个</p><div>标签里面呢?</div><div id="text_translate"><p> 据我所知,这是正确的:</p><pre> &lt;div&gt; &lt;p&gt;some words&lt;/p&gt; &lt;/div&gt;</pre><p> 但这是错误的:</p><pre> &lt;p&gt; &lt;div&gt;some words&lt;/div&gt; &lt;/p&gt;</pre><p> 第一个可以通过<a href="http://validator.w3.org" rel="nofollow noreferrer">W3C 验证器</a>(XHTML 1.0),但第二个不能。 我知道没有人会像第二个那样编写代码。 我只想知道为什么。</p><p> 那么其他标签的包含关系呢?</p></div> - Why can't the <p> tag contain a <div> tag inside it? 在内部添加ap标签时,div标签上方的多余边距 - Unwanted margin above div tag when adding a p tag inside 如何在div标签中分层选择唯一的P标签 - How to select a only a paticular P tag inside div tag hierarchically 是否可以在html中使用p标签内的form和div标签? - Is it possible to use form and div tag inside p tag in html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM