Bootstrap 3 CSS图像标题叠加

时间:2014-09-24 06:43:18

标签: css twitter-bootstrap-3 image-gallery

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

enter image description here

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>

CSS:

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

SOLUTION:

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

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

.fix{
    width: 100%;
    padding: 0px;
}

5 个答案:

答案 0 :(得分:3)

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

从我所看到的是,在锚点或包装器中的图像或填充上有一个边距。

最佳解决方案是将desc和图像放在另一个div中,宽度为100%,填充为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>

CSS:

.fix{width:100%; padding:0px;}

答案 1 :(得分:2)

试试这个

 <div class="wrapper">
    <div class="">
        <img src="images/upload/projects/21/wolf.jpg" class="img-responsive" alt="" >
    </div>
    <div class="desc_content" style="">The pack, the basic unit of wolf social life.</div>
 </div>
 <div class="wrapper">
    <div class="">
         <img src="images/upload/projects/21/wolf.jpg" class="img-responsive" alt="" >
    </div>
    <div class="desc_content" style="">The pack, the basic unit of wolf social life.</div>
 </div>

CSS

div.desc_content {
background-color: #000;
color: #FFF;
font: bold 11px verdana;
padding-left: 10px;
padding-top: 7px;
height: 23px; opacity: 0.7;
position: relative; 
top: -30px;
}
div.wrapper{
    float: left;
    position: relative;
    margin: 10px;
 }

答案 2 :(得分:2)

对于试图在Bootstrap中覆盖图片标题的其他人,请考虑使用轮播字幕。

见这里:http://www.w3schools.com/bootstrap/bootstrap_carousel.asp

glob('/images/*');

答案 3 :(得分:1)

将此属性用于div.desc类

div.desc{
    position: absolute;
    bottom: 0;
    background-color: #000;
    color: #fff;
    opacity: 0.5;
    left:0;   /** new  **/
    right:0;  /** new  **/
    filter: alpha(opacity=50);
}

答案 4 :(得分:0)

这对我有用。这样,您将在图像底部找到标题。

<div class="container-fluid">   
    <img src="someimg.jpg" class="img-fluid " alt="Patience">   
    <div class="carousel-caption d-block">
        <h1>Some label</h1>
        <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
    </div>
</div>

如果您希望它位于顶部,请在CSS中执行以下操作:

.carousel-caption{
top:0;
bottom:auto;
}