实例
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>绝对定位</title> <style> .box{ width: 300px; height: 300px; } .box1{ width:100px; height:100px; background-color:red; position: absolute;/*绝对定位,当前父元素为box*/ top:0; left:100px; } .box2{ width:100px; height:100px; background-color:green; position: absolute;/*绝对定位,当前父元素为box*/ top:100px; left:200px; } .box3{ width:100px; height:100px; background-color:blue; position: absolute;/*绝对定位,当前父元素为box*/ top:100px; left:0; } .box4{ width:100px; height:100px; background-color:gold; position: absolute;/*绝对定位,当前父元素为box*/ top:200px; left:100px; } .box5{ width:100px; height:100px; background-color:pink; position: absolute;/*绝对定位,当前父元素为box*/ top:100px; left:100px; } </style> </head> <body> <div class="box"> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="box4"></div> <div class="box5"></div> </div> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
感悟:绝对定位是根据父元素来确定自身的位置。