Flexbox图片高度调整大小

时间:2018-10-31 20:10:45

标签: css css3 flexbox

我无法完全调整图像大小。

理想情况下,我希望调整图像的大小,以便该选项适合屏幕,当前仅适合宽度,为什么呢?是否可以通过仅使用CSS来获得我希望的行为,也许需要进行媒体查询?烧焦谷歌试图找到类似的问题,但没有运气!

谢谢!

更新:

Image is not resized

为了澄清,我的问题是,如果窗口的高度足够小,图像将无法调整大小-标题消失,并且出现滚动条。我希望所有内容按比例缩小,因此不需要滚动条。

.container {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    background-color: aqua;
}

.content {
    display: flex;
    overflow-x: auto;
    flex: 2 1 auto;
    flex-wrap: nowrap;
}

.content::-webkit-scrollbar {
    display: none;
}

.option {
    display: flex;
    flex-direction: column;
    margin: auto 1em;
    width: 90%;
    height: 70%;
    text-align: justify;
    border: 3px solid black;
}

.img {
    flex: 1;
}

.title {
    flex: 0;
    font-weight: bold;
    text-align: center;
    flex: 1;
    margin: auto;
}

.desc {
    flex: 0;
}

.header {
    flex: 2;
    text-align: center;
    background-color: red;
}

.footer {
    flex: 1;
    background-color: brown;
}
<div class="container">
        <div class="header">
            Fusce pellentesque ante.
        </div>
        <div class="content">
            <div class="option">
                <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
                <p class="title">Proin hendrerit.</p>
                <p class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla non lacus tincidunt suscipit. Nam nec arcu a erat convallis.</p>
            </div>
            <div class="option">
                <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
                <p class="title">Nullam at.</p>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut congue eros. Aenean sit amet quam efficitur, lacinia ligula ac.</p>
            </div>
            <div class="option">
                <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
                <p class="title">Sed nec.</p>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam porttitor nisi mauris, ac interdum tellus pulvinar id. Morbi non molestie</p>
            </div>
            <div class="option">
                <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
                <p class="title">Morbi in.</p>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non rutrum arcu, ac posuere odio. Nunc in dolor eget nisi.</p>
            </div>
            <div class="option">
                <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
                <p class="title">Mauris ac.</p>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pulvinar orci non consectetur accumsan. Aenean posuere, felis commodo congue pulvinar.</p>
            </div>
        </div>
        <div class="footer">
            <p>Quisque venenatis volutpat dictum. Praesent.</p>
        </div>
    </div>

Fiddle

2 个答案:

答案 0 :(得分:2)

希望这就是您想要的。如果这是正确的答案!这就是我所做的。

  • 将高度和宽度的容器类设置为100%,并将显示设置为flexflex-direction: column,以便页眉,页脚和内容类垂直对齐。
  • 现在,如果您将内容类flex的属性设置为1,它将占用其余的宽度和高度。
  • 为选项类提供display: flex的属性并设置flex-direction: column,以使图像以及标题和描述垂直对齐。
  • 通过将30px的min-height分配给标题和选项中的p,可以将其余空间分配给图片,我希望这是您想要的。

* {
  margin: 0;
  padding: 0;
}

.content {
  min-height: 100%;
  width: 100%;
  min-width: 100%;
  display: flex;
  flex-direction: row;
  box-sizing: border-box;
  padding: 30px 0;
}

.content .option {
  flex: 1;
  max-height: 100vh;
  min-width: 100%;
  display: flex;
  flex-direction: column;
}

.content .option img {
  height: calc(100% - 160px);
  width: 100%;
}
.content .option p {
  height: 20px;
}

.content .option p:nth-child(2) {
  height: 40px;
}

.header,
.footer {
  position: fixed;
  left: 0;
  right: 0;
  background-color: #ccc;
  height: 30px;
}

.header {
  top: 0;
}

.footer {
  bottom: 0;
}
<div class="container">
  <div class="header">
    Fusce pellentesque ante.
  </div>
  <div class="content">
    <div class="option">
      <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
      <p class="title">Proin hendrerit.</p>
      <p class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla non lacus tincidunt suscipit. Nam nec arcu a erat convallis.</p>
    </div>
    <div class="option">
      <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
      <p class="title">Nullam at.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut congue eros. Aenean sit amet quam efficitur, lacinia ligula ac.</p>
    </div>
    <div class="option">
      <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
      <p class="title">Sed nec.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam porttitor nisi mauris, ac interdum tellus pulvinar id. Morbi non molestie</p>
    </div>
    <div class="option">
      <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
      <p class="title">Morbi in.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non rutrum arcu, ac posuere odio. Nunc in dolor eget nisi.</p>
    </div>
    <div class="option">
      <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
      <p class="title">Mauris ac.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pulvinar orci non consectetur accumsan. Aenean posuere, felis commodo congue pulvinar.</p>
    </div>
  </div>
  <div class="footer">
    <p>Quisque venenatis volutpat dictum. Praesent.</p>
  </div>
</div>

答案 1 :(得分:-1)

html,
body {
  padding: 0;
  margin: 0;
}

.container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: aqua;
  justify-content: space-between;
}

.header,
.footer {
  flex: 0 1 auto;
  text-align: center;
}

.content {
  display: flex;
  overflow-x: auto;
  flex-wrap: nowrap;
  flex: 1 1 auto;
}

.footer {
  background-color: brown;
}

img {
  max-width: 600px;
}
<div class="container">
  <div class="header">
    Fusce pellentesque ante.
  </div>
  <div class="content">
    <div class="option">
      <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
      <p class="title">Proin hendrerit.</p>
      <p class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur fringilla non lacus tincidunt suscipit. Nam nec arcu a erat convallis.</p>
    </div>
    <div class="option">
      <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
      <p class="title">Nullam at.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut congue eros. Aenean sit amet quam efficitur, lacinia ligula ac.</p>
    </div>
    <div class="option">
      <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
      <p class="title">Sed nec.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam porttitor nisi mauris, ac interdum tellus pulvinar id. Morbi non molestie</p>
    </div>
    <div class="option">
      <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
      <p class="title">Morbi in.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam non rutrum arcu, ac posuere odio. Nunc in dolor eget nisi.</p>
    </div>
    <div class="option">
      <img class="img" src="https://www.w3schools.com/w3css/img_lights.jpg">
      <p class="title">Mauris ac.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque pulvinar orci non consectetur accumsan. Aenean posuere, felis commodo congue pulvinar.</p>
    </div>
  </div>
  <div class="footer">
    <p>Quisque venenatis volutpat dictum. Praesent.</p>
  </div>
</div>