topプロパティ
topプロパティは、エレメントの垂直位置を指定するプロパティです。上端が基準になります。 ただし、positionプロパティがstatic以外であることが必要で、positionプロパティの値によって表示位置の基準が変わります。
top: 100px;
以下は、positionプロパティと表示位置の変化についてです。
positionの値 | 概要 |
---|---|
position:static; | topプロパティは反映されない。 |
position:absolute; | 絶対位置の設定で、基準位置はウィンドウの上端。親エレメント(親要素)にpositon:static;以外が設定されていれば、親エレメントの上端。 |
position:fixed; | 絶対位置の設定でスクロールに影響を受けない。ウィンドウ上の表示位置が固定される。基準位置はウィンドウの上端。 |
position:relative; | 相対位置の設定で、基準位置はデフォルトで表示される位置。 |
position:sticky; | スクロールに影響を受けるために、topプロパティはposition:sticky;によって位置が固定された時の垂直配置。位置が固定されていない間はtopプロパティは影響しない。 |
エレメント内の相対位置
親エレメントに対して配置を設定するには、親エレメントに対してposition:relative;を指定し、エレメントに対してはposition:absolute;を指定します。
この時にエレメントの大きさがデフォルトで100%でも自動で調整されることに注意が必要です。エレメントの幅を指定し直す必要があるかもしれません。
/* 親エレメントへの指定 */ parent_element { position: relative; } /* 調整したいエレメントへの指定 */ element { position: absolute; width: 100%; top: 10px; }
関連プロパティ
関連するプロパティに以下があります。
left | 左端からを基準にした左右の表示位置 |
right | 右端からを基準にした左右の表示位置 |
bottom | 下端からを基準にした上下の表示位置 |
構文(Syntax)
CSS
top: auto | length | initial | inherit;
DOM(JavaScript)
String = object.style.top;
object.style.top="auto | length | initial | inherit";
値(Values)
Value | 摘要 |
---|---|
auto | default値。初期の位置 |
length | 数値による設定、CSS長さの単位を参照 |
initial | 初期値に戻す |
inherit | 親要素(親エレメント)のプロパティを継承 |
備考(Remarks)
Default value | auto |
Inherited(継承) | No |
Animatable | Yes |
Example
topによる位置の例です。 親エレメントにposition:relative;、エレメントにposition:absolute;を指定し、親エレメントを表示位置の基準にしています。
関連(Related)
HTML、CSS、DOMのHTMLエレメント、Windowに関する用途一覧です。
HTML
CSS
- サイズ
-
- height、 width、 max-height、 max-width、 min-height、 min-width、 resize
- スクロール追加
- overflow、 overflow-x、 overflow-y
- マージン、パディング、ボーダー
-
- 共通
- box-sizing、 display
- マージン
- margin、 margin-bottom、 margin-left、 margin-right、 margin-top
- パディング
- padding、 padding-bottom、 padding-left、 padding-right、 padding-top
- ボーダー、罫線
-
- 全体
- border、 border-collapse、 border-color、 border-spacing、 border-style、 border-width
- 上辺
- border-top、 border-top-color、 border-top-left-radius、 border-top-right-radius、 border-top-style、 border-top-width
- 下辺
- border-bottom、 border-bottom-color、 border-bottom-left-radius、 border-bottom-right-radius、 border-bottom-style、 border-bottom-width
- 左辺
- border-left、 border-left-color、 border-left-style、 border-left-width
- 右辺
- border-right、 border-right-color、 border-right-style、 border-right-width
- 角を丸に
- border-radius
- 画像を線に
- border-image、 border-image-outset、 border-image-repeat、 border-image-slice、 border-image-source、 border-image-width
- アウトライン
- outline、 outline-color、 outline-offset、 outline-style、 outline-width
- 配置
-
- 位置調整
- bottom、 left、 right、 top、 position、 z-index
- 横並び(フレックス)
- display:flex;、 align-content、 align-items、 align-self、 justify-content、 flex-wrap、 flex-basis、 flex-direction、 flex-flow
- 配列(グリッド)
- display:grid;、 grid-area、 grid-auto-columns、 grid-auto-flow、 grid-auto-rows、 grid-column、 grid-column-end、 grid-column-gap、 grid-column-start、 grid-gap、 grid-row、 grid-row-end、 grid-row-gap、 grid-row-start、 grid-template、 grid-template-areas、 grid-template-columns、 grid-template-rows、
- フレックス、グリッドの共通 横並び順
- order
- 背景
- background、 background-attachment、 background-blend-mode、 background-clip、 background-color、 background-image、 background-origin、 background-position、 background-repeat、 background-size、 linear-gradient、 radial-gradient
- フィルタ、加工
-
- filter、 transform、 transform-origin
- ブレンド
- isolation、 linear-gradient()、 mix-blend-mode、 radial-gradient()
- 影
- box-shadow
- 透過
- opacity
- 回転
- backface-visibility、
- 立体
- perspective、 perspective-origin
- 切り取り
- clip-path
- カラム
- column-fill、 column-gap、 column-rule、 column-rule-color、 column-rule-style、 column-rule-width、 column-span、 column-width、 columns
- ページ内移動
- scroll-behavior
- 表示、非表示
- visibility
DOM
HTMLエレメント
- インターフェース、クラス
- Document、 Element、 FocusEvent、 Attr
- イベント
- blur、 focus、 focusin、 focusout、 load、 resize、 scroll、 toggle
- プロパティ
- メソッド
Window
- インターフェース
- HashChangeEvent、 PageTransitionEvent、 Window
- イベント
- beforeunload、 hashchange、 load、 pagehide、 pageshow、 unload
- プロパティ
- newURL、 oldURL、 persisted
- メソッド
- clearTimeout()、 setTimeout()