0
1

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 1 year has passed since last update.

calendar-pickerのアイコンを先頭にしてカスタムのsvgを適用させたい

Last updated at Posted at 2023-12-27

背景

カレンダーを以下のような表示にしたいなと思った為です。
image.png

実装

今回は以下のアイコンを例にします。

input[type="date"]::-webkit-calendar-picker-indicator {
    background:url('data:image/svg+xml,%3Csvg xmlns="http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg" width="18" height="18" viewBox="0 0 24 24"%3E%3Cpath fill="%230098c2" d="M7 11h2v2H7zm0 4h2v2H7zm4-4h2v2h-2zm0 4h2v2h-2zm4-4h2v2h-2zm0 4h2v2h-2z"%2F%3E%3Cpath fill="%230098c2" d="M5 22h14c1.103 0 2-.897 2-2V6c0-1.103-.897-2-2-2h-2V2h-2v2H9V2H7v2H5c-1.103 0-2 .897-2 2v14c0 1.103.897 2 2 2M19 8l.001 12H5V8z"%2F%3E%3C%2Fsvg%3E')
}

以下のようになりました。
image.png

次にアイコンとテキストの位置を反転させます。

input[type="date"]::-webkit-calendar-picker-indicator {
    background:url('data:image/svg+xml,%3Csvg xmlns="http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg" width="18" height="18" viewBox="0 0 24 24"%3E%3Cpath fill="%230098c2" d="M7 11h2v2H7zm0 4h2v2H7zm4-4h2v2h-2zm0 4h2v2h-2zm4-4h2v2h-2zm0 4h2v2h-2z"%2F%3E%3Cpath fill="%230098c2" d="M5 22h14c1.103 0 2-.897 2-2V6c0-1.103-.897-2-2-2h-2V2h-2v2H9V2H7v2H5c-1.103 0-2 .897-2 2v14c0 1.103.897 2 2 2M19 8l.001 12H5V8z"%2F%3E%3C%2Fsvg%3E');
    position: absolute;
    left: 750px;
}

input[type="date"]::-webkit-datetime-edit {
    position: relative;
    left: 50px;
}

枠線も消します

input[type="date"] {
    border:none;
}

以下のようにイメージした日付入力フォームができました!
image.png

その他

shadowDOMをdevtoolで見れるようにする
devtool開く→歯車 → Elements → 「Show user agent shadow DOM」 をチェック
image.png

これでdevtool開きながら位置を調整しました。

参考

0
1
3

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?