简体   繁体   English

使子div扩展到溢出内容的宽度

[英]Make child div expand to width of overflown content

I'm messing around with a code searching ui that lists matched lines by file along with the surrounding code. 我正在乱用一个代码搜索ui,它列出了匹配的文件以及周围的代码。 The matched lines and their surroundings are listed inside a container div with overflow: auto so that the code can be scrolled. 匹配的行及其周围环境列在容器div中, overflow: auto以便可以滚动代码。

Here is how the html and css are laid out: 以下是html和css的布局:

 .container { width: 200px; overflow: auto; border: 2px solid #CCC; } .match:first-child { border-top: none; } .match { border-top: 1px solid red; } span { white-space: pre; } 
 <div class="container"> <div class="match"> <div class="line"> <span>This content is so long that it ends up going beyond the edge of the container. Good thing we are using overflow: auto so we can scroll!</span> </div> <div class="line"> <span>Another line that is too long to fit into the container.</span> </div> <div class="line"> <span>There can be many lines in each match, but the border should only be between the matches, not the lines.</span> </div> </div> <div class="match"> <div class="line"> <span>The second line. Does it matter how long this line is? Will the line border extend now that this line is overflowing?</span> </div> </div> </div> 

The problem is that when the content within the .line divs extends beyond the .container , the borders on the .match elements only extend to the width of the .container . 问题是当.line divs中的内容超出.container.match元素上的.match只会扩展到.container的宽度。

Is there any way to make the .match elements extend to the entire width of the container so that the border extends the entire width of the scrollable area? 有没有办法让.match元素扩展到容器的整个宽度,以便边框扩展可滚动区域的整个宽度?

The secret that you are missing is that both .match and .line need to automatically expand to 100% width of the child . 你缺少的秘密是.match.line需要自动扩展到孩子的 100%宽度。

The easiest way to solve this is with display: inline : 解决这个问题的最简单方法是使用display: inline

.match, .line {
  display: inline;
}

I've created a new fiddle showcasing this here . 我创建了一个新的小提琴展示这个在这里

Hope this helps! 希望这可以帮助! :) :)

You can set display: table-row on .match , and set the border on .line 您可以在.match上设置display: table-row ,并在.line上设置边框

.match {
  display: table-row;
}
.match ~ .match .line:first-child {
  border-top: 1px solid red;
}

jsFiddle 的jsfiddle

or 要么

.match {
  display: table-row;
}
.match:not(:first-child) .line:first-child {
  border-top: 1px solid red;
}

jsFiddle 的jsfiddle

I don't have your entire code so I can't exactly test what you're trying to do properly. 我没有你的全部代码所以我无法准确测试你正在尝试做什么。 However, it appears you're missing "width: 100%" on the .match and .line. 但是,您似乎在.match和.line上缺少“width:100%”。 Note that 100% is 100% of the parent, not of the viewport. 请注意,100%是父项的100%,而不是视口的100%。

.match, .line {
    width: 100%;
}

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

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