如何根据文本的数量将图像缩小到某些文本上方?

时间:2016-09-27 14:38:01

标签: html css

我想要在某些文字上方显示图像。文本可能很长或很短。在每种情况下,我希望文本粘贴在容器的底部,并且在其上方的图像在必要时缩小,以便两个项目始终适合容器高度的100%。如果需要,图像可以缩小到非常小,但我不希望它超出其自然的100%宽度或高度。

我已经使用position: absolute; bottom: 0;接近我想要的行为,但这只能确保文本从所需位置开始,并且不会调整其上方图像的大小。我怎样才能做到这一点?



#container {
  height: 100vh;
  width: 100vw;
  position: relative;
}
div.image-content img {
  /* how to force vertical resize? */
  max-height: 100%;
  max-width: 100%;
}
div.copy-content {
  /*this fixes the text but the image does not resize */
  position: absolute;
  bottom: 0;
}

<div id="container">

  <!--image-->
  <div class="image-content">
    <img src="http://i.imgur.com/cFypFxX.jpg" alt="Cat">
  </div>

  <!--text-->
  <div class="copy-content">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper
      porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh.
      Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris
      ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh.</p>
  </div>
</div>
&#13;
&#13;
&#13;

更新:在这里,我已经接近使用背景图片策略,但问题是我不希望我的图像超出其自然的100%宽度或高度,如果容器。当图像必须缩小时,background-size: contain;就是我想要的,当容器比图像大时,background-size: auto就是我想要的:

&#13;
&#13;
html,
body {
  width: 100%;
  height: 100%;
}
#container {
  height: 100%;
  width: 100%;
}
div.image-content {
  background: url(https://placeholdit.imgix.net/~text?txtsize=33&txt=300%C3%97150&w=300&h=150);
  
  /* desired behavior when div is LESS THAN image height */
  background-size: contain; 
  
  /* desired behavior when div is GREATER THAN image height */
  /*background-size: auto; */
  
  background-position: center;
  background-repeat: no-repeat;
  height: 100%;
  width: 100%;
}
div.copy-content {}
&#13;
<div id="container">

  <!--image-->
  <div class="image-content">
    <!-- <img src="http://i.imgur.com/cFypFxX.jpg" alt="Cat"> -->
  </div>

  <!--text-->
  <div class="copy-content">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper
      porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh.
      Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris
      ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt
      mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna
      luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia
      Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec.d</p>
  </div>
</div>
&#13;
&#13;
&#13;

另外,既然SO片段似乎不允许你调整输出窗口的大小,这里有一个jsFiddle:same snippet in jsFiddle

1 个答案:

答案 0 :(得分:1)

混合使用inline-block元素,绝对定位和间隔黑客,你应该能够用css实现这个目标

&#13;
&#13;
/* make container as big as it's content */

.container,
.copy-content,
.copy-content p {
  display: inline-block;
  position: relative;
}
.image {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  width: 100%;
}
.spacer {
  display: block;
  padding-top: 50%;  /* this is specific to the image - worked out by height / width and keeps the spacer in ratio with the image */
  width: 100%;
}
&#13;
<div class="container">
  <span class="spacer"></span>

  <!--image-->
  <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=300%C3%97150&w=300&h=150" alt="sports" class="image">

  <!--text-->
  <div class="copy-content">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper
      porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh.
      Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris
      ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh.</p>
  </div>
</div>
<br>
<br>


<div class="container">
  <span class="spacer"></span>

  <!--image-->
  <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=300%C3%97150&w=300&h=150" alt="sports" class="image">

  <!--text-->
  <div class="copy-content">
    <p>Lorem ipsum dolor.</p>
  </div>
</div>
<br>
<br>


<div class="container">
  <span class="spacer"></span>

  <!--image-->
  <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=300%C3%97150&w=300&h=150" alt="sports" class="image">

  <!--text-->
  <div class="copy-content">
    <p>Lorem ipsum dolor. Sed convallis tristique sem.</p>
  </div>
</div>
&#13;
&#13;
&#13;

如果您愿意,可以将图像作为背景图像放在间隔符上而不是绝对定位