为了账号安全,请及时绑定邮箱和手机立即绑定

为何我的代码会变成这样,本该在main快里的颜色跑到foot块的下面去了?

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>实例1</title>

<style type="text/css">

body{margin:0;padding:0;font-size:20px;font-weight: bold;}

.head{height:100px;background:red;}

.main{height:300px;width:600px;background:#ccc;margin:0 auto;}

.a1{height:300px;width:20%;background:pink;}

.a2{height:300px;width:50%;background:yellow;}

.a3{height:300px;width:30%;background:purple;}

.foot{height:100px;width:600px;background:blue;margin:0 auto;}

</style>

</head>

<body>

<div class="head">head</div>

<div class="main">

<div class="a1">a1</div>

<div class="a2">a2</div>

<div class="a3">a3</div>

</div>

<div class="foot">foot</div>

</body>

</html>


https://img1.sycdn.imooc.com//5c89c8680001f36d13600549.jpg

正在回答

1 回答

因为你a1,a2,a3没有设置浮动,所以没有成一行显示,而是自上而下成一列,a2接在a1下面,a3接在a2下面,a1、a2、a3总高度为900px;但是3个元素都在main里面,而main设置的高度是300px,所以main实际高度只有300px,所以foot默认放在main下面的,这样就造成了foot和a2重叠起来了。将代码改成这样就行了:

.a1{height:300px;width:20%;background:pink;float:left}

.a2{height:300px;width:50%;background:yellow;float:left}

.a3{height:300px;width:30%;background:purple;float:right}

.foot{height:100px;width:600px;background:blue;margin:0 auto;clear:both}

0 回复 有任何疑惑可以回复我~
#1

慕村7315608 提问者

感谢大神!
2019-03-15 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
如何用CSS进行网页布局
  • 参与学习       209686    人
  • 解答问题       1153    个

用最简洁的案例教你布局的那些知识,这是前端工程师基本技能

进入课程

为何我的代码会变成这样,本该在main快里的颜色跑到foot块的下面去了?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信