经典论坛 » WEB标准化专栏 » 看看我说的对不对_百分比定位
中级会员
查看详细资料
TOP
我爱阿仙奴
高级会员
专长 前端制作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <style type="text/css"> <!-- * {margin:0;padding:0} div { width:500px; height:500px; background-color:#3366CC; border:1px solid #ccc; overflow:hidden; position:relative; text-align:center; /*for FF*/ display:table-cell; vertical-align:middle; } div p { background-color:#99CC00; /*for IE*/ +position:absolute; top:50%; left:50%; } img { /*for IE*/ +position:relative; top:-50%; left:-50%; } --> </style> </head> <div><p><img src="http://www.google.com/intl/en/images/logo.gif" /></p></div> <body> </body> </html>
查看个人网站
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <style type="text/css"> <!-- * { margin:0;padding:0; } div { width:500px; height:500px; background-color:#3366CC; border:1px solid #ccc; overflow:hidden; position:relative; } div p { background-color:#99CC00; position:absolute; top:50%; } img { position:relative; top:-50%; left:-50%; } --> </style> </head> <div><p><img src="http://www.google.com/intl/en/images/logo.gif" /></p></div> <body> </body> </html>
所以相对定位的百分比是相对它自己的尺寸来计算