用HTML的嵌套div的方法画一个田字格

本文介绍了如何利用HTML的嵌套div方法来绘制田字格,通过这种方法,无需使用'clear:left'属性,简化了代码实现。文中提到了百度经验中的一种方法,并分享了作者自己的学习心得。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在网上学习HTML,老师留了个画田字格的作业,恕我愚钝最终求助了百度经验,作者用了"clear:left"的方法来画,后来接着学发现只要用嵌套div的方法就可以画出来了,好开心,代码如下:

百度经验的代码:百度经验的链接配套详细说明字,很赞!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>田字格布局</title>
<style>
#prat1{
width: 200px;
height: 200px;
background: blue;
float: left;
}
#prat2{
width: 200px;
height: 200px;
background: red;
float: left;
}
#prat3{
width: 200px;
height: 200px;
background: yellow;
float: left;
clear: left;
}
#prat4{
width: 200px;
height: 200px;
background: green;
float: left;
}
</style>
</head>
<body>
<div id="prat1">块1</div>
<div id="prat2">块2</div>
<div id="prat3">块3</div>
<div id="prat4">块4</div>
</body>
</html>

用嵌套的方法(我的第一段HTML代码,:-P)不需要用到“float:left”

<!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="zh-CN">
<head>
<style>
	#top{
		width:200px;
		height:100px;
	}
	#bottom{
		width:200px;
		height:100px;
	}
	#top1{
		width:100px;
		height:100px;
		float:left;
		background:green;
	}
	#top2{
		width:100px;
		height:100px;
		float:left;
		background:red;
	}
	#btm1{
		width:100px;
		height:100px;
		float:left;
		background:orange;
	}
	#btm2{
		width:100px;
		height:100px;
		float:left;
		background:gray;
	}
</style>
</head>
    <body>
    	<div id="top">
    		<div id="top1">top1</div>
    		<div id="top2">top2</div>
    	</div>
    	<div id="bottom">
    		<div id="btm1">btm1</div>
    		<div id="btm2">btm2</div>
    	</div>
    </body>
</html>

自己摸索程序确实有种莫名的快乐哟~~~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值