CSS相对定位是指元素相对于自身原来的位置进行定位,移动的位置不会影响其他元素的布局。
相对定位的特点是,元素仍然占据原来的空间,而不会脱离文档流,也不会影响其他元素的布局。它的定位是相对于元素自身来计算的,可以通过top、right、bottom和left属性来指定元素的位置。
下面是一个具体的代码示例:
<!DOCTYPE html> <html> <head> <style> .container { position: relative; width: 300px; height: 300px; background-color: lightgrey; } .box { position: relative; width: 100px; height: 100px; background-color: red; top: 50px; left: 50px; } </style> </head> <body> <div class="container"> <div class="box"></div> </div> </body> </html>