使子div扩展为溢出内容的宽度 [英] Make child div expand to width of overflown content

查看:109
本文介绍了使子div扩展为溢出内容的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个通过文件和周围代码一起列出匹配行的代码。匹配的行及其环境被列在容器div中, overflow:auto ,以便代码可以滚动。



以下是html和css的布局:

  .container {width:200px;溢出:自动; border:2px solid #CCC;} match:first-child {border-top:none;}。match {border-top:1px solid red;} span {white-space:pre;}  < div class =container> 

.line div中的内容超出 .container 时, .match 元素只能扩展到 .container 的宽度。



是有什么方法可以使 .match 元素扩展到容器的整个宽度,以便边界扩展可滚动区域的整个宽度?

$ b $您可以在 .match 上设置 display:table-row code>,并将边框设置为 .line

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

jsFiddle





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

jsFiddle


I'm messing around with a code searching ui that lists matched lines by file along with the surrounding code. The matched lines and their surroundings are listed inside a container div with overflow: auto so that the code can be scrolled.

Here is how the html and css are laid out:

.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.

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?

解决方案

You can set display: table-row on .match, and set the border on .line

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

jsFiddle

or

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

jsFiddle

这篇关于使子div扩展为溢出内容的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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