gpt4 book ai didi

css 高度相关问题

转载 作者:行者123 更新时间:2023-11-28 13:41:35 25 4
gpt4 key购买 nike

这更多的是要求解释以帮助解决问题...在设置网站布局时,我一直在设置我所有 div 的背景颜色,以便我可以看到每个 div 的背景颜色正在显示等。只是为了获得基本的 shell 设置。

然而,我曾注意到,虽然我的页脚 div 中的内容显示在正确的位置,但背景颜色显示的比它高得多,并且显示在页脚上方出现的一些盒装内容后面。我的“盒子”div(包含 3 个水平盒子)的背景颜色根本没有显示,页脚颜色显示了盒子颜色应有的位置。我一直认为,由于我没有为盒子指定高度,它会延伸到放置在其中的 3 个水平盒子的高度……但显然情况并非如此。一旦我将“框”div 设置为与水平框的高度相同,颜色就会正确显示并将页脚向下推到正确的位置。

有人可以向我解释为什么会这样吗?为什么我必须指定“框”的高度才能显示它的背景颜色,为什么它不只是拉伸(stretch)到其中包含的水平框的高度?

这里有一些 jsFiddle 链接以查看差异(如果有用的话,我还包括下面的实际代码)。请注意,我想看到的“框”背景颜色是黑色..页脚是红色。

未指定高度且页脚(红色)过高:http://jsfiddle.net/KgFKH/

指定高度并正确显示:http://jsfiddle.net/eD4d7/

如果这是一个愚蠢的问题,我深表歉意,我一直认为容器 div 会拉伸(stretch)到其中元素的高度......所以我觉得这很令人费解!

html:

<!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">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>


<body>


<div id="header-stretch">
<div id="header" class="container">
<h2>THIS IS THE HEADER</h2>
<p>nav<br />will<br />go</br >here
</div><!--header-->
</div><!--header stretch-->

<div id="slider-stretch">
<div id="slider" class="container">
<p>Slider will go here...</p>
</div><!--slider-->
</div><!--slider stretch -->

<div id="main-stretch">
<div id="main" class="container">
<p>Main block of content will be in here.</p>
</div><!--main container-->
</div><!--main stretch-->

<div id="boxes-stretch">
<div id="boxes" class="container">
<div class="box" id="box1">
<h2>Box 1 Heading</h2>
<p>Box 1 content here...</p>
<p class="c2action"><a href="#">learn more</a></p>
</div><!--box1-->

<div class="box" id="box2">
<h2>Box 2 Heading</h2>
<p>Box 2 <br />content here...</p>
<p class="c2action"><a href="#">learn more</a></p>
</div><!--box2-->

<div class="box" id="box3">
<h2>Box 3 Heading</h2>
<p>Box 3<br /> content <br />here...</p>
<p class="c2action"><a href="#">Click here for Map &amp; Directions</a></p>
</div><!--box3-->
</div><!--boxes-->
</div><!--boxes-stretch-->

<div id="footer-stretch">
<div id="footer" class="container">
<p>&copy; Footer</p>
</div><!--footer-->
</div><!--footer stretch-->

</body>

css(未声明高度):

html, body, div, span, applet, object, iframe,  
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}

body {background: #FFFFFF; font-family: Arial, verdana, sans-serif;}

.container {margin: 0 auto; width: 940px;}

#header-stretch{background:#ffff00;}

#header {overflow: hidden; padding-bottom: 10px;}

#header h1 a {
background: url("images/logo.gif") no-repeat;
float: left;
height: 37px;
width: 191px;
margin-top: 40px;
text-indent: -9999px;
}

#header ul{
float: right;
list-style: none;
margin-top: 50px;
}

#header ul li{
float: left;
margin-left: 10px;
padding: 10px 0 10px 12px;
}

#header ul li a {
text-decoration: none;
font-size: 17px;
padding: 10px 0px 10px 2px;
color:#878686;
}

#slider-stretch{
background-color:#9900ff;
}

#slider{
background-color:#00FF00;
height: 260px;
}

#main-stretch{
background-color:#3c0303;
height: 170px;
}

#main{
padding:15px 0px;
}

#main p{
padding-top:15px;
color:#FFFFFF;
}

#boxes-stretch{
background-color:#0000FF;

}

#boxes{
margin-top: 20px;
background-color:#000000;

}

.box{
position:relative;
width:296px;
height:270px;
float:left;
background-color:#999999;

}

.box h2{
font-size: 16px;
margin-top: 18px;
margin-left: 24px;
color: #353535;
}

.box img{
margin-top: 10px;
margin-left: 24px;
}

.box p{
margin-top: 10px;
margin-left: 24px;
width: 252px;
font-size:13px;
color:#525151;
}

p.c2action{
position:absolute;
bottom:10px;
text-align:right;
font-size: 14px;
}

.c2action a{
color:#353535;
}


#box1{
margin-right: 20px;
}

#box2{
margin-right: 20px;
}


#footer-stretch{
background-color:#ff0000;
}

#footer{
padding-top:10px;
padding-bottom:10px;
font-size: 11px;
color:#7e7e7e;
overflow:hidden;
}

#footer p {
text-align: center;
}

css(声明高度):

html, body, div, span, applet, object, iframe,  
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}

body {background: #FFFFFF; font-family: Arial, verdana, sans-serif;}

.container {margin: 0 auto; width: 940px;}

#header-stretch{background:#ffff00;}

#header {overflow: hidden; padding-bottom: 10px;}

#header h1 a {
background: url("images/logo.gif") no-repeat;
float: left;
height: 37px;
width: 191px;
margin-top: 40px;
text-indent: -9999px;
}

#header ul{
float: right;
list-style: none;
margin-top: 50px;
}

#header ul li{
float: left;
margin-left: 10px;
padding: 10px 0 10px 12px;
}

#header ul li a {
text-decoration: none;
font-size: 17px;
padding: 10px 0px 10px 2px;
color:#878686;
}

#slider-stretch{
background-color:#9900ff;
}

#slider{
background-color:#00FF00;
height: 260px;
}

#main-stretch{
background-color:#3c0303;
height: 170px;
}

#main{
padding:15px 0px;
}

#main p{
padding-top:15px;
color:#FFFFFF;
}

#boxes-stretch{
background-color:#0000FF;

}

#boxes{
margin-top: 20px;
background-color:#000000;
height:270px;
}

.box{
position:relative;
width:296px;
height:270px;
float:left;
background-color:#999999;

}

.box h2{
font-size: 16px;
margin-top: 18px;
margin-left: 24px;
color: #353535;
}

.box img{
margin-top: 10px;
margin-left: 24px;
}

.box p{
margin-top: 10px;
margin-left: 24px;
width: 252px;
font-size:13px;
color:#525151;
}

p.c2action{
position:absolute;
bottom:10px;
text-align:right;
font-size: 14px;
}

.c2action a{
color:#353535;
}


#box1{
margin-right: 20px;
}

#box2{
margin-right: 20px;
}


#footer-stretch{
background-color:#ff0000;
}

#footer{
padding-top:10px;
padding-bottom:10px;
font-size: 11px;
color:#7e7e7e;
overflow:hidden;
}

#footer p {
text-align: center;
}

最佳答案

您没有包含(或清除) float 框。有很多方法可以做到这一点,一种方法是在容器上使用 overflow: hidden(不要在此容器上设置显式高度):

http://jsfiddle.net/KgFKH/1/

您还可以在具有 clear 的 float 下方添加一个空的 div;两者都是

关于css 高度相关问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7312407/

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