根据文本框自动调整图像高度

时间:2015-02-28 11:49:30

标签: html css image height

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

首先,图像应与文本框的高度相匹配 first, the image should match the height of the textbox

调整窗口大小后,调整图片的高度 As the window is resized the picture's height is adjusted

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

INDEX.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%;
}

2 个答案:

答案 0 :(得分:0)

如果您不关心图像宽高比,这可以从您的要求中看出来,您可以试试这个:

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标记

答案 1 :(得分:0)

这样的事情怎么样:

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%
}

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

这样,您可以保持纵横比,但文字和图像的宽度将无法适应。或者也许人们可以找到一种方式:)

相关问题