gpt4 book ai didi

css - 三行无表格 CSS 布局,中间行填充剩余空间

转载 作者:技术小花猫 更新时间:2023-10-29 11:03:24 25 4
gpt4 key购买 nike

我需要的是一个基于像素的高度包含 3 行的 div。顶行的高度取决于内容。底行具有固定高度。中间一行填充所有剩余空间。一切都是宽度 100%。

数小时以来,我一直在努力构建一个 div 和基于 CSS 的布局,这实际上需要 使用表格来完成。我已经尝试了很多方法,包括负底边距、嵌套 div、各种定位、高度设置、显示:表格,没有什么能满足我的需要。我搜索了这个网站和互联网,刷新了我对液体布局的各种方法的内存。没有用。

我并不特别担心与 IE6 等“旧”浏览器的兼容性(此应用程序不供“公共(public)”使用)。只要让它在 IE8+FF+Chrome 中工作就很棒。

我已将问题剥离到下面发布的一个简单示例,以及显示我想要的基于表格的布局。旁注:我喜欢 CSS 和无表格布局,但是,有时我们为了让它工作而必须经历的时间似乎很荒谬。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
.container {width:500px;height:200px;border:1px solid black;background-color:#c0c0c0;position:relative;}

/* Styles for colors */
#top td, .top {width:100%;background-color:pink;}
#mid td, .mid {width:100%;background-color:lightgreen;border:1px solid red;}
#bot td, .bot {width:100%;background-color:lightblue;}

/* Styles for Table-based Layout */
#layout {width:100%;height:100%;border-collapse:collapse;}
#layout td {vertical-align:top;padding:0px;}
#top td {}
#mid td {height:100%;}
#bot td {height:2em;}

/* Styles for Table-less Layout */
.top {}
.mid {}
.bot {height:2em;position:absolute;bottom:0px;}

</style>
</head>
<body>

Layout I want (with tables):
<div class="container">
<table id="layout">
<tr id="top"><td>Top:<br/>Content-based<br/>Height</td></tr>
<tr id="mid"><td>Middle:<br/>Fill remaining space</td></tr>
<tr id="bot"><td>Bottom: Fixed Height</td></tr>
</table>
</div>

<hr/>

Best I can get with CSS:
<div class="container">
<div class="top">Top:<br/>Content-based<br/>Height</div>
<div class="mid">Middle:<br/>Fill remaining space</div>
<div class="bot">Bottom: Fixed Height</div>
</div>

</body>
</html>

与此同时,我不能让这阻止我的进步,已经花了太多时间。我将继续我元素中的表格布局。它很简单,完全满足要求,即使纯粹主义者在某处哀嚎。

尽管如此,还是要感谢您的任何建议 - 我主要好奇此时“正确”的解决方案是什么,我讨厌被难倒。当然,这是可行的?

最佳答案

您的问题的关键是以不同的方式看待问题——如果您使用 Google“粘性页脚”,您会找到如下解决方案:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
.container {width:500px;height:200px;border:1px solid black;background-color:#c0c0c0;position:relative;}

.notbottom2 {
min-height: 100%; background-color:lightgreen; width: 100%;
height: auto !important;
height: 100%;
}

.mid2 {padding-bottom: 2em;}
.top2 { width: 100%; background-color: pink;}
.bottom2 { height: 2em; width: 100%; margin-top: -2em; background-color: lightblue; }

</style>
</head>
<body>
<div class="container">
<div class="notbottom2">
<div class="top2">Top:<br/>Content-based<br/>Height</div>
<div class="mid2">Middle:<br/>Fill remaining space</div>
</div>
<div class="bottom2">Bottom: Fixed Height</div>
</div>
</body>
</html>

编辑:在那里,这应该是你想要的,或多或少。

关于css - 三行无表格 CSS 布局,中间行填充剩余空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1703455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com