HTML&CSS



1.利用margin值为负数

左右宽度固定中间自适应。补充:background-color填充:content、padding、border。

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/http; charset=utf-8">  
  5.         <title>测试页</title>  
  6.         <style type="text/css">  
  7.         body{  
  8.             margin: 0;  
  9.             padding: 0;  
  10.         }  
  11.         .main{  
  12.             background-color: black;  
  13.             padding:0 300px;  
  14.             height: 700px;  
  15.         }  
  16.         .main-left{  
  17.             width: 300px;  
  18.             height: 500px;  
  19.             background-color: #98FF1A;  
  20.             float: left;  
  21.             margin-left:-300px;  
  22.         }         
  23.         .main-center{  
  24.             background-color: #8E8DCC;  
  25.             height: 500px;  
  26.         }  
  27.         .main-right{  
  28.             width: 300px;  
  29.             height: 500px;  
  30.             background-color:#7CC0FF;  
  31.             float: right;  
  32.             margin-right: -300px;  
  33.         }  
  34.       
  35.         </style>  
  36.     </head>  
  37.     <body>  
  38.         <div class="main">  
  39.             <div class="main-left"></div>  
  40.             <div class="main-right"></div>  
  41.             <div class="main-center"></div>  
  42.         </div>  
  43.     </body>  
  44. </html>  
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/http; charset=utf-8">
		<title>测试页</title>
		<style type="text/css">
		body{
			margin: 0;
			padding: 0;
		}
		.main{
			background-color: black;
			padding:0 300px;
			height: 700px;
		}
		.main-left{
			width: 300px;
			height: 500px;
			background-color: #98FF1A;
			float: left;
			margin-left:-300px;
		}		
		.main-center{
			background-color: #8E8DCC;
			height: 500px;
		}
		.main-right{
			width: 300px;
			height: 500px;
			background-color:#7CC0FF;
			float: right;
			margin-right: -300px;
		}
	
		</style>
	</head>
	<body>
		<div class="main">
			<div class="main-left"></div>
			<div class="main-right"></div>
			<div class="main-center"></div>
		</div>
	</body>
</html>

2.利用绝对定位

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/http; charset=utf-8">  
  5.         <title>测试页</title>  
  6.         <style type="text/css">  
  7.         body{  
  8.             margin: 0;  
  9.             padding: 0;  
  10.         }  
  11.         .main{  
  12.             background-color: black;  
  13.             margin:0 auto;  
  14.             height: 700px;  
  15.         }  
  16.         .main-left{  
  17.             width: 300px;  
  18.             height: 500px;  
  19.             background-color: #98FF1A;  
  20.             float: left;  
  21.             position: absolute;  
  22.             left: 0;  
  23.         }         
  24.         .main-center{  
  25.             background-color: #8E8DCC;  
  26.             height: 500px;  
  27.         }  
  28.         .main-right{  
  29.             width: 300px;  
  30.             height: 500px;  
  31.             background-color:#7CC0FF;  
  32.             float: right;  
  33.             position: absolute;  
  34.             right: 0;  
  35.         }  
  36.       
  37.         </style>  
  38.     </head>  
  39.     <body>  
  40.         <div class="main">  
  41.             <div class="main-left"></div>  
  42.             <div class="main-right"></div>  
  43.             <div class="main-center"></div>  
  44.         </div>  
  45.     </body>  
  46. </html>  
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/http; charset=utf-8">
		<title>测试页</title>
		<style type="text/css">
		body{
			margin: 0;
			padding: 0;
		}
		.main{
			background-color: black;
			margin:0 auto;
			height: 700px;
		}
		.main-left{
			width: 300px;
			height: 500px;
			background-color: #98FF1A;
			float: left;
			position: absolute;
			left: 0;
		}		
		.main-center{
			background-color: #8E8DCC;
			height: 500px;
		}
		.main-right{
			width: 300px;
			height: 500px;
			background-color:#7CC0FF;
			float: right;
			position: absolute;
			right: 0;
		}
	
		</style>
	</head>
	<body>
		<div class="main">
			<div class="main-left"></div>
			<div class="main-right"></div>
			<div class="main-center"></div>
		</div>
	</body>
</html>

3.混合布局。

先弄成一列布局的:有head、main和foot。它们都要设置高度、宽度。

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/http; charset=utf-8">  
  5.         <title>测试页</title>  
  6.         <style type="text/css">  
  7.         body{  
  8.             margin: 0;  
  9.             padding: 0;  
  10.         }  
  11.         .head{  
  12.             height: 80px;  
  13.             background-color:black;  
  14.         }  
  15.         .main{  
  16.             height: 500px;  
  17.             width: 800px;  
  18.             background-color: red;  
  19.             margin: 0 auto;  
  20.             position: relative;  
  21.         }  
  22.         .foot{  
  23.             height: 100px;  
  24.             width:800px;  
  25.             background-color:#ddd;  
  26.             margin:0 auto;  
  27.         }  
  28.           
  29.         </style>  
  30.     </head>  
  31.     <body>  
  32.         <div class="head"></div>  
  33.         <div class="main">  
  34.         </div>  
  35.         <div class="foot"></div>  
  36.           
  37.     </body>  
  38. </html>  
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/http; charset=utf-8">
		<title>测试页</title>
		<style type="text/css">
		body{
			margin: 0;
			padding: 0;
		}
		.head{
			height: 80px;
			background-color:black;
		}
		.main{
			height: 500px;
			width: 800px;
			background-color: red;
			margin: 0 auto;
			position: relative;
		}
		.foot{
			height: 100px;
			width:800px;
			background-color:#ddd;
			margin:0 auto;
		}
		
		</style>
	</head>
	<body>
		<div class="head"></div>
		<div class="main">
		</div>
		<div class="foot"></div>
		
	</body>
</html>


然后把main进行分割。

分成两列,宽度都固定,采用绝对定位。一个在左上角,一个在右上角。而main本身要弄成相对定位(如果不弄成相对定位,它的子元素会以body为准进行left、top、right的定位)。
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/http; charset=utf-8">  
  5.         <title>测试页</title>  
  6.         <style type="text/css">  
  7.         body{  
  8.             margin: 0;  
  9.             padding: 0;  
  10.         }  
  11.         .head{  
  12.             height: 80px;  
  13.             background-color:black;  
  14.         }  
  15.         .main{  
  16.             height: 500px;  
  17.             width: 800px;  
  18.             background-color: red;  
  19.             margin: 0 auto;  
  20.             position: relative;  
  21.         }  
  22.         .foot{  
  23.             height: 100px;  
  24.             width:800px;  
  25.             background-color:#ddd;  
  26.             margin:0 auto;  
  27.         }  
  28.         .left{  
  29.             width: 200px;  
  30.             height: 600px;  
  31.             float: left;  
  32.             position: absolute;  
  33.             top: 0;  
  34.             left: 0;  
  35.             background-color: #cef;  
  36.         }  
  37.         .right{  
  38.             height: 800px;  
  39.             width:600px;  
  40.             float: right;  
  41.             position: absolute;  
  42.             right: 0;  
  43.             background-color: orange;  
  44.         }  
  45.         </style>  
  46.     </head>  
  47.     <body>  
  48.         <div class="head"></div>  
  49.         <div class="main">  
  50.             <div class="left"></div>  
  51.             <div class="right"></div>  
  52.         </div>  
  53.         <div class="foot"></div>  
  54.           
  55.     </body>  
  56. </html>  
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/http; charset=utf-8">
		<title>测试页</title>
		<style type="text/css">
		body{
			margin: 0;
			padding: 0;
		}
		.head{
			height: 80px;
			background-color:black;
		}
		.main{
			height: 500px;
			width: 800px;
			background-color: red;
			margin: 0 auto;
			position: relative;
		}
		.foot{
			height: 100px;
			width:800px;
			background-color:#ddd;
			margin:0 auto;
		}
		.left{
			width: 200px;
			height: 600px;
			float: left;
			position: absolute;
			top: 0;
			left: 0;
			background-color: #cef;
		}
		.right{
			height: 800px;
			width:600px;
			float: right;
			position: absolute;
			right: 0;
			background-color: orange;
		}
		</style>
	</head>
	<body>
		<div class="head"></div>
		<div class="main">
			<div class="left"></div>
			<div class="right"></div>
		</div>
		<div class="foot"></div>
		
	</body>
</html>



还可以把main的右边再分栏:

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/http; charset=utf-8">  
  5.         <title>测试页</title>  
  6.         <style type="text/css">  
  7.         body{  
  8.             margin: 0;  
  9.             padding: 0;  
  10.         }  
  11.         .head{  
  12.             height: 80px;  
  13.             background-color:black;  
  14.         }  
  15.         .main{  
  16.             height: 500px;  
  17.             width: 800px;  
  18.             background-color: red;  
  19.             margin: 0 auto;  
  20.             position: relative;  
  21.         }  
  22.         .foot{  
  23.             height: 100px;  
  24.             width:800px;  
  25.             background-color:#ddd;  
  26.             margin:0 auto;  
  27.         }  
  28.         .left{  
  29.             width: 200px;  
  30.             height: 500px;  
  31.             float: left;  
  32.             position: absolute;  
  33.             top: 0;  
  34.             left: 0;  
  35.             background-color: #cef;  
  36.         }  
  37.         .right{  
  38.             height: 500px;  
  39.             width:600px;  
  40.             float: right;  
  41.             position: absolute;  
  42.             right: 0;  
  43.             background-color: orange;  
  44.         }  
  45.         .right-1{  
  46.             width: 400px;  
  47.             background-color: #0ff;  
  48.             float: left;  
  49.             height: 500px;  
  50.         }  
  51.         .right-2{  
  52.             width: 200px;  
  53.             height: 500px;  
  54.             background-color: #ff0;  
  55.             float: right;  
  56.         }  
  57.         </style>  
  58.     </head>  
  59.     <body>  
  60.         <div class="head"></div>  
  61.         <div class="main">  
  62.             <div class="left"></div>  
  63.             <div class="right">  
  64.                 <div class="right-1"></div>  
  65.                 <div class="right-2"></div>  
  66.             </div>  
  67.         </div>  
  68.         <div class="foot"></div>  
  69.           
  70.     </body>  
  71. </html>  
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/http; charset=utf-8">
		<title>测试页</title>
		<style type="text/css">
		body{
			margin: 0;
			padding: 0;
		}
		.head{
			height: 80px;
			background-color:black;
		}
		.main{
			height: 500px;
			width: 800px;
			background-color: red;
			margin: 0 auto;
			position: relative;
		}
		.foot{
			height: 100px;
			width:800px;
			background-color:#ddd;
			margin:0 auto;
		}
		.left{
			width: 200px;
			height: 500px;
			float: left;
			position: absolute;
			top: 0;
			left: 0;
			background-color: #cef;
		}
		.right{
			height: 500px;
			width:600px;
			float: right;
			position: absolute;
			right: 0;
			background-color: orange;
		}
		.right-1{
			width: 400px;
			background-color: #0ff;
			float: left;
			height: 500px;
		}
		.right-2{
			width: 200px;
			height: 500px;
			background-color: #ff0;
			float: right;
		}
		</style>
	</head>
	<body>
		<div class="head"></div>
		<div class="main">
			<div class="left"></div>
			<div class="right">
				<div class="right-1"></div>
				<div class="right-2"></div>
			</div>
		</div>
		<div class="foot"></div>
		
	</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值