HTML
CSS
CSS3

CSS影付き吹き出し位置12パターン

More than 1 year has passed since last update.

影付き吹き出しの作り方を毎回調べている気がするのでメモっておく。

スクリーンショット 2017-02-06 4.22.58.png


内側にも要素を設ける方法


概要

一番無難な方法です。特に事情が無ければこれを選択してください。


  • サイズゼロ要素のborderを太くする方法だと影つきのものに対応できないので,transformrotate()skew()を活用する方法を使います。


  • z-indexを使うことで,前後関係を定義しています。

  • ひし形の内側部分がテキストと被らないように,四角形の疑似要素を上から被せています。テキストはさらにその上に来るようにします。


長所


  • 位置の微調整をしやすい

  • 倍率によらず表示が安定している

  • 基本的にアニメーションを併用できる


短所


  • HTMLがやや冗長


実装


ブラウザのアドレスバーにコピペして確認

data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+Cgo8bWV0YSBjaGFyc2V0PSJVVEYtOCI+Cjx0aXRsZT5DU1PlvbHku5jjgY3lkLnjgY3lh7rjgZfkvY3nva4xMuODkeOCv+ODvOODszwvdGl0bGU+Cgo8c3R5bGU+CgovKiDooajnpLrnlKggKi8KaHRtbCxib2R5IHsKICBoZWlnaHQ6IDEwMCU7CiAgZGlzcGxheTogZmxleDsKICBmbGV4LWRpcmVjdGlvbjogY29sdW1uOwogIGp1c3RpZnktY29udGVudDogY2VudGVyOwogIGFsaWduLWl0ZW1zOiBjZW50ZXI7Cn0KLnJvdyB7CiAgZGlzcGxheTogZmxleDsKfQoKLyog5ZC544GN5Ye644GX44O75YWo5YWx6YCaICovCi5iYWxsb29uIHsKICBwb3NpdGlvbjogcmVsYXRpdmU7CiAgd2lkdGg6IDIwMHB4OyAvKiDluYUgKi8KICBtaW4taGVpZ2h0OiA2MHB4OyAvKiDmnIDlsI/jga7pq5jjgZUgKi8KICBtYXJnaW46IDIwcHg7IC8qIOWkluWBtOOBruS9meeZvSAqLwogIHBhZGRpbmc6IDEwcHg7IC8qIOWGheWBtOOBruS9meeZvSAqLwp9Ci5iYWxsb29uOjpiZWZvcmUgewogIGNvbnRlbnQ6ICc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Y+PC9kaXY+CjwvZGl2PgoKPGRpdiBjbGFzcz0icm93Ij4KICA8ZGl2IGNsYXNzPSJiYWxsb29uIGJhbGxvb24tLWJvdHRvbV9sZWZ0Ij48ZGl2PuS4i+i+uuW3puWBtDwvZGl2PjwvZGl2PgogIDxkaXYgY2xhc3M9ImJhbGxvb24gYmFsbG9vbi0tYm90dG9tX2NlbnRlciI+PGRpdj7kuIvovrrkuK3lpK48L2Rpdj48L2Rpdj4KICA8ZGl2IGNsYXNzPSJiYWxsb29uIGJhbGxvb24tLWJvdHRvbV9yaWdodCI+PGRpdj7kuIvovrrlj7PlgbQ8L2Rpdj48L2Rpdj4KPC9kaXY+Cgo8ZGl2IGNsYXNzPSJyb3ciPgogIDxkaXYgY2xhc3M9ImJhbGxvb24gYmFsbG9vbi0tbGVmdF90b3AiPjxkaXY+5bem6L665LiK5YG0PC9kaXY+PC9kaXY+CiAgPGRpdiBjbGFzcz0iYmFsbG9vbiBiYWxsb29uLS1sZWZ0X21pZGRsZSI+PGRpdj7lt6bovrrkuK3lpK48L2Rpdj48L2Rpdj4KICA8ZGl2IGNsYXNzPSJiYWxsb29uIGJhbGxvb24tLWxlZnRfYm90dG9tIj48ZGl2PuW3pui+uuS4i+WBtDwvZGl2PjwvZGl2Pgo8L2Rpdj4



ソースコード

<!DOCTYPE html>

<meta charset="UTF-8">
<title>CSS影付き吹き出し位置12パターン</title>

<style>

/* 表示用 */
html,body {
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.row {
display: flex;
}

/* 吹き出し・全共通 */
.balloon {
position: relative;
width: 200px; /* 幅 */
min-height: 60px; /* 最小の高さ */
margin: 20px; /* 外側の余白 */
padding: 10px; /* 内側の余白 */
}
.balloon::before {
content: '';
position: absolute;
z-index: 1;
width: 20px; height: 20px; /* 吹き出しサイズ */
}
.balloon::after {
content: '';
position: absolute;
z-index: 2;
top: 0; left: 0;
width: 100%; height: 100%;
}
.balloon>* {
position: relative;
z-index: 3;
}
.balloon,
.balloon::after {
border-radius: 5px; /* 角の丸め方 */
}
.balloon,
.balloon::before {
box-shadow: 0 0 10px 0 rgba(163,163,163,0.50); /* ぼかし距離(第3引数),拡散距離(第4引数),色(第5引数) */
}
.balloon,
.balloon::before,
.balloon::after {
background: #fffed6; /* 背景色 */
}

/* 吹き出し・上辺左側 */
.balloon--top_left::before {
top: -10px; left: 10px; /* 位置 */
transform: rotate(45deg) skew(20deg,20deg); /* 傾斜角(skew) */
}
/* 吹き出し・上辺中央 */
.balloon--top_center::before {
top: -10px; left: 50%; /* 位置 */
transform: translateX(-50%) rotate(45deg) skew(20deg,20deg); /* 傾斜角(skew) */
}
/* 吹き出し・上辺右側 */
.balloon--top_right::before {
top: -10px; right: 10px; /* 位置 */
transform: rotate(45deg) skew(20deg,20deg); /* 傾斜角(skew) */
}

/* 吹き出し・右辺上側 */
.balloon--right_top::before {
right: -10px; top: 10px; /* 位置 */
transform: rotate(45deg) skew(-20deg,-20deg); /* 傾斜角(skew) */
}
/* 吹き出し・右辺中央 */
.balloon--right_middle::before {
right: -10px; top: 50%; /* 位置 */
transform: translateY(-50%) rotate(45deg) skew(-20deg,-20deg); /* 傾斜角(skew) */
}
/* 吹き出し・右辺下側 */
.balloon--right_bottom::before {
right: -10px; bottom: 10px; /* 位置 */
transform: rotate(45deg) skew(-20deg,-20deg); /* 傾斜角(skew) */
}

/* 吹き出し・下辺左側 */
.balloon--bottom_left::before {
bottom: -10px; left: 10px; /* 位置 */
transform: rotate(45deg) skew(20deg,20deg); /* 傾斜角(skew) */
}
/* 吹き出し・下辺中央 */
.balloon--bottom_center::before {
bottom: -10px; left: 50%; /* 位置 */
transform: translateX(-50%) rotate(45deg) skew(20deg,20deg); /* 傾斜角(skew) */
}
/* 吹き出し・下辺右側 */
.balloon--bottom_right::before {
bottom: -10px; right: 10px; /* 位置 */
transform: rotate(45deg) skew(20deg,20deg); /* 傾斜角(skew) */
}

/* 吹き出し・左辺上側 */
.balloon--left_top::before {
left: -10px; top: 10px; /* 位置 */
transform: rotate(45deg) skew(-20deg,-20deg); /* 傾斜角(skew) */
}
/* 吹き出し・左辺中央 */
.balloon--left_middle::before {
left: -10px; top: 50%; /* 位置 */
transform: translateY(-50%) rotate(45deg) skew(-20deg,-20deg); /* 傾斜角(skew) */
}
/* 吹き出し・左辺下側 */
.balloon--left_bottom::before {
left: -10px; bottom: 10px; /* 位置 */
transform: rotate(45deg) skew(-20deg,-20deg); /* 傾斜角(skew) */
}

</style>

<div class="row">
<div class="balloon balloon--top_left"><div>上辺左側</div></div>
<div class="balloon balloon--top_center"><div>上辺中央</div></div>
<div class="balloon balloon--top_right"><div>上辺右側</div></div>
</div>

<div class="row">
<div class="balloon balloon--right_top"><div>右辺上側</div></div>
<div class="balloon balloon--right_middle"><div>右辺中央</div></div>
<div class="balloon balloon--right_bottom"><div>右辺下側</div></div>
</div>

<div class="row">
<div class="balloon balloon--bottom_left"><div>下辺左側</div></div>
<div class="balloon balloon--bottom_center"><div>下辺中央</div></div>
<div class="balloon balloon--bottom_right"><div>下辺右側</div></div>
</div>

<div class="row">
<div class="balloon balloon--left_top"><div>左辺上側</div></div>
<div class="balloon balloon--left_middle"><div>左辺中央</div></div>
<div class="balloon balloon--left_bottom"><div>左辺下側</div></div>
</div>



div要素1個だけで作る方法


概要

CSS職人の @junya さんにご紹介していただいた方法です。アニメーションを使わない,静的な表示しか行わないという前提なら,これを選択してもいいかもしれません。



  • translateZを使うことで,前後関係を定義しています。

  • ひし形の内側部分がテキストと被らないように,グラデーションで透明色にして,実質的な三角形を描画しています。


長所


  • HTMLが美しい


短所


  • Webkitの仕様(?)で,表示倍率によっては分断する線が見えてしまう

  • IE10+以外ではアニメーションを併用した実装が内容によっては不可能になる


実装


ブラウザのアドレスバーにコピペして確認

data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+Cgo8bWV0YSBjaGFyc2V0PSJVVEYtOCI+Cjx0aXRsZT5DU1PlvbHku5jjgY3lkLnjgY3lh7rjgZfkvY3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G9tIj7lj7PovrrkuIvlgbQ8L2Rpdj4KPC9kaXY+Cgo8ZGl2IGNsYXNzPSJyb3ciPgogIDxkaXYgY2xhc3M9ImJhbGxvb24gYmFsbG9vbi0tYm90dG9tX2xlZnQiPuS4i+i+uuW3puWBtDwvZGl2PgogIDxkaXYgY2xhc3M9ImJhbGxvb24gYmFsbG9vbi0tYm90dG9tX2NlbnRlciI+5LiL6L665Lit5aSuPC9kaXY+CiAgPGRpdiBjbGFzcz0iYmFsbG9vbiBiYWxsb29uLS1ib3R0b21fcmlnaHQiPuS4i+i+uuWPs+WBtDwvZGl2Pgo8L2Rpdj4KCjxkaXYgY2xhc3M9InJvdyI+CiAgPGRpdiBjbGFzcz0iYmFsbG9vbiBiYWxsb29uLS1sZWZ0X3RvcCI+5bem6L665LiK5YG0PC9kaXY+CiAgPGRpdiBjbGFzcz0iYmFsbG9vbiBiYWxsb29uLS1sZWZ0X21pZGRsZSI+5bem6L665Lit5aSuPC9kaXY+CiAgPGRpdiBjbGFzcz0iYmFsbG9vbiBiYWxsb29uLS1sZWZ0X2JvdHRvbSI+5bem6L665LiL5YG0PC9kaXY+CjwvZGl2Pg



ソースコード

<!DOCTYPE html>

<meta charset="UTF-8">
<title>CSS影付き吹き出し位置12パターン</title>

<style>

/* 表示用 */
html,body {
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.row {
display: flex;
}

/* 吹き出し・全共通 */
.balloon {
position: relative;
transform-style: preserve-3d;
width: 200px; /* 幅 */
min-height: 60px; /* 最小の高さ */
margin: 20px; /* 外側の余白 */
padding: 10px; /* 内側の余白 */
border-radius: 5px; /* 角の丸め方 */
}
.balloon::before,
.balloon::after {
content: '';
position: absolute;
width: 20px; height: 20px; /* 吹き出しサイズ */
}
.balloon,
.balloon::before {
background: #fffed6; /* 背景色(linear-gradientもこの色と統一する必要がある)*/
box-shadow: 0 0 10px 0 rgba(163,163,163,0.50); /* ぼかし距離(第3引数),拡散距離(第4引数),色(第5引数) */
}

/* 吹き出し・上辺共通 */
.balloon--top_left::after,
.balloon--top_center::after,
.balloon--top_right::after {
background: linear-gradient(135deg, #fffed6 51%, transparent 51%); /* 第2引数にbackgroundと共通の背景色を指定 */
}
/* 吹き出し・右辺共通 */
.balloon--right_top::after,
.balloon--right_middle::after,
.balloon--right_bottom::after {
background: linear-gradient(-135deg, #fffed6 51%, transparent 51%); /* 第2引数にbackgroundと共通の背景色を指定 */
}
/* 吹き出し・下辺共通 */
.balloon--bottom_left::after,
.balloon--bottom_center::after,
.balloon--bottom_right::after {
background: linear-gradient(-45deg, #fffed6 51%, transparent 51%); /* 第2引数にbackgroundと共通の背景色を指定 */
}
/* 吹き出し・左辺共通 */
.balloon--left_top::after,
.balloon--left_middle::after,
.balloon--left_bottom::after {
background: linear-gradient(45deg, #fffed6 51%, transparent 51%); /* 第2引数にbackgroundと共通の背景色を指定 */
}

/* 吹き出し・上辺左側 */
.balloon--top_left::before,.balloon--top_left::after {
top: -10px; left: 10px; /* 位置 */
transform: rotate(45deg) skew(20deg,20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--top_left::after {
transform: rotate(45deg) skew(20deg,20deg) translateZ(1px); /* 傾斜角(skew) */
}

/* 吹き出し・上辺中央 */
.balloon--top_center::before,.balloon--top_center::after {
top: -10px; left: 50%; /* 位置 */
transform: translateX(-50%) rotate(45deg) skew(20deg,20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--top_center::after {
transform: translateX(-50%) rotate(45deg) skew(20deg,20deg) translateZ(1px); /* 傾斜角(skew) */
}

/* 吹き出し・上辺右側 */
.balloon--top_right::before,.balloon--top_right::after {
top: -10px; right: 10px; /* 位置 */
transform: rotate(45deg) skew(20deg,20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--top_right::after {
transform: rotate(45deg) skew(20deg,20deg) translateZ(1px); /* 傾斜角(skew) */
}

/* 吹き出し・右辺上側 */
.balloon--right_top::before,.balloon--right_top::after {
right: -10px; top: 10px; /* 位置 */
transform: rotate(45deg) skew(-20deg,-20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--right_top::after {
transform: rotate(45deg) skew(-20deg,-20deg) translateZ(1px); /* 傾斜角(skew) */
}

/* 吹き出し・右辺中央 */
.balloon--right_middle::before,.balloon--right_middle::after {
right: -10px; top: 50%; /* 位置 */
transform: translateY(-50%) rotate(45deg) skew(-20deg,-20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--right_middle::after {
transform: translateY(-50%) rotate(45deg) skew(-20deg,-20deg) translateZ(1px); /* 傾斜角(skew) */
}

/* 吹き出し・右辺下側 */
.balloon--right_bottom::before,.balloon--right_bottom::after {
right: -10px; bottom: 10px; /* 位置 */
transform: rotate(45deg) skew(-20deg,-20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--right_bottom::after {
transform: rotate(45deg) skew(-20deg,-20deg) translateZ(1px); /* 傾斜角(skew) */
}

/* 吹き出し・下辺左側 */
.balloon--bottom_left::before,.balloon--bottom_left::after {
bottom: -10px; left: 10px; /* 位置 */
transform: rotate(45deg) skew(20deg,20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--bottom_left::after {
transform: rotate(45deg) skew(20deg,20deg) translateZ(1px); /* 傾斜角(skew) */
}

/* 吹き出し・下辺中央 */
.balloon--bottom_center::before,.balloon--bottom_center::after {
bottom: -10px; left: 50%; /* 位置 */
transform: translateX(-50%) rotate(45deg) skew(20deg,20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--bottom_center::after {
transform: translateX(-50%) rotate(45deg) skew(20deg,20deg) translateZ(1px); /* 傾斜角(skew) */
}

/* 吹き出し・下辺右側 */
.balloon--bottom_right::before,.balloon--bottom_right::after {
bottom: -10px; right: 10px; /* 位置 */
transform: rotate(45deg) skew(20deg,20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--bottom_right::after {
transform: rotate(45deg) skew(20deg,20deg) translateZ(1px); /* 傾斜角(skew) */
}

/* 吹き出し・左辺上側 */
.balloon--left_top::before,.balloon--left_top::after {
left: -10px; top: 10px; /* 位置 */
transform: rotate(45deg) skew(-20deg,-20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--left_top::after {
transform: rotate(45deg) skew(-20deg,-20deg) translateZ(1px); /* 傾斜角(skew) */
}

/* 吹き出し・左辺中央 */
.balloon--left_middle::before,.balloon--left_middle::after {
left: -10px; top: 50%; /* 位置 */
transform: translateY(-50%) rotate(45deg) skew(-20deg,-20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--left_middle::after {
transform: translateY(-50%) rotate(45deg) skew(-20deg,-20deg) translateZ(1px); /* 傾斜角(skew) */
}

/* 吹き出し・左辺下側 */
.balloon--left_bottom::before,.balloon--left_bottom::after {
left: -10px; bottom: 10px; /* 位置 */
transform: rotate(45deg) skew(-20deg,-20deg) translateZ(-1px); /* 傾斜角(skew) */
}
.balloon--left_bottom::after {
transform: rotate(45deg) skew(-20deg,-20deg) translateZ(1px); /* 傾斜角(skew) */
}

</style>

<div class="row">
<div class="balloon balloon--top_left">上辺左側</div>
<div class="balloon balloon--top_center">上辺中央</div>
<div class="balloon balloon--top_right">上辺右側</div>
</div>

<div class="row">
<div class="balloon balloon--right_top">右辺上側</div>
<div class="balloon balloon--right_middle">右辺中央</div>
<div class="balloon balloon--right_bottom">右辺下側</div>
</div>

<div class="row">
<div class="balloon balloon--bottom_left">下辺左側</div>
<div class="balloon balloon--bottom_center">下辺中央</div>
<div class="balloon balloon--bottom_right">下辺右側</div>
</div>

<div class="row">
<div class="balloon balloon--left_top">左辺上側</div>
<div class="balloon balloon--left_middle">左辺中央</div>
<div class="balloon balloon--left_bottom">左辺下側</div>
</div>



番外編: 吹き出し部分を「▲」の文字で表現する

高度な微調整が必要であり,傾斜角も固定なので実用性は薄めですが,


  • どうしても<div>1個で実現したい

  • どうしてもopacityを変化させるアニメーションを使いたい

という縛りつきで @junya さんによって生み出されたトリッキーな方法です。

(コメント欄参照)