【问题标题】:Auto-adjust image height depending on textbox根据文本框自动调整图像高度
【发布时间】:2015-02-28 20:12:13
【问题描述】:

我正在寻找一种根据文本框大小自动调整图像高度的方法。文本框的高度会随着浏览器窗口的大小而改变(

首先,图片应该与文本框的高度相匹配

随着窗口大小的调整,图片的高度也随之调整

到目前为止,这是我的 html 和 css 代码:

索引.HTML

<div class="wrapper">
    <img id="about" src="images/about.jpg">
    <div class="textbox">
        Lorem ipsum dolor sit amet....
    </div>
</div>

MAIN.CSS

.wrapper {
}

#about {
  float:right;
  padding-left: 25px;
}

.textbox {
  width: 50%;
}

【问题讨论】:

    标签: html css image height


    【解决方案1】:

    如果您不关心从您询问的内容中可以看出的图像纵横比,您可以试试这个:

    CSS:

    html, body {
        width:100%;
        height:100%;
    }
    .container {
        font-size:0;
        border:1px solid red;
        display:table;
    }
    .container>div {
        display:table-cell;
        width:50%;
        font-size:12px;
        height:inherit;
    }
    .container>div.rhs {
        background-image:url(http://www.lorempixel.com/600/200/sports/1/);
        background-size:cover;
        background-repeat:no-repeat;
    }
    

    HTML:

    <div class="container">
        <div class="lhs">Dummy Text</div>
        <div class="rhs"></div>
    </div>
    

    演示:http://jsfiddle.net/GCu2D/588/

    技巧是在 css 中使用与 background-image 相同的图像,而不是使用 img 标签

    【讨论】:

      【解决方案2】:

      这样的事情怎么样:

      http://codepen.io/jlowcs/pen/MYXXyv

      HTML:

      <div class="wrapper">
          <div class="textbox">
            <p>Lorem ipsum dolor sit amet...</p>
          </div
          ><img id="about" src=images/about.jpg">
      </div>
      

      CSS:

      .wrapper {
        position: relative;
      }
      
      .textbox {
        display: inline-block;
        width: 50%;
      }
      
      #about {
        position: absolute;
        padding-left: 25px;
        box-sizing: border-box;
        max-height: 100%;
        max-width: 50%
      }
      

      不要使用浮动,而是使用绝对定位。只是一个想法,可能有更好的方法。

      这样,你可以保持纵横比,但文字和图像的宽度不会适应。或者也许有人可以找到一种方法:)

      【讨论】:

      • 这是一个很好的答案,完美地解决了我的问题!谢谢
      猜你喜欢
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 2012-05-21
      • 2020-11-23
      • 2012-09-21
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      相关资源
      最近更新 更多