横向滚动的 flexbox:子图高度超过父级,而不是填充高度

huangapple go评论63阅读模式
英文:

Horizontal scroll flexbox: Child image exceeds height of parent instead of filling up height

问题

我正在尝试使用flexbox创建一个水平滚动的图像库,但我无法使图像的高度增长以填满容器的高度,同时保持固定的纵横比。

卡片的数量可以是可变的,因此应该有一些灵活的代码,以填充空间,如果有足够的图块,可以进行水平滚动。

横向滚动的 flexbox:子图高度超过父级,而不是填充高度

以下是问题的简化版本(查看完整页面以获得更好的结果):

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

ul.hor-bar{
 background:pink;
 border:1rem solid crimson;
 padding:2rem;
 box-sizing:border-box;
 
 display: flex;
 gap: 2rem;
 height:100vh;
 max-height: 100%;
 overflow-x: scroll;
}

li.card{
  height: 100%;
  max-height: 100%;
  display:flex;
  flex-direction: column;
}

p{
  flex:auto;

  /*额外样式*/
  background: magenta;
  padding: 0.75rem 0;
  font-weight:bold;
  margin: 0;
}

.img-container{
  height:100%;
  flex:1 ;
  width:auto;
  max-height: 100%; /* <=required */
}

img{
 height:100%;
 width:auto;  /* <=required => or 100% with flex-shrink: 0 */
 object-fit:cover;
}
<ul class="hor-bar">
  <li class="card">
    <div class="img-container">
      <img src="https://i.imgur.com/bCBt6ga.jpeg" alt="pepe">
    </div>
    <p>Card text</p>
  </li>
   <li class="card">
    <div class="img-container">
      <img src="https://i.imgur.com/bCBt6ga.jpeg" alt="pepe">
    </div>
    <p>Card text</p>
  </li>
  <!-- 添加更多卡片 -->
</ul>

我知道在上面的示例中,去除100vh的高度可以解决问题。但我正在寻找一个使用动态高度的解决方案。以下是另一个示例,它不使用100vh:

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

.fixed{
  position: fixed;
  padding-top:4rem;
  box-sizing:border-box;
  height: 100%;
  width:100%;
  background: green;
}

ul.hor-bar{
 background:pink;
 border:1rem solid crimson;
 padding:2rem;
 box-sizing:border-box;
 
 display: flex;
 gap: 2rem;
 height:100%;
 
 overflow-x: scroll;
}

li.card{
  height: 100%;
  max-height: 100%;
  display:flex;
  flex-direction: column;
}

p{
  flex:auto;

  /*额外样式*/
  background: magenta;
  padding: 4vh 0;
  font-weight:bold;
  margin: 0;
}

.img-container{
  height:100%;
  flex:1 ;
  width:auto;
  max-height: 100%; /* <=required */
}

img{
 height:100%;
 width:auto;  /* <=required => or 100% with flex-shrink: 0 */
 object-fit:cover;
}
<div class="fixed">
<ul class="hor-bar">
  <li class="card">
    <div class="img-container">
      <img src="https://i.imgur.com/bCBt6ga.jpeg" alt="pepe">
    </div>
    <p>Card text</p>
  </li>
   <li class="card">
    <div class="img-container">
      <img src="https://i.imgur.com/bCBt6ga.jpeg" alt="pepe">
    </div>
    <p>Card text</p>
  </li>
  <!-- 添加更多卡片 -->
</ul>
</div>
英文:

I am trying to make a horizontal scrollable image gallery with flexbox, but I am unable to make the image height grow to fill up the container height while also keeping a fixed aspect ratio.

The amount of cards can be variable, so it should be some flexible code that fills up the space and has a horizontal scroll if there are enough tiles.

横向滚动的 flexbox:子图高度超过父级,而不是填充高度

Here is a simplified version of the issue (check out the full page for better result):

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-css -->

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

ul.hor-bar{
 background:pink;
 border:1rem solid crimson;
 padding:2rem;
 box-sizing:border-box;
 
 display: flex;
 gap: 2rem;
 height:100vh;
 max-height: 100%;
 overflow-x: scroll;
}

li.card{
  height: 100%;
  max-height: 100%;
  //flex-shrink: 0; 
  display:flex;
  flex-direction: column;

}

p{
  flex:auto;

  /*extra style*/
  background: magenta;
  padding: 0.75rem 0;
  font-weight:bold;
  margin: 0;
}

.img-container{
  height:100%;
  flex:1 ;
  width:auto;
  max-height: 100%; /* &lt;=required */
}

img{
 height:100%;
 width:auto;  /* &lt;=required =&gt; or 100% with flex-shrink: 0 */
 object-fit:cover;
}

<!-- language: lang-html -->

&lt;ul class=&quot;hor-bar&quot;&gt;
  &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
 &lt;/ul&gt;

<!-- end snippet -->

I know in this previous example removing the height of 100vh fixes the issue. But I am searching for a solution that uses a dynamic height. Here is another example which does not use the 100vh.

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-css -->

body,ul{
margin:0; padding:0;

}

.fixed{
  position: fixed;
  padding-top:4rem;
  box-sizing:border-box;
  height: 100%;
  width:100%;
  background: green;
}

ul.hor-bar{
 background:pink;
 border:1rem solid crimson;
 padding:2rem;
 box-sizing:border-box;
 
 display: flex;
 gap: 2rem;
 height:100%;
 
 overflow-x: scroll;
}

li.card{
  height: 100%;
  max-height: 100%;
  //flex-shrink: 0; 
  display:flex;
  flex-direction: column;

}

p{
  flex:auto;

  /*extra style*/
  background: magenta;
  padding: 4vh 0;
  font-weight:bold;
  margin: 0;
}

.img-container{
  height:100%;
  flex:1 ;
  width:auto;
  max-height: 100%; /* &lt;=required */
}

img{
 height:100%;
 width:auto;  /* &lt;=required =&gt; or 100% with flex-shrink: 0 */
 object-fit:cover;
}

<!-- language: lang-html -->

&lt;div class=&quot;fixed&quot;&gt;
&lt;ul class=&quot;hor-bar&quot;&gt;
  &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://i.imgur.com/bCBt6ga.jpeg&quot; alt=&quot;pepe&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
 &lt;/ul&gt;
 
 &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

有些东西需要定义滚动区域的高度。每个子元素的高度都需要被定义为除了自动之外的某个值。然后,你可以将 <img> 的高度设置为其容器的 100%。如果你不另行指定元素的宽度,它将是自动的,因此保留其自然的宽高比。

以下是带有不同大小和宽高比的图像的演示。

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

.hor-bar {
  background: pink;
  border: 1rem solid crimson;
  margin: 0;
  padding: 0;
  display: flex;
  gap: 2rem;
  height: 100vh;
  overflow-x: scroll;
  overflow-y: hidden;
  list-style-type: none;
}

.card {
  height: 100%;
  display: grid;
  grid-template-rows: 85% 15%;
}

.img-container {
  height: 100%;
}

img {
  height: 100%;
}

p {
  background: magenta;
  font-weight: bold;
  margin: 0;
  padding: .5rem;
}
<ul class="hor-bar">
  <li class="card">
    <div class="img-container">
      <img src="https://picsum.photos/id/237/600/900" alt="dog">
    </div>
    <p>Card text</p>
  </li>
  <!-- ... (其他卡片的代码) ... -->
</ul>
英文:

Something needs to define the height of the scroll area. Every child needs to have its height be defined as something other than auto. Then you can set the &lt;img&gt; height to 100% of its container. If you don't otherwise specify the width of the element, it will be auto, and therefore retain its natural aspect ratio.

Demo below with images of different sizes and aspect ratios.

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-css -->

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

.hor-bar {
  background: pink;
  border: 1rem solid crimson;
  margin: 0;
  padding: 0;
  display: flex;
  gap: 2rem;
  height: 100vh;
  overflow-x: scroll;
  overflow-y: hidden;
  list-style-type: none;
}

.card {
  height: 100%;
  display: grid;
  grid-template-rows: 85% 15%;
}

.img-container {
  height: 100%;
}

img {
  height: 100%;
}

p {
  background: magenta;
  font-weight:bold;
  margin: 0;
  padding: .5rem;
}

<!-- language: lang-html -->

&lt;ul class=&quot;hor-bar&quot;&gt;
  &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://picsum.photos/id/237/600/900&quot; alt=&quot;dog&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://picsum.photos/id/0/600&quot; alt=&quot;laptop&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://picsum.photos/id/63/700/600&quot; alt=&quot;coffee&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://picsum.photos/id/70/900&quot; alt=&quot;trees&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://picsum.photos/id/112/800/500&quot; alt=&quot;wheat&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://picsum.photos/id/168/200/300&quot; alt=&quot;rocks&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://picsum.photos/id/210/800/300&quot; alt=&quot;bricks&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
    &lt;/li&gt;
   &lt;li class=&quot;card&quot;&gt;
    &lt;div class=&quot;img-container&quot;&gt;
      &lt;img src=&quot;https://picsum.photos/id/216/500/800&quot; alt=&quot;path&quot;&gt;
    &lt;/div&gt;
    &lt;p&gt;Card text&lt;/p&gt;
  &lt;/li&gt;
 &lt;/ul&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 00:06:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定