如何使我的图像响应宽度和高度?

时间:2014-09-10 13:30:08

标签: css

我不会像我网站的其余部分一样响应我的图像。如何使其适应容器的大小。不只是宽度,还要调整高度?

图像位于最大宽度为1300像素的容器中,因此我制作了尺寸为400像素高度和1300像素宽度的测试图像

这是jsfiddle所以你可以看看我的意思。
http://jsfiddle.net/z6fj8dtg/2/

    <div id="wrapper">
        <div id="codeback">
        </div>
        <div id="container">

        <div class="nav">
        </div>
        <div id="wrap">
            <div class="banner">
                <img src="http://s12.postimg.org/vnsghsvf1/banner.png" >
            </div><!-- END OF BANNER -->
        </div>
    </div><!-- END OF CONTAINER -->







 body{
        background-color:#272822;
        padding:0;
        margin:0;
    }

        #wrapper{
            width:100%; 
            height:inherit;
        }
        #codeback{
            width:100%;
            height:100%;
            background-image:url('capture.jpg');
              background-repeat: no-repeat;
            position:fixed;
            left:0px;
            top:0px;
            z-index:-1;

        }
        #container{
            width:100%;
            float:right;

        }
        .nav{
            margin-top:200px;
            width:80%;
            max-width:1300px;
            height:50px;
            float:right;
            background-color:black;
            position:relative;

        }
.fixedNav {
    position:fixed;
    margin:0;
    width:80%;
    right:0;
}

        #wrap{
            float:right;
            width:80%;
            max-width:1300px;
            height:1500px;
            background-color:white;
            box-shadow: -1px -1px 35px lightblue;
        }
        .banner{        
            max-width:100%;
        }

它刚刚坐在容器中但向右溢出。

2 个答案:

答案 0 :(得分:9)

是的,根本没问题。只需插入以下CSS:

.banner img {
    width: 100%;
    height: auto; //Auto adjust height (maintain aspect ratio)
}

答案 1 :(得分:0)

我使用了引导程序图像响应类img-fluid和额外的div标签来控制图像大小。

      <div style="width: 20%;height: 100px">
        <img src="http://www.everlastingcelebrations.com/wp-content/uploads/2018/09/Top-Ganesh-Chaturthi-Messages-Images-Photos.jpg" class="img-fluid" alt="...">
      </div>
相关问题