設(shè)置方法:1、給a標(biāo)簽的父標(biāo)簽添加“position: relative;”樣式,進(jìn)行相對(duì)定位;2、給a標(biāo)簽添加“position: absolute;”樣式進(jìn)行絕對(duì)定位;3、用top、bottom、left、right屬性控制a標(biāo)簽位置。
本教程操作環(huán)境:windows7系統(tǒng)、CSS3&&HTML5版、Dell G3電腦。
在html中,可以利用position屬性來(lái)設(shè)置a標(biāo)簽位置:
-
給a標(biāo)簽的父標(biāo)簽添加“position: relative;”樣式,進(jìn)行相對(duì)定位;
-
給a標(biāo)簽添加“position: absolute;”樣式進(jìn)行絕對(duì)定位;
-
用top、bottom、left、right屬性控制a標(biāo)簽位置。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> div{ width: 300px; height: 200px; background-color: yellow; position: relative; } a{ position: absolute; top: 50px; left: 50px; } </style> </head> <body> <div> <a>我是一個(gè)a標(biāo)簽</a> </div> </body> </html>
說(shuō)明:
position屬性指定一個(gè)元素(靜態(tài)的,相對(duì)的,絕對(duì)或固定)的定位方法的類型。
-
relative:生成相對(duì)定位的元素,相對(duì)于其正常位置進(jìn)行定位。因此,"left:20" 會(huì)向元素的 LEFT 位置添加 20 像素。
-
absolute:生成絕對(duì)定位的元素,相對(duì)于 static 定位以外的第一個(gè)父元素進(jìn)行定位。元素的位置通過(guò) "left", "top", "right" 以及 "bottom" 屬性進(jìn)行規(guī)定。
推薦教程:《html視頻教程》