【问题标题】:How to make Bootstrap img-responsive stay within the boundaries of it's parent div如何使 Bootstrap img-responsive 保持在其父 div 的边界内
【发布时间】:2019-07-06 15:54:01
【问题描述】:

使用 Bootstrap 3,我试图使图像保持在其父 div 的边界内。 这适用于宽度,但无论我尝试什么,图像总是从父 div 的底部掉出来,因为当我降低浏览器高度时它的高度不会更新。有可能完成这项工作吗?如果是这样,如何?谢谢。

JSFiddle : http://jsfiddle.net/rensdenobel/9486t/

HTML:

<div class="container">
    <div class="row">
        <div class="col-sm-3 col-md-3 col-lg-4"></div>
        <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4 center">
            <div class="content">
                <div id="text">You chose this photo :</div>            
                <div id="thumbnail-container">
                    <img id="thumbnail-image" class="img-responsive" src="http://placehold.it/800x800" />
                </div>
                <div id="button-share">
                        <button class="btn btn-success">Share it !</button>
                </div>        
            </div>
        </div>
        <div class="col-sm-3 col-md-3 col-lg-4"></div>        
    <div>
</div>

CSS:

html, body {
    height:100%;
}

.container {
    height:100%;
    width:100%;
    border:1px solid blue;
}

.row {
    border:1px solid green;
    height:100%;
}

.center {
    display:table;
    border:1px solid red;
    padding-top:12px;
    padding-bottom:12px;
    text-align:center;
    height:100%;
    max-height:100%;
}

.content {
    display:table-cell;
    vertical-align:middle;
    text-align:center;
    height:auto;
    border:1px solid green;
}

#thumbnail-image {
    margin-left:auto;
    margin-right:auto;
    height:auto;
}

#thumbnail-container{
    width:100%;   
    border:1px solid pink;
    max-height:70%;
    height:70%;
    margin-top:12px;
    margin-bottom:12px;    
 }

【问题讨论】:

标签: css twitter-bootstrap


【解决方案1】:

试试

<div id="thumbnail-container col-sm-4 col-xs-4">
   <img id="thumbnail-image" class="img-responsive" src="http://placehold.it/800x800" />
 </div>

更改 col-sm-*col-xs-* 以获得所需的输出

【讨论】:

  • 谢谢!似乎不起作用,在小提琴中尝试一下:jsfiddle.net/rensdenobel/9486t
  • 你用什么浏览器?当我调整屏幕大小时,高度小于图像时,图像会落在屏幕上。
  • 我用的是firefox,你用的是IE吗?
  • Firefox 和 Chrome 的结果相同
  • 对于新读者:讨论的结果是,使用建议的解决方案,高度保持不变。到目前为止,还没有解决问题的办法。
【解决方案2】:

这种方法对我有用。请尝试将此添加到您的 CSS:

.container {
     height:max-content;
}    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多