填充100%宽度的滚动flex容器 [英] Fill 100% width of scrolling flex container

查看:60
本文介绍了填充100%宽度的滚动flex容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个水平滚动元素(带有 overflow-x:scroll ),其中包含包含flex项目的flex容器.我正在尝试将背景应用于flex容器.

I have a horizontally scrolling element (with overflow-x:scroll) with flex containers that contain flex items. I'm trying to apply background to the flex containers.

但是,如您在下面的示例中看到的(尝试向左/向右滚动),背景仅应用于视口的可见部分(橙色).有什么方法可以将其扩展为全宽,而不必为每个 .item 着色?

But as you can see in example below (try scrolling left/right) the background is applied only to visible part of viewport (orange). Is there some way to expand it to full width without having to color each .item?

.list-container {
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  background: tomato;
  margin-bottom: 10px;
}
.item {
  flex: 0 0 100px;
  height: 100px;
  margin-right: 10px;
  background-color: skyblue;
  opacity: 0.6;
}
.crop-container {
  width: 300px;
  overflow-x: scroll;
}

.item:first-child {
  margin-left: 10px;
}
.item:last-child {
  margin-right: 10px;
}

<div class='crop-container'>
  <div class='list-container'>
    <div class='item'></div>        
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
  </div>
  <div class='list-container'>
    <div class='item'></div>        
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
  </div>
</div>

推荐答案

以下是带有注释的更新代码:

Here is the updated code with comments:

.list-container {
  /* width:100% Removed to allow element to expand */
  display: inline-flex; /* inline to fit content width */
  /*flex-direction: row;
  flex-wrap: nowrap;  Not needed since it the default behavior */
  background: tomato;
  margin-bottom: 10px;
}
.item {
  width: 100px;  /* Width instead of flex-basis */
  flex-shrink:0; /* Avoid the shrinking*/
  height: 100px;
  margin-right: 10px;
  background-color: skyblue;
  opacity: 0.6;
}
.crop-container {
  width: 300px;
  overflow-x: scroll;
  display: flex;
  flex-direction: column;
  align-items:flex-start; /* Change default alignement to avoid the stretch effect*/
}

.item:first-child {
  margin-left: 10px;
}
/*.item:last-child {
  margin-right: 10px;
} Not needed since all the elements already have margin-right */

<div class='crop-container'>
  <div class='list-container'>
    <div class='item'></div>        
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
  </div>
  <div class='list-container'>
    <div class='item'></div>        
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
    <div class='item'></div>
  </div>
</div>

这篇关于填充100%宽度的滚动flex容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆