简体   繁体   English

可滚动水平容器的内部 div 具有其中元素的宽度

[英]Inner div of scrollable horizontal container to have the width of the elements inside it

Sorry if I couldn't be clear in the title, but checking out the following jsFiddle will clear out what I want to do:对不起,如果我在标题中不清楚,但查看以下 jsFiddle 将清除我想要做什么:

JSFiddle JSFiddle

I have a horizontally scrollable container that contains a div, and in that div i have several inline-block container.我有一个包含 div 的水平可滚动容器,在那个 div 中我有几个内联块容器。 I want the inner container's border to span the whole width of the scrollable area.我希望内部容器的边框跨越可滚动区域的整个宽度。 Any help is appreciated.任何帮助表示赞赏。

<div class="container">
  <div class="innerContainer">
    <div>item 1</div>
    <div>item 2</div>
    <div>item 3</div>
    <div>item 4</div>
    <div>item 5</div>
    <div>item 6</div>
    <div>item 7</div>
    <div>item 8</div>
  </div>
</div>
.container {
  overflow-x: auto;
  white-space: nowrap;
  border: solid 1px green;
  padding: 20px;
}

.innerContainer {
  border-top: solid 1px red;
}

.innerContainer div {
  display: inline-block;
  width: 300px;
  height: 100px;
  margin-right: 20px;
  margin-top: 10px;
  border: solid 1px gray;
}

just move overflow-x: auto;只需移动overflow-x: auto;

from .container来自.container

to .innerContainer.innerContainer

or或者

use width:max-content in .innerContainer.innerContainer使用width:max-content

 .container { white-space: nowrap; border: solid 1px green; padding: 20px; } .innerContainer { border-top: solid 1px red; overflow-x: auto; } .innerContainer div { display: inline-block; width: 300px; height: 100px; margin-right: 20px; margin-top: 10px; border: solid 1px gray; }
 <h3> I want the red border to expand all the width of the scrollable container. </h3> <div class="container"> <div class="innerContainer"> <div>item 1</div> <div>item 2</div> <div>item 3</div> <div>item 4</div> <div>item 5</div> <div>item 6</div> <div>item 7</div> <div>item 8</div> </div> </div>

.innerContainer {
  width: max-content;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM