html - 两个 DIV 在较小的 DIV 中并排

标签 html css

我试图在一个较小的 DIV 中并排放置两个 DIV,并带有 overflow:hidden; 我希望两个 DIV 并排放置,并且由于其父 DIV 的宽度较小而隐藏了其中的一些内容。相反,最后一个 DIV 移动到另一个 DIV 下面,即使它们都具有 float:left;。

HTML:

<div id="wrapper"> <!-- wrapper -->
    <div id="nav"> <!-- nav -->

    </div> <!-- /nav -->
    <div id="container"> <!-- container -->

    </div> <!-- /container -->
</div> <!-- /wrapper -->

CSS:

#wrapper {
    float:left;
    height:960px;
    width:540px;
    background-color:#eeeeee;   
    overflow:hidden;
}

#nav {
    float:left;
    width:460px;
    height:960px;
    background-color:red;
}

#container {
    float:left;
    width:540px;
    height:960px;
    background-color:green;
}

这是我想要的效果:

Mobile UI example

最佳答案

如果您想要并排放置这两个元素,我认为您必须添加某种额外的“包装器”元素来包围您的 #nav#container元素。 (溢出设置为自动,因此您可以看到元素彼此相邻。)

示例:

#wrapper {
  float: left;
  height: 960px;
  width: 540px;
  background-color: #eeeeee;
  overflow: auto;
}
#wrap {
  width: 1000px;
}
#nav {
  float: left;
  width: 460px;
  height: 960px;
  background-color: red;
  clear:
}
#container {
  float: left;
  width: 540px;
  height: 960px;
  background-color: green;
}
<div id="wrapper">
  <!-- wrapper -->
  <div id="wrap">
    <!-- some new wrapping element -->
    <div id="nav">
      <!-- nav -->

    </div>
    <!-- /nav -->
    <div id="container">
      <!-- container -->

    </div>
    <!-- /container -->
  </div>
</div>
<!-- /wrapper -->

关于html - 两个 DIV 在较小的 DIV 中并排,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26619170/

相关文章:

html - 为什么这个 Twitter 关注按钮不会下移?

javascript - 使用 jquery 或 PHP 捕获当前屏幕的正确图像或将 div 转换为 pdf

css - Mobile Webkit 回流问题

html - 你能在 CSS flex 中指定包装元素的顺序吗?

javascript - 以百分比形式获取光标位置

javascript - 我的预加载器没有褪色,并且一直在无限循环中滚动

html - 有没有办法用 Mechanize 处理动态加载的内容?

jquery - 水平滚动条始终可见

html - (Firefox) 如何在点击时停止按钮缩进

html - 将表格中的两行合并在一起,以便没有单元格可见