简体   繁体   English

Div 占据页面的整个宽度

[英]Div taking up full width of page

I've implemented a hover state that overlays text when a div is hovered.我已经实现了一个悬停状态,当一个 div 悬停时,它会覆盖文本。

Here is the code -这是代码 -

 .desktop-image { position: relative; } .img_description { position: absolute; top: -13%; bottom: 0; left: 0; right: 0; color: #000; visibility: hidden; opacity: 0; transition: opacity .7s, visibility .7s; } .desktop-image:hover .img_description { visibility: visible; opacity: 1; }
 <div id="images" class="misma"> <div class="desktop-image"> <img src="http://via.placeholder.com/200x200"> <p class="img_description">This image looks super neat.</p> </div> <div class="mobile-image-misma"> <img src="http://via.placeholder.com/100x100"> </div> </div>

The problem When I inspect the page I notice that the div .desktop-image is taking up the full width of the screen (see pic).问题当我检查页面时,我注意到 div .desktop-image占据了屏幕的整个宽度(见图)。

在此处输入图片说明

Question How can I make this div wrap to the size of the actual image, so that the hover state is ONLY implemented when that image is hovered, as opposed to when anywhere within the blue section is hovered.问题如何使这个 div 环绕到实际图像的大小,以便仅在悬停该图像时实现悬停状态,而不是悬停在蓝色部分内的任何位置。

Thanks谢谢

By default div 's are defined with display: block , meaning that they will take the entire available width.默认情况下div是用display: block定义的,这意味着它们将占用整个可用宽度。

You can specify that .desktop-image will be display: inline-block;您可以指定.desktop-image将被display: inline-block; and you will get the wanted result.你会得到想要的结果。

My suggestion to you is to use semantic HTML, there are 2 element that are dedicated to what you trying to achieve figure & figcaption .我对您的建议是使用语义HTML,有 2 个元素专门用于您尝试实现的内容figure & figcaption

Added an example with them.添加了一个例子。

 .desktop-image { position: relative; display: inline-block; } .desktop-image img { vertical-align: middle; } .img_description { position: absolute; top: 0; bottom: 0; left: 0; right: 0; color: #000; visibility: hidden; opacity: 0; transition: opacity .7s, visibility .7s; } .desktop-image:hover .img_description { visibility: visible; opacity: 1; }
 <div id="images" class="misma"> <div class="desktop-image"> <img src="http://via.placeholder.com/200x200"> <p class="img_description">This image looks super neat.</p> </div> <div class="mobile-image-misma"> <img src="http://via.placeholder.com/100x100"> </div> <figure class="desktop-image"> <img src="http://via.placeholder.com/200x200"> <figcaption class="img_description">This image looks super neat.</figcaption> </figure> </div>

Please see this.请看这个。

 .desktop-image { position: relative; display: inline-block; } .img_description { position: absolute; top: -13%; bottom: 0; left: 0; color: #000; visibility: hidden; opacity: 0; transition: opacity .7s, visibility .7s; right:0; } .desktop-image:hover .img_description { visibility: visible; opacity: 1; }
 <div id="images" class="misma"> <div class="desktop-image"> <img src="http://via.placeholder.com/200x200"> <p class="img_description">This image looks super neat.</p> </div> <div class="mobile-image-misma"> <img src="http://via.placeholder.com/100x100"> </div> </div>

By default div tag has a default display property display: block默认div标签有一个默认的显示属性display: block

In your code <div class="desktop-image"> div appear full width of the display在您的代码<div class="desktop-image"> div 中显示全宽

set a width: and a min-height: property to solve the problem设置一个width:和一个min-height:属性来解决问题

.desktop-image {
   position: relative;
   width: 200px; /*new*/
   height: 200px; /*new*/
} 

将 CSS 属性clear: both添加到带有desktop-image类的 div 中。

Specify the width of the .desktop-image class in percentage like .desktop-image{width:70%;} or whatever percentage suit the page design.以百分比形式指定 .desktop-image 类的宽度,例如.desktop-image{width:70%;}或任何适合页面设计的百分比。

By doing that image will remain in this .desktop-image div.通过这样做,该图像将保留在此 .desktop-image div 中。

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

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