【问题标题】:Show a thumbnail caption on hover centered在悬停居中显示缩略图标题
【发布时间】:2015-01-29 18:41:26
【问题描述】:

我试图在悬停居中显示缩略图标题:水平和垂直对齐,但不成功。

在小提琴中,您会看到一个响应式缩略图网格,悬停时带有标题。我给了它一个红色的虚线边框,所以你可以看到它与缩略图的大小不同。我无法让标题标题在缩略图中居中。也许我的缩略图网格需要不同的构建。希望有人可以帮助我。

Here is the fiddle

#content {
position: relative;
display: inline-block;
margin: 110px 40px 100px 40px;
background-color: transparent;
}

.col-25 {
position: relative;
float: left;
width: 25%;
margin: 0;
}

.thumb {
display: block;
margin: 10px;
}

.col-25 img {
width: 100%;
height: auto;
}

.col-25 a:hover img {
opacity: 0.1;
}

.caption {
position: absolute;
top:0;
left:0;
border: dashed 1px red;
width:100%;
height:100%;
opacity: 0;
text-align:center;
vertical-align: middle;
z-index:2;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
-ms-transition: all 0.3s;
-o-transition: all 0.3s;
transition: all 0.3s;
}

.col-25 a:hover .caption {
opacity: 1;
}

【问题讨论】:

    标签: html css hover caption


    【解决方案1】:

    http://jsfiddle.net/a0zwecou/14/

    我在标题中添加了一个内部 div,并为其设置了 25% 的边距高度。删除媒体查询中宽度极小的边距。

        <div class="caption">
              <div class="inner-caption">
                  <h3>Untitled for now</h3>
                  <p>Caption</p>
              </div>
        </div>
    

    CSS -

    .inner-caption{margin-top:25%;}
    

    【讨论】:

    • 谢谢你的作品最好!是否可以删除标题的外边距,以便在使用背景颜色时与缩略图大小相同?
    • 我能想到的最好的方法是将图像作为缩略图的背景,高度为 100%,宽度为 100%。然后在悬停时移除背景。
    • http://jsfiddle.net/a0zwecou/22/ 我想这就是你想要的。
    • 谢谢,但缩略图下方仍有一些空间,现在它会将其下方的缩略图推开。
    【解决方案2】:

    JSFIIDLE

    仅通过更改高度和向标题类添加填充。

    .caption {
    position: absolute;
    padding-top:25%;
    top:0;
    left:0;
    border: dashed 1px red;
    width:100%;
    height:60%;
    opacity: 0;
    text-align:center;
    vertical-align: middle;
    z-index:2;
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    -ms-transition: all 0.3s;
    -o-transition: all 0.3s;
    transition: all 0.3s;
    }
    

    【讨论】:

    • 感谢侯赛因!看起来它有效且简单。但是你说改变宽度,但你的意思是正确的高度?
    • 是否可以在为标题使用背景颜色时完全覆盖缩略图大小?现在它与缩略图重叠。
    • 移除边距:10px;来自拇指类。
    • 这不会导致缩略图网格折叠,因此没有 20 像素(10+10 像素)的间隙。
    【解决方案3】:

    试试这个,

    .thumb:hover{border:1px dashed red;background-color:red;}
    
    .caption {
    position: absolute;
    top:40%;
    left:0;
    /*border: dashed 1px red;*/ //remove it
    width:100%;
    /*height:100%;*/ // remove it
    opacity: 0;
    text-align:center;
    vertical-align: middle;
    z-index:2;
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    -ms-transition: all 0.3s;
    -o-transition: all 0.3s;
     transition: all 0.3s;
     }
    

    小提琴:http://jsfiddle.net/a0zwecou/20/

    【讨论】:

    • 谢谢,但不是真的居中
    • 40% 居中,但在使用背景颜色作为标题时是否可以完全覆盖缩略图大小?
    • 检查我的拇指悬停背景颜色的更新答案。小提琴:jsfiddle.net/a0zwecou/20
    【解决方案4】:

    请看这里的小提琴: http://jsfiddle.net/a0zwecou/13/ 并添加css:

    .caption h3 {
        margin-top: 59px;
    }
    

    【讨论】:

      【解决方案5】:

      您可以简单地将您的标题包装在另一个 div 中并使用 transate 将其居中(当然要使用所有前缀),或者您可以使用表格(表格 + 表格单元格)简单地将其居中
      HERE THE PEN,将第一个悬停.

      .insideCap{
        background: red;
        top: 50%;
        position: absolute;
        width: 100%;
        transform: translateY(-50%);
      }
      

      其他方式:

      parent{
        dispaly:table;
      }
      son{
        display: table-cell;
        vertical-align: middle;
      }
      

      【讨论】:

        【解决方案6】:
        you can try this:
        <div id="content">
        
        
        HTML:
        Add this div:
        
            <div class="col-25">
                    <a class="thumb" href="#">
                        <img src="http://fakeimg.pl/500x310/999/">
                            <div class="caption"><div class="caption-text"><h3>Untitled for now</h3><p>Caption</p></div></div>
                    </a>
                </div>
        
        CSS:
        add this css in your style:
        .caption-text {
            left: 0;
            position: absolute;
            right: 0;
            top: 32%;
        }   
        

        JS Fiddle DEMO

        【讨论】:

        • 谢谢,但没有达到我想要的效果。
        猜你喜欢
        • 2015-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-07
        • 1970-01-01
        • 1970-01-01
        • 2017-03-31
        • 1970-01-01
        相关资源
        最近更新 更多