html - 转换为无表布局

标签 html css css-tables

我目前使用 1 个表格来对齐网站的 2 个主要部分。它现在给我带来了问题,所以我想使用纯 CSS。

我在左侧有一个 205 像素宽的导航栏列。占据右边的剩余空间,我想要一个容器(所以在屏幕的右侧,占用屏幕宽度 - 200 像素)这个容器没有固定的高度,但我想要它的顶部与导航栏的顶部对齐。

这是一个 demo我目前拥有的

我希望解决方案与此类似,但不使用表格,并使容器的顶部与侧边栏的顶部对齐。

我已经多次尝试这样做(在我开始使用该表之前和之后),但都没有远程工作。我是不得已才来这里的,所以我希望有人能提供帮助。

最佳答案

Fiddle

.container{height: 100%; border: 1px solid #0f0; display: table;}
#sidebar{
    width:40%; height: 100%; display: table-cell; background: #ccc;    
}
#sidebar2{
    width:60%; height: 100%; display: table-cell; background: #f00;    
}
body, html {
    height:100%;
}

HTML

<div class="container">
  <div id="sidebar">links and whatnot go here</div>
  <div id="sidebar2">this is the container (but its top is not aligned with the sidebar as I would like)</div>    
</div>

注意:table-cell属性支持IE8+

编辑: 如果你不能使用 table-cell 那么你必须使用一些 jquery 来计算高度。检查this Fiddle

关于html - 转换为无表布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11710879/

相关文章:

javascript - 解决 Google Chrome 29 中 Canvas 崩溃的问题

php - 通过 css 和 html 添加响应样式后,注册和登录表单停止响应

jquery - 使用 jQuery UI 自动完成和选项卡 - 修改后的 CSS 冲突

css - 第一项之前和最后一项之后没有边框间距

jquery - 盒子阴影无法正常工作

html - 使文本完全出现在 div 中

jquery调整页面的容器表

css - 垂直对齐与绝对定位

html - 使用表格布局 :fixed but item is still moving over onto the next line

javascript - 手动将 HTML 代码写入网页更快,还是使用 jQuery(或普通的 Javascript)更快?