このページの本文へ

前へ 1 2 3 次へ

菊池 崇の実践!CSS3道場 ― 第5回

CSS3を駆使してTwitter風のふきだしを作る

2011年03月24日 10時01分更新

菊池 崇

  • この記事をはてなブックマークに追加
  • 本文印刷

ふきだしのバリエーション

 サンプル1で作成したCSSを応用すると、さまざまなふきだしのデザインを作成できる。しっぽの位置を上・左に変更したのがサンプル2、3だ。

サンプル2

サンプル2[CSS]

.speech-bubble.top p:after {
top : -30px; /*三角形の上下位置*/
right : 50px; /*三角形の左右位置*/
border-width : 15px 15px 15px 15px;
border-color : transparent transparent #87e0fd transparent; }
サンプル3

サンプル3[CSS]

.speech-bubble.right p:after {
top : 10px; /*三角形の上下位置*/
left : -30px; /*三角形の左右位置*/
border-width : 15px 15px 15px 15px;
border-color : transparent #47c6ee transparent transparent; }

白抜きのふきだし

 さらにCSSに手を加えると「白抜き」のふきだしも作れる。以下のふきだしは、:before と :after を利用して2つの三角形を配置し、片方の三角形の色を白に変更することで白抜きに見せている。

三角形を描き…
もう1つの三角形を重ねて白抜きにする

サンプル4

.speech-bubble p:before {
content : "";
position : absolute;
bottom : -40px; /*三角形の上下位置*/
left : 50px; /*三角形の左右位置*/
right : auto;
width : 0;
height : 0;
border-width : 20px;
border-style : solid;
border-color : #ff6600 transparent transparent transparent; }
.speech-bubble p:after {
content : "";
position : absolute;
bottom : -34px; /*三角形の上下位置*/
left : 50px; /*三角形の左右位置*/
width : 0;
height : 0;
border-width : 20px;
border-style : solid;
border-color : #fff transparent transparent transparent; }

楕円形のバブル

 楕円形の「バブル」も「白抜き」を応用して作れるふきだしのテクニックだ。 border-radius プロパティを利用して楕円形のテキストボックスを作り(.speech-bubble p)、そのすぐ下に:before擬似要素を利用して角丸の三角形(右下の角にのみ大きくアールを付けたボックス)を配置する。

サンプル5[CSS]

.speech-bubble p {
position : relative;
padding : 30px 20px;
color : #fff;
width : 250px;
margin : 40px auto 0;
text-align : center;
/* css3 */
-webkit-border-radius : 200px/100px;
-moz-border-radius : 200px/100px;
border-radius : 200px/100px;
background : #87e0fd; /* old browsers */
background : -moz-linear-gradient(top, #87e0fd 0%, #53cbf1 40%, #05abe0 100%); /* firefox */
background : -webkit-gradient(linear, left top, left bottom, color-stop(0%,#87e0fd), color-stop(40%,#53cbf1), color-stop(100%,#05abe0)); /* webkit */
filter : progid:DXImageTransform.Microsoft.gradient( startColorstr='#87e0fd', endColorstr='#05abe0',GradientType=0 ); /* ie */}
/* 三角形部分 */
.speech-bubble p:before {
content: "";
position: absolute;
z-index: -1;
bottom: -30px;
right: 50%;
width: 0;
height: 30px;
border-right: 60px solid #05abe0;
background: #05abe0;
-moz-border-radius-bottomright: 80px 50px;
-webkit-border-bottom-right-radius: 80px 50px;
border-bottom-right-radius: 80px 50px;
-moz-transform: translate(0, -2px);
-webkit-transform: translate(0, -2px);
-o-transform: translate(0, -2px);
transform: translate(0, -2px); }
.speech-bubble p:after {
content: "";
position: absolute;
z-index: -1;
bottom: -30px;
right: 50%;
width: 60px;
height: 30px;
background: #fff;
-moz-border-radius-bottomright: 40px 50px;
-webkit-border-bottom-right-radius: 40px 50px;
border-bottom-right-radius: 40px 50px;
-moz-transform: translate(-30px, -2px);
-webkit-transform: translate(-30px, -2px);
-o-transform: translate(-30px, -2px);
transform: translate(-30px, -2px); }

 角丸の三角形の上に、もう1つ角丸の三角形を配置する(以下の黒のボックス)。

.speech-bubble p:after {
content: "";
position: absolute;
z-index: -1;
bottom: -30px;
right: 50%;
width: 60px;
height: 30px;
background: #000;
-moz-border-radius-bottomright: 40px 50px;
-webkit-border-bottom-right-radius: 40px 50px;
border-bottom-right-radius: 40px 50px;
-moz-transform: translate(-30px, -2px);
-webkit-transform: translate(-30px, -2px);
-o-transform: translate(-30px, -2px);
transform: translate(-30px, -2px); }

 2番目の角丸の三角形を白に変更したらバブルの完成だ。

background: #fff;

 いかがだろうか。今回紹介したふきだしのデザインテクニックは、いずれも:before と :after を活用することで、シンプルなマークアップを実現している。アイデア次第ではもっといろいろなデザインができるので、ぜひ挑戦してほしい。

参考:http://nicolasgallagher.com/pure-css-speech-bubbles/


著者:菊池 崇(きくち・たかし)

著者近影

Web Directions East LLC代表。allWebクリエイター塾講師。米国にてマーケティングを専攻修了。貿易会社を経て、allWebにてXHTML+CSS基礎講座、XHTML+CSS中級講座、XHTML+CSS上級講座、microformats講座などの講師。オーストラリア、米国、カナダなどで開催されている世界的に有名なカンファレンス「Web Directions」の日本版である「Web Directions East」の発起人。


前へ 1 2 3 次へ

カテゴリートップへ

この連載の記事

ASCII.jp会員サービス 週刊Web Professional登録

Webディレクター江口明日香が行く

みんなが買ってる最新アイテムはコレだ!

Microsoft Windows 7 Home Premium 通常版 Service Pack 1 適用済み

iPhone 4S/4 防指紋性・高光沢機能性フィルム PRO GUARD AF for iPhone 4S/4 / PGAF-IPH4

iPhone 4S/4 防指紋性・高光沢機能性フィルム PRO GUARD AF for iPhone 4S/4 / PGAF-IPH4

マイクロソリューション Micro Solution Inc.

83人が購入

BenQ 24型 LCDワイドモニタ XL2420T

BenQ 24型 LCDワイドモニタ XL2420T

ベンキュージャパン

37,238円〜

3人が購入

jQuery Mobile スマートフォンサイト デザイン入門 (WEB PROFESSIONAL)

jQuery Mobile スマートフォンサイト デザイン入門 (WEB PROFESSIONAL)

アスキー・メディアワークス

2,499円〜

20人が購入

メモリーカード 32GB (PCH-Z321J)

メモリーカード 32GB (PCH-Z321J)

ソニー・コンピュータエンタテインメント

7,772円〜

6人が購入

Amazon.co.jp