html - jar 宽度:auto expand the div beyond screen width

标签 html css

我有一个动态创建的图表,其宽度可能会不断增加。它被放置在一个具有固定宽度和自动滚动的容器内。因此,如果图表比容器宽,图表就会滚动。

我的问题是图表的宽度不是固定的,它取决于内容。 我可以使用 width:auto 根据需要设置图表宽度吗?如果没有,有什么办法只用 CSS 来实现它。

编辑:即使容器必须滚动,我也希望 block 位于一行中。仅使用 CSS 就可以实现这一点吗?

.container{
  float:left;
  width:300px;
  background:#e3e3e3;
  height:200px;
  overflow:auto;
}
.sidebar{
  float:left;
  width:100px;
  background:#666;
  height:200px;
}
.chart{
  margin:50px 20px;
}
.block{width:100px;float:left;background:#ccc;margin:10px;}
<div class="container">
CONTAINER
  <div class="chart">
    <div class="block">100</div>
    <div class="block">200</div>
    <div class="block">300</div>
    <div class="block">400</div>
    <div class="block">500</div>
  </div>
</div>
<div class="sidebar">
SIDEBAR
</div>

block 必须水平对齐。使用图像更新以显示输出布局

最佳答案

如果将 .blockfloat: left 更改为 display: inline-block 并设置 white-space: nowrap 在您的 .chart 上,它们将水平排列。

.container{
  float:left;
  width:300px;
  background:#e3e3e3;
  height:200px;
  overflow:auto;
}
.sidebar{
  float:left;
  width:100px;
  background:#666;
  height:200px;
}
.chart{
  margin:50px 20px;
  white-space: nowrap;
}
.block{width:100px;background:#ccc;margin:10px;display: inline-block;}
<div class="container">
CONTAINER
  <div class="chart">
    <div class="block">100</div>
    <div class="block">200</div>
    <div class="block">300</div>
    <div class="block">400</div>
    <div class="block">500</div>
  </div>
</div>
<div class="sidebar">
SIDEBAR
</div>

关于html - jar 宽度:auto expand the div beyond screen width,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35939189/

相关文章:

javascript - 如何在输入类型 ="input"中添加微调按钮

javascript - CSS+Javascript 交换不同的背景图片

css - HTML5 和 Bootstrap - 两侧留有空格?

html - 当复选框移动到标签内时,与复选框关联的标签不起作用

javascript - 我想让一个词在鼠标悬停期间执行 CSS 动画后消失

javascript - 填充重叠的圆形区域

javascript - 移动设备上的垂直滚动 - 页面中的hammer.js 元素

javascript - jQuery UI 日期选择器 : don't highlight today when it is also selected

css - ng-table 可排序标题的选择器

html - 如何水平裁剪背景图像并在浏览器调整大小时保持高度?