简体   繁体   English

让孩子采取父溢出宽度

[英]Make a child take parent overflow width

I have a number of child items inside an explicit width parent eg我在显式宽度父项中有许多子项,例如

 document.querySelector('.parent').scrollTo(100,0)
 .parent { overflow: auto; width: 200px; } .child { font-size: 40px; background-color: blue; white-space: nowrap; }
 <div class='parent'> <div class='child'>asdf asdf adsf asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> </div>

I want the child items background to cover the complete child.我希望child项背景覆盖完整的子项。 However it doesn't extend beyond the initial visible area.但是,它不会超出初始可见区域。 You can see what I mean in the image below where I've scrolled the parent horizontally:您可以在下图中水平滚动父级的位置看到我的意思:

在此处输入图片说明

try to add display: inline-block;尝试添加display: inline-block; on .child element在 .child 元素上

change this:改变这个:

  .parent {
      overflow: auto;
      width: 200px;
      background-color: blue;
    }

check: https://jsfiddle.net/sugandhnikhil/pLbfatgc/检查: https : //jsfiddle.net/sugandhnikhil/pLbfatgc/

I don't think that this is going to be possible with CSS alone.我认为仅使用 CSS 是不可能的。 A background will take up 100% of an element's width, which in this case is 200px, not the scroll width.背景将占据元素宽度的 100%,在本例中为 200px,而不是滚动宽度。

Here's a bit of a hack that will get the job done with javascript.这里有一些技巧可以使用 javascript 完成工作。 You can get the scrollWidth of the parent and then set that to be the width of each child.您可以获取父项的 scrollWidth,然后将其设置为每个子项的宽度。 The forces each child to have the width of the longest child.强制每个孩子都拥有最长孩子的宽度。

This isn't a responsive solution, however.然而,这不是一个响应式解决方案。 If you resize the text or parent for different screen sizes, you will need to recalculate the width of the children.如果针对不同的屏幕尺寸调整文本或父级的大小,则需要重新计算子级的宽度。

 document.querySelector('.parent').scrollTo(100,0); var scrollWidth = document.querySelector('.parent').scrollWidth; var children = document.querySelectorAll('.parent .child'); for (var i = 0; i < children.length; i++) { children[i].style.width = scrollWidth + "px"; }
 .parent { overflow: auto; width: 200px; } .child { font-size: 40px; background-color: blue; white-space: nowrap; }
 <div class='parent'> <div class='child'>asdf asdf adsf asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> </div>

You can hack it by using box-shadow您可以使用box-shadow破解它

 document.querySelector('.parent').scrollTo(100,0)
 .parent { overflow: auto; width: 200px; } .child { font-size: 40px; background-color: blue; box-shadow: 200px 0 0 blue; white-space: nowrap; }
 <div class='parent'> <div class='child'>asdf asdf adsf asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> <div class='child'>asdf</div> </div>

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

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