html - 将文本在缩略图的中间垂直对齐

标签 html css image hover

我有几个悬停时具有多色悬停样式的缩略图(看起来像这样,黄色缩略图是您将鼠标悬停在其上时的样子)enter image description here

唯一的问题是我无法在不破坏其他内容的情况下让内部文本在缩略图中间垂直对齐。当我将 .thumb 更改为 display: table;和 .align-mid 显示:表格单元格;垂直对齐:中间;文本对齐到中间但背景颜色变得不透明。看起来像这样: enter image description here

我似乎无法弄清楚如何实现这一目标。

HTML:

<div class="thumb" onclick="location.href='{{ cms:page:thumb_one.link:string }}'">
    {{ cms:page_file:thumb_one.image:image}}
    <div class="align-mid">
        {{ cms:page:thumb_one.text:string }}<br>
        {{ cms:page:thumb_one.description:string }}
    </div>
    <div class="yellow">
    </div>
</div>

CSS:

.thumb {
   -webkit-border-radius: 12px;
   -moz-border-radius: 12px;
   border-radius: 12px;
   display:inline-block;
   position: relative;
   height: 170px;
   width: 235px;
   overflow: hidden;
}
.thumb:after {
   background: rgba(255,255,255,.7);
   content:'';
   display: block;
   height: 170px;
   left: 0;
   opacity: 0;
   padding: 20px;
   position: absolute;
   top: 0;
   width: 100%;
   z-index: 1;
}

.thumb:hover:after {
   opacity: 1;
   padding: 20px;
   transition: opacity .4s;
}

.thumb:hover > .align-mid {
   background-color: transparent;
   color: white;
}

.thumb:hover {
   cursor: pointer;
}

.thumb img {
     height: 100%;
     width: 100%;
}   

.yellow {
     opacity: 0;
}  

.thumb:hover .yellow {
   background: rgba(255,213,43,.8);
   content:'';
   display: block;
   left: 13px;
   right: 13px;
   bottom: 13px;
   top: 13px;
   opacity: 1;
   position: absolute;
   transition: opacity .4s;
   z-index: 2;
}

.align-mid {
   background-color: rgba(0,0,0,.5);
   color: rgb(255,213,43);
   height: auto;
   padding-top: 10px;
   padding-bottom: 10px;
   position: relative;
   top: -105px;
   width: 100%;
   z-index: 3;
}

最佳答案

很明显,当您的包装器有多个子项时,并且所有子项都被视为表格单元格,如果您不希望它们垂直堆叠(y 轴),则必须为每个子项分配绝对定位,这样它们将堆叠在一起(z 轴)

所以一个简单的解决方案是:

.thumb img { height: 100%; width: 100%; position:absolute; } 

关于html - 将文本在缩略图的中间垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23124281/

相关文章:

html - 为什么我的标题标签没有出现在使用 Ruby on Rails 的搜索结果中?

javascript - 在移动设备上删除 'sticky navigation'

css - 一个媒体查询不会覆盖原始 CSS

jquery - 如何在 Logo 的 Bootstrap 导航栏中心创建一个半圆?

php - 显示 PHP mysql 表中的图像

html - 如何为图像制作边框

jquery - 无法点击正文背景图片进行重定向

html - 从 HTML 文档树打印 CSS 的算法

javascript - 在iframe中调整网页宽度

python - 对如何使用 numpy.pad 向我的 numpy imagearray 正确添加白色边框感到困惑