當我在相對元素內絕對定位一個元素時,座標是從容器的邊緣計算出來的,沒有考慮到邊界(相當於從邊界的內側定位)從邊界外部絕對定位
有沒有任何方法來定位元素,但從邊界的外側?
例如:如果我有一個沒有邊框的紅色正方形(如第一個),文本將粘貼到容器的左上角,因爲它具有top:0; left:0
。但在第二方的文本仍然有top:0;left:0
,但邊界推廣場內的文本:
.box {
position:relative;
width:150px;
height:150px;
background:red;
box-sizing:border-box;
margin:10px;
float:left;
}
.box-bordered {
border:25px solid rgba(0,0,0,0.1);
}
.text {
position:absolute;
top:0;
left:0;
color:white;
}
<div class="box">
<div class="text">Text</div>
</div>
<div class="box box-bordered">
<div class="text">Text</div>
</div>
我想它是什麼讓堅持到頂部的文本彩色區域的左上角。那可能嗎?它怎麼能做到?
注意:這是一個出於好奇心的理論問題;我知道有替代品,將工作(至少在視覺上),如使用切緣陰性,負位置值或插圖
box-shadow
:.box { position:relative; width:150px; height:150px; background:red; box-sizing:border-box; margin:10px; float:left; } .box-shadow { box-shadow:inset 0 0 0 25px rgba(0,0,0,0.1); } .text { position:absolute; top:0; left:0; color:white; }
<div class="box box-shadow"> <div class="text">Text</div> </div>
但我想知道是否有可能在保持邊界的同時進行。
請記住,邊框可以去裏面只有當盒大小:border-框;財產設置。 –
在這兩種情況下都有'box-sizing:border-box' –
如果改變html有點不打擾你,你可以看看這個https://jsfiddle.net/87hurwnu/或者更好的是不改變html https://jsfiddle.net/87hurwnu/1/ – Lidaranis