gpt4 book ai didi

CSS 格式出错

转载 作者:行者123 更新时间:2023-11-28 09:38:11 24 4
gpt4 key购买 nike

我希望列表 float 在菜单容器的中心。房子下方方框中的文字(这只是我为演示拍摄的随机图像)漂浮在方框的中央。

然后我希望内容容器填满菜单容器下方的大容器中剩余的所有空间。

这就是我想要得到的: what i wanted...

这就是我的结局: what i got...

显然我不知道我在做什么。这是代码:http://jsfiddle.net/krhzM/

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
ul {
list-style:none;
position:relative;
}
li {
border: 1px solid #CCC;
margin:5px;
float:left;
}
#menucontainer {
border: 1px solid #111;
width: 400px;
margin-left: auto;
margin-right: auto;
}
#contentcontainer {
width:100px;
border: 1px solid #fac;
}
#bigcontainer {
border: 2px solid #cfa;
width: 700px;
margin-left: auto;
margin-right:auto;
}
</style>
</head>
<body>
<div id="bigcontainer">
<div id="menucontainer">
<ul>
<li>
<img src="http://png-1.findicons.com/files/icons/909/archigraphs_collection/80/house.png"/>
<br/>
<a href="#">Coffee</a>
</li>
<li>
<img src="http://png-1.findicons.com/files/icons/909/archigraphs_collection/80/house.png"/>
<br/>
<a href="#">Tea</a>
</li>
<li>
<img src="http://png-1.findicons.com/files/icons/909/archigraphs_collection/80/house.png"/>
<br/>
<a href="#">Coca Cola</a>
</li>
</ul>
</div>
<div id="contentcontainer">
<h1>this is my content</h1>
</div>
</div>
</body>
</html>

最佳答案

float 元素的高度不会插入/扩展或影响父元素的高度,除非父元素在末尾包含一个 clear。

使用 float 时需要在父容器上使用一些clearfix方法。

http://css-tricks.com/snippets/css/clear-fix/

将以下内容添加到您的 CSS:

.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { display: inline-block; }
/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* close commented backslash hack */

对于所有包含 float 子元素的父容器,添加类 .clearfix ,像这样:

<div class="clearfix">
<div class="float-left">This is floated left.</div>
<div class="float-right">This is floated right.</div>
</div>

在您的情况下,您需要添加 .clearfix给你上课 <ul>包含在您的 #menucontainer 中div,因为你在 float <li>标签。

如果您仍然感到困惑,请查看此涉及 float 和清除的不同场景的演示,以更好地了解其工作原理:http://themergency.com/clearfix/clearfix_demo_4_micro_clearfix.htm

关于CSS 格式出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16590253/

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