html - 由于图像大小,两个 flex 的子 div 不会均分空间

标签 html css

我有一个显示设置为 flex 的父容器。里面有两个 div 都设置了 flex:1 但我的第一个子 div 里面有一个图像,占用的空间是第二个 div 的两倍。

如果屏幕的高度是 1000 像素,那么每个屏幕不应该覆盖 500 个高度吗?

当我的 flex-direction 是 'row' 时,这很好用,但是对于 'column',你有一个无限的垂直空间,所以 div 可以根据需要占据尽可能多的高度。

我希望两个子 div 都覆盖可见的剩余屏幕高度,而不是垂直扩展其父容器的高度。

#info-container {
  margin: 0 auto;
  margin-top: 5%;
  text-shadow: 0 2px 2px #000;
  display: flex;
  flex-direction: column;
}

#info-container .cover {
  text-align: center;
  flex: 1;
}

#info-container .cover img {
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 2%;
  box-shadow: 0px 0px 5px #000;
  max-width: 100%;
}

#info-container .song-info {
  align-self: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 18px;
  flex: 1;
}

#info-container .song-info h4 {
  margin-top: 10px;
  font-weight: 100;
  // text-decoration: underline;
  padding-bottom: 5px;
  border-bottom: 1px solid white;
  width: 100%;
}

// suggested songs
.suggested-songs {
  margin-top: 5%;
}
<div id="info-container">
  <div class="cover"><img class='cover-photo' src='https://images.unsplash.com/photo-1523895665936-7bfe172b757d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' alt="cover">
  </div>
  <div class="song-info">
    <h4>Test : <span class="song-name"> Test</span>
    </h4>
    <h4>Test : <span class="artist">Test</span></h4>
    <h4>Duration : <span class="duration">3:26</span></h4>
  </div>
</div>

代码笔:https://codepen.io/AfaqQazi/pen/QWLaybo?editors=1100

最佳答案

flex 属性只设置 FlexBox 元素的最小高度。由于您的图像标签比文本内容高,因此它使第一个 div 更高。如果你想平衡 div,你有两个选择。

首先:为 img 标签设置最大高度:

#info-container {
  margin: 0 auto;
  /*margin-top: 5%;*/
  text-shadow: 0 2px 2px #000;
  display: flex;
  flex-direction: column;
}

#info-container .cover {
  text-align: center;
  flex: 1;
}

#info-container .cover img {
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 2%;
  box-shadow: 0px 0px 5px #000;
  max-width: 100%;
  max-height: 200px;
}

#info-container .song-info {
  align-self: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 18px;
  flex: 1;
}

#info-container .song-info h4 {
  margin-top: 10px;
  font-weight: 100;
  // text-decoration: underline;
  padding-bottom: 5px;
  border-bottom: 1px solid white;
  width: 100%;
}

// suggested songs
.suggested-songs {
  margin-top: 5%;
}
<div id="info-container">
  <div class="cover">
    <img class='cover-photo' src='https://images.unsplash.com/photo-1523895665936-7bfe172b757d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' alt="cover">
  </div>
  <div class="song-info">
    <h4>Test : <span class="song-name"> Test</span></h4>
    <h4>Test : <span class="artist">Test</span></h4>
    <h4>Duration : <span class="duration">3:26</span></h4>
  </div>
</div>

第二:将图片设为背景图片,为其设置最小高度,并将其所有父元素设置为100%高度:

html, body, #info-container{
   height: 100%;
}

#info-container {
  margin: 0 auto;
  /*margin-top: 5%;*/
  text-shadow: 0 2px 2px #000;
  display: flex;
  flex-direction: column;
}

#info-container .cover {
  text-align: center;
  flex: 1;
  background: url(https://images.unsplash.com/photo-1523895665936-7bfe172b757d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80) center/cover;
  min-height: 300px;
}

/*#info-container .cover img {
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 2%;
  box-shadow: 0px 0px 5px #000;
  max-width: 100%;
}*/

#info-container .song-info {
  align-self: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 18px;
  flex: 1;
}

#info-container .song-info h4 {
  margin-top: 10px;
  font-weight: 100;
  // text-decoration: underline;
  padding-bottom: 5px;
  border-bottom: 1px solid white;
  width: 100%;
}

// suggested songs
.suggested-songs {
  margin-top: 5%;
}
<div id="info-container">
  <div class="cover">
    <!--<img class='cover-photo' src='https://images.unsplash.com/photo-1523895665936-7bfe172b757d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80' alt="cover">-->
  </div>
  <div class="song-info">
    <h4>Test : <span class="song-name"> Test</span></h4>
    <h4>Test : <span class="artist">Test</span></h4>
    <h4>Duration : <span class="duration">3:26</span></h4>
  </div>
</div>

关于html - 由于图像大小,两个 flex 的子 div 不会均分空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57808138/

相关文章:

javascript - 从左到右滑动多个div

javascript - WebSocket 仅在本地文件 :///page 上执行时有效

javascript - 如何使用 .textContent 更新元素

CSS box-shadow VS 溢出隐藏

jquery - 将 CheckBoxList 标签的宽度设置为全宽(以允许选择行)

css - 在 CSS 上将制表符转换为空格

html - 防止在 iPhone 上垂直滚动

jquery - 如何卡住 Pane /修复网站标题和水平导航栏?

javascript - JQuery - 选择选项不会附加在更改事件上

javascript - 引用 Canvas 时出现空指针