html - 如果 DIV 内容太大,则保持 DIV 并排并使页面可水平滚动

标签 html css css-float overflow

我希望并排放置两个 div,无论右侧 div 中内容的宽度如何。左边的 div 总是有很短的线条。右边的 div 可能有很长的行。如果行很长,我只希望整个页面都可以水平滚动。

这是基本布局:

(div)
+--------------------------------------------------
| (div>pre)  (div>pre)
| +-------+  +-------------------------------------
| |line 1 |  |
| |line 2 |  |  Make the page horizontally scroll
| |line 3 |  |  if needed based on the width of
| |line 4 |  |  content in this div. Keep these
| |line 5 |  |  divs side by side.
| |line 6 |  |
| +-------+  +-------------------------------------
+--------------------------------------------------

这是我的代码片段:

body, pre {
  margin: 0;
  padding: 0;
}

#container {
  border-style: solid;
  border-color: blue;
  white-space: nowrap;
}

#container::after {
  content: "";
  clear: both;
  display: table;
}

#sidebar {
  width: 100px;
  float: left;
  border-style: dashed;
  border-color: green;
}

#main {
  float: left;
  border-style: solid;
  border-color: red;
}
<div id="container">
  <div id="sidebar">
    <pre>line 1    
line 2    
line 3    
line 4    
line 5    
line 6</pre>
  </div>
  <div id="main">
    <pre>Lorem ipsum dolor sit amet, consectetur adipiscing elit,
    foo
ea commodo consequat
Duis aute irure dolor
in reprehenderit in
voluptate velit</pre>
  </div>
</div>

在上面的代码片段中,一切看起来都很棒。但是,如果 #main div 中的某一行变得很长,则 div 不会保持并排。示例:http://jsfiddle.net/ouqamf5y/1/

无论右侧 div 中的线条宽度如何,我怎样才能将这些 div 并排放置?


这不是 How to get these two divs side-by-side? 的副本因为这个问题没有解决宽 div > pre 行的溢出问题。

最佳答案

您还没有为第二个 block /右边的 block 定义任何宽度。

尝试使用这个:

sidebar { width: 30%; float: left; }
main { width:68%; float: left; }

一旦为两个 block 定义了宽度,主 div 将保持并排。检查一下,让我知道它是否有效。

关于html - 如果 DIV 内容太大,则保持 DIV 并排并使页面可水平滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52084488/

相关文章:

html - 使用CSS将跨度定位在图像下方

html - CSS对齐图像文本

html - 在响应式设计中, float div 不会向左对齐

html - CSS使事件导航栏箭头

html - body 顶部的宽度 100% 需要滚动

javascript - 如何右对齐/对齐 Bootstrap 下拉菜单项?

javascript - 具有绝对位置的 HTML div 尝试换行文本。我可以避免它吗,不使用空格

css - 在一系列每个都有负边距的元素中,如何使每个元素出现在前一个元素的下方?

html - 需要帮助调整站点图像

javascript - 仅在向上滚动时固定 CSS 中的布局位置(对齐到顶部)