LoginSignup
35
17

More than 5 years have passed since last update.

親要素の表示領域内でabsoluteな配置

Posted at

親要素の表示領域内でabsoluteな配置

普通にposition: absoluteを使うと画面内での絶対座標に配置されてしまう。

だが、親要素のpositionstatic以外を指定することで、親要素の表示領域内での絶対座標に配置することができる。

.parent {
  position: relative;

  margin-left: auto;
  margin-right: auto;
  width: 80%;
}

.child {
  position: absolute;
  right: 0px;
  top: 0px;
}

static以外だとpositionにはabsoltute, relative, fixedが指定可能だけれども、
ここではtop等を指定しなければstaticとなんら変わらないrelativeを親要素.parentに指定している。
absolutefixedを指定した場合には絶対座標に配置されてしまうためである。

35
17
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
17