Skip to content

Instantly share code, notes, and snippets.

@shirokuro331
Created December 20, 2012 13:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirokuro331/4345182 to your computer and use it in GitHub Desktop.
Save shirokuro331/4345182 to your computer and use it in GitHub Desktop.
画像の幅と高さを取得する

画像の幅と高さを取得する

background プロパティで背景画像を指定した時、width と height の値が欲しい時もある。
そういうときに、いちいち画像のサイズを調べるのは面倒なので、下記のようにする。

background: url("../image/common/hoge.png") no-repeat 0 0;
width: image-width("../image/common/hoge.png");
height: image-height("../image/common/pagetop.png");

image-width, image-height で、自動的に画像のサイズを取得してくれる。
後々画像のサイズが変更になってもこれでいちいち css ファイルの変更をしなくてもよい。

ちなみに演算もできるので、

height: image-height("../image/common/pagetop.png") /2;

って書けば2分の1の値を取得してくれる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment