0
0

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.

pxをViewport Widthに変換するStylus関数

Last updated at Posted at 2022-07-30

pxをViewport Widthに変換するStylusの関数をつくった

//基準となるwidth(単位はpx)
baseWidth =

px-to-vw(px)
vw = 100 / baseWidth * px
unit(vw, 'vw') //'vw'という文字列を末尾につけるvwの数値だけを返したい場合は削除

Usage

1.基準となるwidth(単位はpx)を設定し、関数を記述

_setting.styl
baseWidth = 1400

px-to-vw(px)
vw = 100 / baseWidth_pc * px
unit(vw, 'vw')

2.変換したいpxを引数に与えて関数を呼び出す

style.styl
p
  font-size px-to-vw-pc(16)

3.Stylusをコンパイル。CSSにvwとして出力される

style.css
p {
  font-size: 1.142857142857143vw;
}

今後の課題

  • 負の値にも対応する
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?