Bootstrap 3 CSS图像标题叠加

con*_*men 12 css image-gallery twitter-bootstrap-3

我使用bootstrap并且在图像上覆盖标题时出现问题,标题div无法放入框中,如下所示:

在此输入图像描述

HTML:

<div class="col-sm-4">
    <a href="#">
        <img src="images/upload/projects/21/wolf.jpg" class="img-responsive" alt="" />
        <div class="desc">
            <p class="desc_content">The pack, the basic unit of wolf social life.</p>
        </div>
    </a>
</div>

<div class="col-sm-4">
    <a href="#">
        <img src="images/upload/projects/21/cake.png" class="img-responsive" alt="">
        <div class="desc">
            <p class="desc_content">The pack, the basic unit of wolf social life.</p>
        </div>
    </a>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

div.desc{
    position: absolute;
    bottom: 0px;
    width: 100%;
    background-color: #000;
    color: #fff;
    opacity: 0.5;
    filter: alpha(opacity=50);
}
Run Code Online (Sandbox Code Playgroud)

解:

感谢@himanshu解决了这个问题.

div.desc{
    background-color: #000;
    bottom: 0;
    color: #fff;
    left: 0;
    opacity: 0.5;
    position: absolute;
    width: 100%;
}

.fix{
    width: 100%;
    padding: 0px;
}
Run Code Online (Sandbox Code Playgroud)

小智 3

我认为问题在于放置而不是重叠,div.desc这是绝对的,图像是它的兄弟。因此,覆盖不会跟随图像,它只会跟随锚标记或您的div.wrapper.

据我所见,图像上有边距,锚点​​或包装器中有填充。

最好的解决方案是将desc图像放在另一个div100% 宽度、0 填充的图像中。

<div class="col-sm-4 wrapper">
    <a href="#">
<div class="fix">
        <img src="images/upload/projects/21/wolf.jpg" class="img-responsive" alt="" />
        <div class="desc">
            <p class="desc_content">The pack, the basic unit of wolf social life.</p>
        </div></div>
    </a>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.fix{width:100%; padding:0px;}
Run Code Online (Sandbox Code Playgroud)