css Image缩小以适应div,但宽度会随着column变窄而缩小

dwbf0jvd  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(165)

我很确定我只是错过了一些基本的东西,但为了我的生活,我得到了它。背景资料:我使用bootstrap 3(请不要判断:)),html和css。我有一个具有报纸样式列的div(文本从一列的底部流到下一列的底部)。我想在图像周围环绕文本,这样图像就缩小到div的高度内,但是如果列比图像窄,图像的宽度仍然会缩小。
我已经得到了第二部分,但似乎无法将图像保留在div中。
HTML

<h1>
  Test
</h1>
<div class="container-fluid">
  <div class="row">
    <div class="col-xs-12">
      <div class="newspaper">
        <p><img src="https://streissguthgardens.com/assets/Streissguth-In_Love_with_a_Hillside_Garden-e1b787f274c8c2cc3beaf52cdcba1dbee982c503ab098d558a4053702ba8d8e1.jpg" class="book img-responsive img-thumbnail pull-left" alt="Place holder text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Nisl tincidunt eget nullam non nisi est sit amet facilisis. Faucibus vitae aliquet nec ullamcorper sit amet risus. Urna id volutpat lacus laoreet non curabitur gravida arcu. Neque gravida in fermentum et sollicitudin. Ipsum nunc aliquet bibendum enim facilisis gravida.</p>
        <p>Congue nisi vitae suscipit tellus mauris a diam. Mus mauris vitae ultricies leo integer malesuada nunc vel. Nunc vel risus commodo viverra maecenas. Sed arcu non odio euismod lacinia at quis risus sed. Eu sem integer vitae justo eget. Lectus quam id leo in vitae. Malesuada proin libero nunc consequat interdum varius sit amet mattis. Orci phasellus egestas tellus rutrum tellus pellentesque eu tincidunt. Lectus vestibulum mattis ullamcorper velit sed ullamcorper. Id leo in vitae turpis massa sed elementum tempus. In eu mi bibendum neque egestas congue quisque egestas. Nisl pretium fusce id velit ut tortor pretium. Convallis tellus id interdum velit laoreet id donec. Sed vulputate odio ut enim. Ipsum a arcu cursus vitae congue mauris. Lectus nulla at volutpat diam ut venenatis. Morbi tristique senectus et netus et malesuada fames ac. Vel turpis nunc eget lorem dolor sed viverra ipsum nunc. Est velit egestas dui id.
        </p>
      </div>
    </div>
  </div>
</div>

CSS

.book {  height: 100%
  max-height: 100%;
  object-fit: scale-down;
  width: auto;
}
.newspaper {
  background: lightgray;
  column-count: 3;
  column-gap: 20px;
}

这是我的fiddle
下面是全屏的屏幕截图(不工作,图像不应该延伸到文本底部)

下面是围绕图像流动的文本(工作)

这里的图像缩小为列是新的窄(工作)

hpcdzsge

hpcdzsge1#

我发现导致图像溢出其容器的原因是pull-left类(声明了float: left!important)。如果删除此类,图像不会溢出,但显然,您希望文本在图像周围浮动。
但是,我能够简单地通过将图像放在p之前来解决这个问题。我不明白有什么变化,但决定继续下去,并张贴这个答案,而不是花更多的时间试图找出它。
前:<div class="newspaper"><p><img>Lorem ipsum</p></div>
后:<div class="newspaper"><img><p>Lorem ipsum</p></div>

.book {
  height: 100%;
  max-height: 100%;
  object-fit: scale-down;
  width: auto;
}
.newspaper {
  background: lightgray;
  column-count: 3;
  column-gap: 20px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<h1>
  Test
</h1>
<div class="container-fluid">
  <div class="row">
    <div class="col-xs-12">
      <div class="newspaper">
        <img src="https://streissguthgardens.com/assets/Streissguth-In_Love_with_a_Hillside_Garden-e1b787f274c8c2cc3beaf52cdcba1dbee982c503ab098d558a4053702ba8d8e1.jpg" class="book img-responsive img-thumbnail pull-left" alt="Place holder text">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Nisl tincidunt eget nullam non nisi est sit amet facilisis. Faucibus vitae aliquet nec ullamcorper sit amet risus. Urna id volutpat lacus laoreet non curabitur gravida arcu. Neque gravida in fermentum et sollicitudin. Ipsum nunc aliquet bibendum enim facilisis gravida.</p>
        <p>Congue nisi vitae suscipit tellus mauris a diam. Mus mauris vitae ultricies leo integer malesuada nunc vel. Nunc vel risus commodo viverra maecenas. Sed arcu non odio euismod lacinia at quis risus sed. Eu sem integer vitae justo eget. Lectus quam id leo in vitae. Malesuada proin libero nunc consequat interdum varius sit amet mattis. Orci phasellus egestas tellus rutrum tellus pellentesque eu tincidunt. Lectus vestibulum mattis ullamcorper velit sed ullamcorper. Id leo in vitae turpis massa sed elementum tempus. In eu mi bibendum neque egestas congue quisque egestas. Nisl pretium fusce id velit ut tortor pretium. Convallis tellus id interdum velit laoreet id donec. Sed vulputate odio ut enim. Ipsum a arcu cursus vitae congue mauris. Lectus nulla at volutpat diam ut venenatis. Morbi tristique senectus et netus et malesuada fames ac. Vel turpis nunc eget lorem dolor sed viverra ipsum nunc. Est velit egestas dui id.
        </p>
      </div>
    </div>
  </div>
</div>
fbcarpbf

fbcarpbf2#

我已经通过改变.book类的样式得到了它,如果你想避免图像质量的损失,我建议设置一个“最大高度”的像素。

.book {
    width: 100%;
    background: transparent;
    border: none;
    object-fit: contain;
    max-height: inherit; /* replace "inherit" with something like "250px" to avoid poor image quality on larger screens. */
}
.newspaper {
  background: lightgray;
  column-count: 3;
  column-gap: 20px;
}
<!-- 
  Bootstrap docs: https://getbootstrap.com/docs
-->
    <h1>
      Test
    </h1>
    <div class="container-fluid">
      <div class="row">
        <div class="col-xs-12">
          <div class="newspaper">
            <p><img src="https://streissguthgardens.com/assets/Streissguth-In_Love_with_a_Hillside_Garden-e1b787f274c8c2cc3beaf52cdcba1dbee982c503ab098d558a4053702ba8d8e1.jpg" class="book img-responsive img-thumbnail pull-left" alt="Place holder text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Nisl tincidunt eget nullam non nisi est sit amet facilisis. Faucibus vitae aliquet nec ullamcorper sit amet risus. Urna id volutpat lacus laoreet non curabitur gravida arcu. Neque gravida in fermentum et sollicitudin. Ipsum nunc aliquet bibendum enim facilisis gravida.</p>
            <p>Congue nisi vitae suscipit tellus mauris a diam. Mus mauris vitae ultricies leo integer malesuada nunc vel. Nunc vel risus commodo viverra maecenas. Sed arcu non odio euismod lacinia at quis risus sed. Eu sem integer vitae justo eget. Lectus quam id leo in vitae. Malesuada proin libero nunc consequat interdum varius sit amet mattis. Orci phasellus egestas tellus rutrum tellus pellentesque eu tincidunt. Lectus vestibulum mattis ullamcorper velit sed ullamcorper. Id leo in vitae turpis massa sed elementum tempus. In eu mi bibendum neque egestas congue quisque egestas. Nisl pretium fusce id velit ut tortor pretium. Convallis tellus id interdum velit laoreet id donec. Sed vulputate odio ut enim. Ipsum a arcu cursus vitae congue mauris. Lectus nulla at volutpat diam ut venenatis. Morbi tristique senectus et netus et malesuada fames ac. Vel turpis nunc eget lorem dolor sed viverra ipsum nunc. Est velit egestas dui id.
            </p>
          </div>
        </div>
      </div>
    </div>

相关问题