Skip to content

Instantly share code, notes, and snippets.

@usagi
Last active August 19, 2022 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usagi/d774894241257ef0b6faad02079fba39 to your computer and use it in GitHub Desktop.
Save usagi/d774894241257ef0b6faad02079fba39 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<style>
/* HTML要素の「文字」を対象に擬似的に縁取りする CSS の例 */
:root {
/* 輪郭線にしたい色 */
--shadow-color: #333;
/* 輪郭線にしたい幅(長さ) */
--shadow-thickness: 0.5mm;
/* 輪郭線にしたい幅の2倍の + 向きの長さ = + 向きのオフセット値 */
--shadow-offset-positive: calc(var(--shadow-thickness) * 2);
/* 輪郭線にしたい幅の2倍の - 向きの長さ = - 向きのオフセット値 */
--shadow-offset-negative: calc(var(--shadow-offset-positive) * -1);
/* 8方向に影を出して擬似的に輪郭線を縁取る text-shadow 用の設定 */
--shadow:
/* 影付けの向き①↘: +x, +y = 右下 */
var(--shadow-offset-positive) var(--shadow-offset-positive) var(--shadow-thickness) var(--shadow-color),
/* 影付けの向き②↙: -x, +y = 左下 */
var(--shadow-offset-negative) var(--shadow-offset-positive) var(--shadow-thickness) var(--shadow-color),
/* 影付けの向き③➚: +x, -y = 右上 */
var(--shadow-offset-positive) var(--shadow-offset-negative) var(--shadow-thickness) var(--shadow-color),
/* 影付けの向き④↖: -x, -y = 左上 */
var(--shadow-offset-negative) var(--shadow-offset-negative) var(--shadow-thickness) var(--shadow-color),
/* 影付けの向き⑤→: +x, 0 = 右 */
var(--shadow-offset-positive) 0 var(--shadow-thickness) var(--shadow-color),
/* 影付けの向き⑥↓: 0, +y = 下 */
0 var(--shadow-offset-positive) var(--shadow-thickness) var(--shadow-color),
/* 影付けの向き⑦←: -x, 0 = 左 */
var(--shadow-offset-negative) 0 var(--shadow-thickness) var(--shadow-color),
/* 影付けの向き⑧↑: 0, -y = 上 */
0 var(--shadow-offset-negative) var(--shadow-thickness) var(--shadow-color);
}
body * {
border: hsla(0, 50%, 50%, 0.5) solid 4mm;
}
body {
background-color: #888;
color: #fff;
font-size: 20mm;
}
#html-text {
background-color: yellow;
color: #fff;
text-shadow: var(--shadow);
}
img {
background-color: yellow;
width: 60mm;
}
.x2{
width: 120mm;
}
</style>
<!-- 例A: HTML の文字列要素 + CSS による擬似的な縁取り -->
<p id="html-text">"This 😊 is a happy symbol."</p>
<!-- 例B: HTML に img で読み込む SVG + SVG の内部で stroke で縁取り -->
<img id="with-stroke" src="with-stroke.svg" />
<!-- 例C: HTML に img で読み込む SVG + SVG の内部で shadow で縁取り -->
<img id="with-shadow" src="with-shadow.svg" />
<br/>
<img class="x2" id="with-stroke" src="with-stroke.svg" />
<img class="x2" id="with-stroke" src="with-shadow.svg" />
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment