Coder Social home page Coder Social logo

postcss-image-dimensions's Introduction

PostCSS Image Dimensions

PostCSS plugin for using image width, height, ratio and color data in your CSS.

Features

  • Image width and height
  • Image size ratio (width:height and height:width)
  • Average color
  • Caching

Note: All width, height and ratio helpers include a 2x resolution version. For example, image-width() becomes image-width-2x(). The assumption is that the 2x image is the original image found and the 1x image is half of that.

Usage

Install package as a dependency.

yarn add postcss-image-dimensions

Add to your PostCSS build chain.

const postcssImageDimensions = require('postcss-image-dimensions');

postcss([
	postcssImageDimensions({
		globPattern: ['./src/images/**/*', './other/images/**/*'], // String or Array of strings
		cachePath: './.cache'
	})
]);

Use available helper methods in your CSS.

/* Input */
.example-image {
    width: image-width('src/images/example.jpg');
}

/* Output */
.example-image {
    width: 200px;
}

CSS helper methods

Important: All image paths used with the CSS helpers must be absolute from the root of your project.

  • image-width() - Width in pixels.
  • image-width-2x() - Width in pixels.
  • image-width-ratio() - Ratio of width as a percentage (to 4 decimal places).
  • image-width-ratio-2x() - Ratio of width as a percentage (to 4 decimal places).
  • image-height() - Height in pixels.
  • image-height-2x() - Height in pixels.
  • image-height-ratio() - Ratio of height as a percentage (to 4 decimal places).
  • image-height-ratio-2x() - Ratio of height as a percentage (to 4 decimal places).
  • image-color() - The average color of the image as a hexadecimal string or transparent if image is transparent.

Examples

All examples below assume the following image has been used.

alt text

Example Input Example Output
image-width('test/juice.jpg') 125px
image-width-2x('test/juice.jpg') 250px
image-width-ratio('test/juice.jpg') 117.9245%
image-width-ratio-2x('test/juice.jpg') 118.4834%
image-height('test/juice.jpg') 106px
image-height-2x('test/juice.jpg') 211px
image-height-ratio('test/juice.jpg') 84.8%
image-height-ratio-2x('test/juice.jpg') 84.4%
image-color('test/juice.jpg') #cbbc8e

API

postcssImageDimensions([options])

options.globPattern

  • Type: String | Array
  • Default value: './src/images/**/*'

Override where the plugin should look for images. Paths need to be provided as glob patterns. See globby for supported patterns.

options.cachePath

  • Type: String
  • Default value: './.cache'

The plugin creates a cache of all image data so it doesn't have to keep processing images each time.

Todo

  • Write tests
  • Add more accurate average color

postcss-image-dimensions's People

Contributors

dobby89 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.