简体   繁体   English

响应式图像上的Bootstrap和文本

[英]Bootstrap and text on responsive image

I am trying to accomplish something like this: 我想要完成这样的事情:

在此输入图像描述

As I am using Bootstrap, for now I have this: 当我使用Bootstrap时,我现在有这个:

<div class="col-md-3">
        <div class="test">
            <%= (image_tag("http://example.com.image.jpg", :class => "img-responsive")) %>
            <h2>hello</h2>
        </div>
    </div>
</div>

CSS: CSS:

.test { 
   position: relative;

   h2 {
   background-color: #bebebe;
   position: absolute;
   bottom: 0px; 
   left: 0px; 
   width: 100%;
  }
}

This works well for big and medium screens but the problem is on smaller screens. 这适用于大屏幕和中等屏幕,但问题出在较小的屏幕上。 After all elements stack to one column the problem appears with smaller images. 在所有元素堆叠到一列后,问题会出现较小的图像。 On bigger screens where there are multiple elements in one row there is no problem, text appears on the bottom of the image. 在较大的屏幕上,一行中有多个元素没有问题,文本显示在图像的底部。 The problem is that some images are not big enough on smaller screens as they need to have width 100% of the screen and they are only 300-400ox wide. 问题是一些图像在较小的屏幕上不够大,因为它们需要宽度为100%的屏幕而且它们只有300-400ox宽。 Because of that they don't use 100% of the screen, but header does use it . 因为他们不使用100%的屏幕,但标头确实使用它

It looks something like this: 它看起来像这样:

     **********************************
     *                                *
     *                                *
     *            Image               *
     *                                *
     *                                *
********************************************
*                Header                    *
********************************************

So, header spreads all the way to the left and right of the screen and jumps outside of the image. 因此,标题一直传播到屏幕的左侧和右侧,并跳转到图像之外。 How to keep header inside of the image no matter of screen size? 无论屏幕尺寸如何,如何将标题保留在图像内?

Thank you! 谢谢!

Try this: 尝试这个:

Demo : http://jsfiddle.net/lotusgodkk/bm3mjhm1/ 演示: http//jsfiddle.net/lotusgodkk/bm3mjhm1/

CSS: CSS:

.test {
    position: relative;
    display:inline-block; //Add display:inline-block; if it doesn't affect your code.
}
img {
    max-width:100%;
}
h2 {
    background-color: #bebebe;
    position: absolute;
    bottom: 0;
    left: 0;
    right:0; // Remove width:100% and add left,right offset.
}

HTML: HTML:

<div class="test">
    <img src="http://lorempixel.com/400/200/sports/1/" />
     <h2>Sample TExt. Sample TExt</h2>
</div>

Check the demo for different sizes of image. 检查演示,了解不同尺寸的图像。

DEMO: http://jsbin.com/nobape 演示: http//jsbin.com/nobape

在此输入图像描述

There are two options. 有两种选择。 The first is the answer, but it's not the best idea. 第一个是答案,但这不是最好的主意。 When the viewport gets very small, the caption text can cover up a good portion of the image. 当视口变得非常小时,标题文本可以掩盖图像的大部分。 When you look at the demo, the second row only does the caption over the image at the 500px min-width and under that, it stacks, which looks very nice and easy to read. 当您查看演示时,第二行仅在500px最小宽度处对图像执行标题,并且在其下方,它堆叠,看起来非常好并且易​​于阅读。

CSS CSS

.img-full-width {
    width: 100%;
    height: auto;
}
.caption {
    position: relative;
    margin-bottom: 2%;
}
.caption h3 {
    margin: 0;
    color: #fff;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 10px;
    background: rgba(41,82,123,.8);
    font-size: 15px;
}


.caption-2 {
    position: relative;
    margin-bottom: 2%;
}
.caption-2 h3 {
    margin: -1px 0 0 0;
    color: #fff;
    padding: 10px;
    background: rgba(41,82,123,1);
    font-size: 15px;
}
@media (min-width:500px) { 
    .caption-2 h3 {
        margin:0;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(41,82,123,.8);
    }
}

HTML HTML

 <div class="container">
  <div class="row">
     <div class="col-sm-4">
        <div class="caption">
           <img src="http://placekitten.com/g/400/300" class="img-full-width alt="" />
           <h3>My Caption Goes Here</h3>
        </div>
     </div>
     <div class="col-sm-4">
        <div class="caption">
           <img src="http://placekitten.com/g/400/300" class="img-full-width alt="" />
           <h3>My Caption Goes Here</h3>
        </div>
     </div>
     <div class="col-sm-4">
        <div class="caption">
           <img src="http://placekitten.com/g/400/300" class="img-full-width alt="" />
           <h3>My Caption Goes Here</h3>
        </div>
     </div>
  </div>

  <hr>

  <div class="row">
     <div class="col-sm-4">
        <div class="caption-2">
           <img src="http://placekitten.com/g/400/300" class="img-full-width alt="" />
           <h3>My Caption Goes Here</h3>
        </div>
     </div>
     <div class="col-sm-4">
        <div class="caption-2">
           <img src="http://placekitten.com/g/400/300" class="img-full-width alt="" />
           <h3>My Caption Goes Here</h3>
        </div>
     </div>
     <div class="col-sm-4">
        <div class="caption-2">
           <img src="http://placekitten.com/g/400/300" class="img-full-width alt="" />
           <h3>My Caption Goes Here</h3>
        </div>
     </div>
  </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM