73
68

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CSSを駆使してDIV要素1つでデジタル数字を作る

Last updated at Posted at 2015-10-17

##概要

digits.png

こんな感じのデジタル数字(7セグメント)を、

<!DOCTYPE html>
<style>
  /* CSS */
</style>
<div class="display d4"></div>

こんな感じでひとつの要素だけで作ってみようと思います。

##方針

digit-explanation.png

こんな感じで分割してみようと思います。
どのセグメントを光らせるかはクラスで指定します。

##作成

とりあえず上記の方針に従い4を作ってみます。

.display {
  display: inline-block;
  box-sizing: border-box;
  position: relative;
  width: 60px;
  height: 100px;
  border: solid 4px #111;
  background-color: #111;
  overflow: hidden;
}

.display::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 28px;
  height: 28px;
  transform: translate(-50%, -50%) rotate(45deg);
}

.display::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 30px;
  height: 12px;
  border-top: solid 30px #111;
  border-bottom: solid 30px #111;
  transform: translate(-50%, -50%);
}

.d4::before {
  background-color: #ff0;
  box-shadow: -30px -30px #333,
                0px -30px #ff0,
              -30px   0px #ff0,
                0px  30px #333,
               30px   0px #ff0,
               30px  30px #333;
}

digit-4.png

はいできました!

##デモ

##所感

数値の調整に関してはまだ改善する余地があると思います。
とはいえなんとなく作れてしまったので、CSS恐ろしいです。

73
68
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
73
68

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?