簡體   English   中英

當父級溢出時如何使子級div占用所有水平空間:滾動

[英]How to make a child div take up all the horizontal space when the parent has overflow: scroll

我希望有一個子 div,其中一行文本,背景顏色覆蓋整行,在父 div 內,最大寬度,並且能夠沿 x 軸滾動。

背景顏色必須在子元素上,因為子元素(有多個子元素)將具有不同的 colors。 所有子元素必須具有相同的寬度。

我只想在子元素上使用 CSS 並且沒有硬編碼數字。

我的問題是子元素沒有覆蓋整行,水平滾動就可以看到問題。

重現我的問題:

.parent {
  width: 400px;
  max-width: 400px;
  overflow: scroll;
}

.child {
  white-space: nowrap;
  background: skyblue;
}

<div class="parent">
  <div class="child">
     Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. 
     Aenean massa. 
  </div>
  <div class="child">second child</div>
  <div class="child">third child</div>
</div>

https://codepen.io/alawiii521/pen/rNObXrm

您可以通過將文本包裝在這樣的跨度中並將顏色添加到跨度來實現此目的。

 .parent { width: 400px; max-width: 400px; overflow: scroll; }.child { white-space: nowrap; display:table-row; background: skyblue; }
 <div class="parent"> <div class="child"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. </div> <div class="child">second child</div> <div class="child">third child</div> </div>

編輯:只需將 display:table 添加到子 class,它將起作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM