CSS:div with display:table;(CSS: div with display:table; within a list element no auto height?)

编程入门 行业动态 更新时间:2024-10-11 01:11:42
CSS:div with display:table;(CSS: div with display:table; within a list element no auto height?)

我有一个无序列表ul li其中每个li都浮动但由于设置overflow:hidden;而具有高度值overflow:hidden; 到列表项。

因此,当将元素悬停在webinspector中时,list-item的高度值明显为288px。

在每个li我还有一个具有以下属性的div.caption :

.caption {
  opacity: 0;
  position: absolute;
  height: 100%;
  left: 0;
  top: 0;
  width: 101%;
  text-align: center;
  background-color: rgba(0,0,0,.6);
  display: table;
} 
  
<li>
  <a href="#"><img src="#"></a>
  <div class="caption">some more elements in here</div>
</li> 
  
 

由于li内的图像的高度,如上所述的列表项的高度是自动的。 (webinspector清楚地显示列表项具有高度)

但是我的li中的.caption div不会自动高于父级。 我希望div.caption与列表项一样高100%。

我能在这做什么?

示例: http : //jsfiddle.net/rgwcL8qt/1/叠加层应始终覆盖整个图像。

I have an unordered list ul li where each li is floated but has a height value due to setting overflow:hidden; to the list-item.

So when hovering the element in the webinspector the list-item clearly has a height value of 288px.

Within each li i also have a div.caption with the following attributes:

.caption {
  opacity: 0;
  position: absolute;
  height: 100%;
  left: 0;
  top: 0;
  width: 101%;
  text-align: center;
  background-color: rgba(0,0,0,.6);
  display: table;
} 
  
<li>
  <a href="#"><img src="#"></a>
  <div class="caption">some more elements in here</div>
</li> 
  
 

The height of the list-item as mentioned above is automatic due to the height of the image within the li. (the webinspector clearly shows that the list-item has a height)

However my .caption div within the li is not automatically as high as the parent. I want the div.caption to be 100% as high as the list item.

What can I do here?

Example: http://jsfiddle.net/rgwcL8qt/1/ The overlay should always cover the entire image.

最满意答案

div.caption { display: table }与div.caption-in { display: table-cell } div.caption { display: table }在一起创建一个新的块格式化上下文。

更改为div.caption { display: block; } 应该解决你的问题。

编辑:如果你需要将内容集中在标题div中,那么你应该采取不同的方法:

.caption:before { content: ''; display: inline-block; height: 100%; vertical-align: middle; margin-right: -0.3em; } .caption { opacity: 0; z-index: 1; position: absolute; height: 100%; left: 0; top: 0; width: 101%; text-align: center; background-color: rgba(0,0,0,.6); display: block; cursor: pointer; } .caption-in { display: inline-block; vertical-align: middle; text-align: center; }

http://jsfiddle.net/vmg8jfed/

div.caption { display: table } with div.caption-in { display: table-cell } within creates a new block formatting context.

Changing to div.caption { display: block; } should solve your problem.

EDIT: Should you need to center the content inside the caption div then you ought to take a different approach:

.caption:before { content: ''; display: inline-block; height: 100%; vertical-align: middle; margin-right: -0.3em; } .caption { opacity: 0; z-index: 1; position: absolute; height: 100%; left: 0; top: 0; width: 101%; text-align: center; background-color: rgba(0,0,0,.6); display: block; cursor: pointer; } .caption-in { display: inline-block; vertical-align: middle; text-align: center; }

http://jsfiddle.net/vmg8jfed/

更多推荐

本文发布于:2023-07-30 14:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:display   table   CSS   div   auto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!