![Internet Explorer](https://htmqcom1.wpengine.com/wp-content/uploads/2024/07/b_ie.gif)
![Google Chrome](https://htmqcom1.wpengine.com/wp-content/uploads/2024/07/b_gc.gif)
![Safari](https://htmqcom1.wpengine.com/wp-content/uploads/2024/07/b_s.gif)
![Firefox](https://htmqcom1.wpengine.com/wp-content/uploads/2024/07/b_f.gif)
![Opera](https://htmqcom1.wpengine.com/wp-content/uploads/2024/07/b_o.gif)
text-alignプロパティは、ブロックコンテナ内の行の揃え位置・均等割付を指定する際に使用します。
テキストブロックは、単一または複数行のラインボックスを積み重ねたものです。
下のサンプルでいうと、背景がピンク色の部分がテキストブロック、赤いボーダーで囲んだ部分がラインボックスです。
このサンプルには text-align:end; を指定しています。
らららら~りりりり~るるるる~れれれれ~ろろろろ~♪
text-alignプロパティは、各ラインボックスをコンテンツ(テキスト内容)が完全に満たしていない場合に、そのコンテンツがインライン軸に沿ってどのように整列するかを設定します。
上のサンプルでは、最後の行はテキスト内容がラインボックスを完全には満たしていないため、text-align:end; の指定によってコンテンツがラインボックスの終端に揃えられているのが確認できます。
text-alignプロパティは、
text-align-allプロパティ、および、
text-align-lastプロパティの値をまとめて指定するショートハンド(短縮形)です。
text-alignプロパティの値が justify-all 以外の場合、text-align-lastプロパティの値は auto に設定されます。
■値
- start
- インラインレベルのコンテンツは、ラインボックスの始端に揃えられる(初期値)
- end
- インラインレベルのコンテンツは、ラインボックスの終端に揃えられる
- left
- インラインレベルのコンテンツは、ラインボックスの左端に揃えられる(縦書きモードではtext-orientationプロパティで指定するテキストの方向次第で上端または下端のいずれかに揃えられる)
- right
- インラインレベルのコンテンツは、ラインボックスの右端に揃えられる(縦書きモードではtext-orientationプロパティで指定するテキストの方向次第で上端または下端のいずれかに揃えられる)
- center
- インラインレベルのコンテンツは、ラインボックスの中央に配置される
- justify
- 均等割付。最後の行についてはtext-align-lastプロパティの指定がない限り、ラインボックスの始端に揃えられる
- justify-all
- 均等割付。最後の行も強制的に均等割付となる
- match-parent
- 親要素の値を継承する。継承した値が start または end の場合は left または right と解釈される
■初期値・適用対象・値の継承
- 初期値
- start
- 適用対象
- ブロックコンテナ
- 値の継承
- する
■使用例
スタイルシート部分は外部ファイル(sample.css)に記述。
p.sample { width:100px; background-color:#99cc00; }
HTMLソース
<p class="sample" style="text-align:start;"> text-align:start; を指定<br> How inline content is distributed within a line box? </p> <p class="sample" style="text-align:end;"> text-align:end; を指定<br> How inline content is distributed within a line box? </p> <p class="sample" style="text-align:left;"> text-align:left; を指定<br> How inline content is distributed within a line box? </p> <p class="sample" style="text-align:right;"> text-align:right; を指定<br> How inline content is distributed within a line box? </p> <p class="sample" style="text-align:center;"> text-align:center; を指定<br> How inline content is distributed within a line box? </p> <p class="sample" style="text-align:justify;"> text-align:justify; を指定<br> How inline content is distributed within a line box? </p> <p class="sample" style="text-align:justify-all;"> text-align:justify-all; を指定<br> How inline content is distributed within a line box? </p> <p class="sample" style="text-align:match-parent;"> text-align:match-parent; を指定<br> How inline content is distributed within a line box? </p>
ブラウザ上の表示
text-align:start; を指定
How inline content is distributed within a line box?
text-align:end; を指定
How inline content is distributed within a line box?
text-align:left; を指定
How inline content is distributed within a line box?
text-align:right; を指定
How inline content is distributed within a line box?
text-align:center; を指定
How inline content is distributed within a line box?
text-align:justify; を指定
How inline content is distributed within a line box?
text-align:justify-all; を指定
How inline content is distributed within a line box?
text-align:match-parent; を指定
How inline content is distributed within a line box?
■関連項目
text-justify …… 均等割付の形式を指定する(IEが独自に採用)
vertical-align …… 縦方向の揃え位置を指定する
text-decoration …… テキストの下線・上線・打ち消し線・点滅を指定する
text-underline-position・・下線の表示位置を指定する(IE独自の仕様)
text-indent …… 一行目のインデント幅を指定する
text-transform …… テキストの大文字表示・小文字表示を指定する
white-space …… ソース中のスペース・タブ・改行の表示の仕方を指定する
letter-spacing …… 文字の間隔を指定する
word-spacing …… 単語の間隔を指定する
text-autospace …… アルファベット等との間隔を指定する(IEが独自に採用)
line-break …… 禁則処理のしかたを指定する(IEが独自に採用)
word-break …… 文の改行の仕方について指定する(IEが独自に採用)
word-wrap …… 単語の途中で改行するかどうかを指定する(IE独自の仕様)
text-shadow …… テキストに影をつける