【CSSリファレンス】text-align - web design lab
にほんブログ村 デザインブログ Webデザインへ PVアクセスランキング にほんブログ村

【CSSリファレンス】text-align


【CSSリファレンス】text-align

こんにちは(・∀・)

CSSプロパティをご紹介します。今回ご紹介するCSSプロパティはtext-alignです。

概要

text-alignプロパティを使用して行揃えの位置・均等割付(ブロックレベル要素)の指定を行います。


.sample {
  text-align: left;
}
適用要素

ブロックレベル要素

指定できる値
left
左寄せで表示
center
中央で表示
right
右寄せで表示
justify
両端揃えで表示
初期値

left

1. サンプル

テキストにtext-alignを指定します。

HTML

<p class="demo1">左寄せ</p>
<p class="demo2">中央</p>
<p class="demo3">右寄せ</p>
<p class="demo4">The waiting's almost over for those of you eager to strap an Apple wearable to your wrist, with pre-orders for the Apple Watch starting on April 10 and the device available to buy from ...</p>
CSS

.demo1 {
  text-align: left;
}
.demo2 {
  text-align: center;
}
.demo3 {
  text-align: right;
}
.demo4 {
  width: 200px;
  text-align: justify;
}
Result

左寄せ

中央

右寄せ

The waiting's almost over for those of you eager to strap an Apple wearable to your wrist, with pre-orders for the Apple Watch starting on April 10 and the device available to buy from ...


2. サンプル

罫線にtext-alignを指定します。

指定方法

ブラウザによってはtext-alignが効かないためmarginを使う場合もあります。

text-align: left;
左に表示 IE Opera
text-align: right;
右に表示 IE Opera
margin-left: 0;
左に表示 Firefox Safari Crome
margin-right: 0;
右に表示 Firefox Safari Crome
HTML

<p class="demo2"><hr></p>
CSS

hr {
  width: 300px;
  height: 2px;
  text-align: center;
  margin-right: auto;
  margin-left: auto;
}
Result



3. サンプル

テーブルにtext-alignとvertical-alignを指定します。

HTML

<div class="demo3">
<table>
<caption>bottom</caption>
<tr>
<td>サンプルデモ</td>
<td>サンプルデモ</td>
</tr>
</div>
</table>
CSS

.demo3 table {
  border: 2px solid #9fb7d4;
  border-collapse: separate;
}
.demo3 table td {
  border: 2px solid #d49fc2;
  text-align: right;
  vertical-align: bottom;
}
Result
サンプルデモ サンプルデモ

にほんブログ村 デザインブログ Webデザインへ PVアクセスランキング にほんブログ村