CSSでの負の値を使用した位置指定
これまでに学んだプロパティ top、right、
bottom、left は、
正の値だけでなく負の値も受け取ることができます。
例を見てみましょう。
例
次の例では、要素は親要素を基準として絶対位置指定され、
上から 0、左から 0 の位置に配置されています:
<div id="parent">
<div id="child"></div>
</div>
#parent {
position: relative;
width: 300px;
height: 300px;
margin: 50px auto;
border: 1px solid red;
}
#child {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
border: 1px solid green;
}
:
例
では、要素を上から -20px、
左から -30px の位置に移動させてみましょう:
<div id="parent">
<div id="child"></div>
</div>
#parent {
position: relative;
width: 300px;
height: 300px;
margin: 50px auto;
border: 1px solid red;
}
#child {
position: absolute;
top: -20px;
left: -30px;
width: 100px;
height: 100px;
border: 1px solid green;
}
:
次のサンプルでは、緑色のブロックが
margin を使用して画面の中央に配置され、
赤色のブロックがそのブロックを基準に位置指定されています。
提供されたサンプルに従ってページを再現してください: