gpt4 book ai didi

html - 侧边栏和内容的 div 高度相同

转载 作者:太空宇宙 更新时间:2023-11-04 15:32:43 26 4
gpt4 key购买 nike

假设以下 html 页面:

<html>
<head></head>
<body>
<div id="wrapper">
<div id="header"/>
<div id="navigation"/>
<div id="leftcolumn"/>
<div id="content/>
<div id="footer"/>
</div>
</body>
</html>

我想要实现的是,leftcolumn div 和内容 div 始终具有相同的高度。有时leftcolumn div高于content div是因为内容少,反之亦然:当内容很多时,content div高于leftcolumn div。

现在的情况会产生一些布局恶作剧,因为正文背景、左栏背景和内容背景都是不同的颜色。

这是我的 CSS 原样:

* {
padding: 0;
margin: 0;
}

body {
font-family: Arial, Helvetica, sans-serif;
background: #FFFEE9;
}

#wrapper {
margin: 0 auto;
width: 900px;
height: auto;
}

#content {
float: left;
color: #006350;
background: #DEE3DC;
height: 100%;
display: inline;
width: 730px;
}

#contents {
margin: 35px;
}

#contents h1, h2 {
padding-bottom: 15px;
}

#contents p {
padding-top: 5px;
padding-bottom: 5px;
}

#header {
color: black;
width: 900px;
float: left;
height: 160px;
background: #FFFEE9;
margin-top: 10px;
}

#navigation p {
padding: 3px;
}

#footer {
width: 900px;
height: 40px;
clear: both;
color: white;
background: #83422D;
font-size: 80%;
}

#footer a {
color: white;
text-decoration: none;
}

#footer p {
padding: 4px;
}

#navigation {
float: left;
width: 900px;
height: 25px;
color: white;
background: #83422D;
}

#navigation a {
text-decoration: none;
color: white;
}

#navigation a:hover {
text-decoration: underline;
}

#leftcolumn {
color: white;
background: #006350;
height: 100%;
width: 170px;
float: left;
}

#leftcolumn a {
text-decoration: none;
color: white;
}

#leftcolumn a:hover {
text-decoration: underline;
}

#leftcolumn li {
padding-bottom: 10px;
}

#leftcolumn ul {
margin-left: 20px;
margin-top: 20px;
list-style: none;
}

.centered {
text-align: center;
margin: auto;
display: block;
}

#gallery {
height: 300px;
width: 650px;
}

#gallery img {
margin-right: 50px;
margin-top: 50px;
}

#gallerytext {
width: 400px;
font-size: 75%;
color: black;
position: relative;
margin-left: 155px;
margin-top: -100px;
text-align: justify;
}

我尝试在不同的 div(包装器、左列和内容)上使用 CSS 样式的组合,例如 height: 100%, height:inherit, height:auto,... 但我似乎得不到我想要的结果。底线:如果我希望 leftcolumn 和 content div 具有相同的高度,我应该使用什么 CSS 样式,而不管当时哪个更高。 (请不要使用 javascript)。

JsFiddle 示例,其中左列较大:http://jsfiddle.net/CQPnF/4/

内容较大的示例:http://jsfiddle.net/BQkme/

最佳答案

您可以通过在左右面板中使用 display:table-cell 而不是 float 来获得您想要的结果:-

我已经从您的 #content & #leftcolummn 中删除了 float 并给了它们 display:table-cell; 并且它按照您的要求工作正常。

更新的 CSS

* {
padding: 0;
margin: 0;
}

body {
font-family: Arial, Helvetica, sans-serif;
background: #FFFEE9;
}

#wrapper {
margin: 0 auto;
width: 900px;
overflow:hidden;
}

#content {
color: #006350;
background: #DEE3DC;
display:table-cell;
width: 730px;
}

#contents {
margin: 35px;
}

#contents h1, h2 {
padding-bottom: 15px;
}

#contents p {
padding-top: 5px;
padding-bottom: 5px;
}

#header {
color: black;
width: 900px;
float: left;
height: 160px;
background: #FFFEE9;
margin-top: 10px;
}

#navigation p {
padding: 3px;
}

#footer {
width: 900px;
height: 40px;
clear: both;
color: white;
background: #83422D;
font-size: 80%;
}

#footer a {
color: white;
text-decoration: none;
}

#footer p {
padding: 4px;
}

#navigation {
float: left;
width: 900px;
height: 25px;
color: white;
background: #83422D;
}

#navigation a {
text-decoration: none;
color: white;
}

#navigation a:hover {
text-decoration: underline;
}

#container {
border:1px solid red;
overflow:hidden;

}

#leftcolumn {
color: white;
background: #006350;
width: 170px;
display:table-cell;
}

#leftcolumn a {
text-decoration: none;
color: white;
}

#leftcolumn a:hover {
text-decoration: underline;
}

#leftcolumn li {
padding-bottom: 10px;
}

#leftcolumn ul {
margin-left: 20px;
margin-top: 20px;
list-style: none;
}

.centered {
text-align: center;
margin: auto;
display: block;
}

#gallery {
height: 300px;
width: 650px;
}

#gallery img {
margin-right: 50px;
margin-top: 50px;
}

#gallerytext {
width: 400px;
font-size: 75%;
color: black;
position: relative;
margin-left: 155px;
margin-top: -100px;
text-align: justify;
}

查看演示:- http://jsfiddle.net/BQkme/10/

关于html - 侧边栏和内容的 div 高度相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12069897/

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