带有 css /html 的基本分屏

the*_*ide 5 html css css-position css-float

我正在尝试创建两个单独的 div,一个覆盖屏幕的左半部分,一个覆盖屏幕的右侧。为什么我的代码只创建了一个 div 覆盖页面的左半部分,当我包含float:left和 时float:right?我该如何解决?

#col-1 {
  position: fixed;
  width: 50%;
  float: left;
  height: 100%;
  background-color: #282828;
  z-index: 1010101010
}

#col-2 {
  position: fixed;
  width: 50%;
  float: right;
  height: 100%;
  background-color: #0080ff;
  z-index: 1010101010
}
Run Code Online (Sandbox Code Playgroud)
<div id="col-1">
  <h1>This is half of a page</h1>
</div>
<div id="col-2">
  <h1>This is another half of a page</h1>
</div>
Run Code Online (Sandbox Code Playgroud)

在 JSFiddle 上查看

ALD*_*ENT 7

使用弹性盒

.container {
  display: flex;
}

#col-1 {
  background-color: yellow;
  flex: 1;
}

#col-2 {
  background-color: orange;
  flex: 1;
}
Run Code Online (Sandbox Code Playgroud)
<section class="container">
<div id="col-1">
  <h1>This is half of a page</h1>
</div>
<div id="col-2">
  <h1>This is another half of a page</h1>
</div>
</section>
Run Code Online (Sandbox Code Playgroud)


Tan*_*ock 5

#col-1 {
  position: relative;
  width: 50%;
  float: left;
  height: 100%;
  background-color: #282828;
  z-index: 1010101010
}

#col-2 {
  position: relative;
  width: 50%;
  float: left;
  height: 100%;
  background-color: #0080ff;
  z-index: 1010101010
}
Run Code Online (Sandbox Code Playgroud)
<div id="col-1">
  <h1>This is half of a page</h1>
</div>
<div id="col-2">
  <h1>This is another half of a page</h1>
</div>
Run Code Online (Sandbox Code Playgroud)

这对我有用。我换position: fixedrelative。此外,它们都应该是 float:left。如果你这样做float:right,右边的会分散,他们应该都离开。

另外,只是我的一个建议,我喜欢在我的页面上做 - 如果使用得当,我是表格的忠实粉丝。表格倾向于将事物放在一起,具有相同的测量和对齐方式。它为您做了很多造型工作。尝试做的东西<table><tbody><thead>标签。

  • 关于您使用表格的建议的旁注:您应该**不要**将表格用于不需要表格的任何其他内容。永远不要将它们用于布局,这是一个糟糕的主意,并且在调整移动网站(以及其他内容)时它们将不起作用。非常不鼓励将它们用作布局管理器。相反,学习 CSS 并以正确的方式使用它。一本好书:[**不要**使用表格进行布局](https://www.thoughtco.com/dont-use-tables-for-layout-3468941) (2认同)