left
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
* Some parts of this feature may have varying levels of support.
left は CSS のプロパティで、位置指定要素の水平位置の決定に関与します。このインセットプロパティは、位置指定されていない要素には効果がありません。
試してみましょう
left: 0;
left: 4em;
left: 10%;
left: 20px;
<section id="default-example">
<div class="example-container">
<div id="example-element">これは絶対位置指定されています。</div>
<p>
まるで地球の表面から水が引き始めたかのように、通りは泥だらけで、ホルボーン通りを象のようなトカゲのようによちよち歩く、体長 12 メートルほどのメガロサウルスに出会っても不思議ではないでしょう。
</p>
</div>
</section>
.example-container {
border: 0.75em solid;
padding: 0.75em;
text-align: left;
position: relative;
width: 100%;
min-height: 200px;
}
#example-element {
background-color: #264653;
border: 4px solid #ffb500;
color: white;
position: absolute;
width: 140px;
height: 60px;
}
構文
css
/* <length> 値 */
left: 3px;
left: 2.4em;
left: anchor(--my-anchor 50%);
left: calc(anchor-size(--my-anchor inline, 100px) * 2);
/* 包含ブロックの幅に対する <percentage> */
left: 10%;
/* キーワード値 */
left: auto;
/* グローバル値 */
left: inherit;
left: initial;
left: revert;
left: revert-layer;
left: unset;
値
<length>-
負、null、または正の
<length>で、以下のものを表します。- 絶対位置指定要素の場合は、包含ブロックの左端までの距離を表します。
- アンカー位置指定要素の場合は、
anchor()関数は、関連付けられたアンカー要素の左端または右端の位置を基準とした相対的な<length>値に解決されます(詳細はインセットプロパティでanchor()関数値を使用を参照)。また、anchor-size()関数は、関連するアンカー要素の幅または高さに相対的な<length>値に解決します(アンカーサイズに基づく要素の位置の設定 を参照)。 - 相対位置指定要素の場合は、通常の位置からの右方向への移動量を表します。
<percentage>-
包含ブロックの幅に対する
<percentage>です。 auto-
以下のように指定します。
解説
left の効果は、要素がどの様に配置されているか(つまり、 position プロパティの値)によって変わります。
positionがabsoluteまたはfixedに設定されている場合、leftプロパティは要素の左辺の外側のマージンと、包含ブロックの左辺の内側のマージンとの間の距離を指定します。(包含ブロックは、この要素が相対位置指定される祖先要素です。)位置指定要素に関連付けられたアンカー要素があり、プロパティ値にanchor()関数が含まれいる場合、leftは位置指定要素の左端を指定された<anchor-side>の辺からの相対位置を基準とします。leftプロパティは、left、right、start、end、self-start、self-end、center、および<percentage>値と互換性があります。positionがrelativeに設定されている場合、leftプロパティは要素の左辺が通常位置から右方向へ移動する量を指定します。positionがstickyに設定されている場合、leftプロパティは sticky 制約の矩形を計算するために使用されます。positionがstaticに設定されている場合、leftプロパティは効果がありません。
left と right の両方が定義されていて、幅の制約がない場合、要素は両方を満たすように伸縮されます。要素が両方を満たすように伸縮できない場合、要素の位置は過剰指定になります。このような場合、コンテナーが左書きの場合は left の値が優先され、コンテナーが右書きの場合は right の値が優先されます。
公式定義
形式定義
left =
auto |
<length-percentage> |
<anchor()> |
<anchor-size()>
<length-percentage> =
<length> |
<percentage>
<anchor()> =
anchor( <anchor-name>? &&
<anchor-side> , <length-percentage>? )
<anchor-size()> =
anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )
<anchor-name> =
<dashed-ident>
<anchor-side> =
inside |
outside |
top |
left |
right |
bottom |
start |
end |
self-start |
self-end |
<percentage> |
center
<anchor-size> =
width |
height |
block |
inline |
self-block |
self-inline
例
>位置指定要素
HTML
html
<div id="wrap">
<div id="example_1">
<pre>
position: absolute;
left: 20px;
top: 20px;
</pre>
<p>
この div を含んでいる要素はメインウィンドウのみであるため、それに関連して位置を決定します。
</p>
</div>
<div id="example_2">
<pre>
position: relative;
top: 0;
right: 0;
</pre>
<p>兄弟要素から相対の相対位置指定。</p>
</div>
<div id="example_3">
<pre>
float: right;
position: relative;
top: 20px;
left: 20px;
</pre>
<p>上の兄弟 div に相対的ですが、コンテンツのフローから除外されています。</p>
<div id="example_4">
<pre>
position: absolute;
bottom: 10px;
right: 20px;
</pre>
<p>相対位置指定の親の内部で絶対位置指定</p>
</div>
<div id="example_5">
<pre>
position: absolute;
right: 0;
left: 0;
top: 200px;
</pre>
<p>left と right の両方を宣言した絶対位置指定</p>
</div>
</div>
</div>
CSS
css
#wrap {
width: 700px;
margin: 0 auto;
background: #5c5c5c;
}
pre {
white-space: pre-line;
word-wrap: break-word;
}
#example_1 {
width: 200px;
height: 200px;
position: absolute;
left: 20px;
top: 20px;
background-color: #d8f5ff;
}
#example_2 {
width: 200px;
height: 200px;
position: relative;
top: 0;
right: 0;
background-color: #c1ffdb;
}
#example_3 {
width: 600px;
height: 400px;
position: relative;
top: 20px;
left: 20px;
background-color: #ffd7c2;
}
#example_4 {
width: 200px;
height: 200px;
position: absolute;
bottom: 10px;
right: 20px;
background-color: #ffc7e4;
}
#example_5 {
position: absolute;
right: 0;
left: 0;
top: 100px;
background-color: #d7ffc2;
}
結果
仕様書
| Specification |
|---|
| CSS Positioned Layout Module Level 3> # insets> |
ブラウザーの互換性
関連情報
top,bottom,rightinset一括指定inset-block-start,inset-block-end,inset-inline-start,inset-inline-endinset-blockおよびinset-inline一括指定position- CSS 位置指定レイアウトモジュール