Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
postcss-custom-media
Advanced tools
The postcss-custom-media package allows you to define custom media queries in CSS, which can then be reused throughout your stylesheets. This helps in maintaining a consistent and DRY (Don't Repeat Yourself) approach to media queries.
Define Custom Media Queries
This feature allows you to define custom media queries using the @custom-media rule. In this example, a custom media query named --small-viewport is defined for viewports with a maximum width of 30em.
@custom-media --small-viewport (max-width: 30em);
Use Custom Media Queries
Once a custom media query is defined, it can be used in your CSS just like any other media query. In this example, the custom media query --small-viewport is used to change the text color to red for small viewports.
body { color: black; }
@media (--small-viewport) { body { color: red; } }
postcss-preset-env allows you to use future CSS features today by converting modern CSS into something most browsers can understand. It includes a variety of plugins, including the ability to use custom media queries. Compared to postcss-custom-media, postcss-preset-env offers a broader range of features but may be overkill if you only need custom media queries.
postcss-media-minmax lets you write simpler and more concise media queries by using the < and > operators. While it doesn't offer custom media queries, it simplifies the syntax of media queries, making them easier to read and write. It is a good alternative if you are looking for a way to simplify your media queries without defining custom ones.
PostCSS Custom Media lets you use Custom Media Queries in CSS, following the CSS Media Queries specification.
@custom-media --small-viewport (max-width: 30em);
@media (--small-viewport) {
/* styles for small viewport */
}
/* becomes */
@media (max-width: 30em) {
/* styles for small viewport */
}
Add PostCSS Custom Media to your project:
npm install postcss-custom-media --save-dev
Use PostCSS Custom Media to process your CSS:
const postcssCustomMedia = require('postcss-custom-media');
postcssCustomMedia.process(YOUR_CSS /*, processOptions, pluginOptions */);
Or use it as a PostCSS plugin:
const postcss = require('postcss');
const postcssCustomMedia = require('postcss-custom-media');
postcss([
postcssCustomMedia(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
PostCSS Custom Media runs in all Node environments, with special instructions for:
Node | PostCSS CLI | Webpack | Create React App | Gulp | Grunt |
---|
The preserve
option determines whether custom media and atrules using custom
media should be preserved in their original form.
@custom-media --small-viewport (max-width: 30em);
@media (--small-viewport) {
/* styles for small viewport */
}
/* becomes */
@custom-media --small-viewport (max-width: 30em);
@media (max-width: 30em) {
/* styles for small viewport */
}
@media (--small-viewport) {
/* styles for small viewport */
}
The importFrom
option specifies sources where custom media can be imported
from, which might be CSS, JS, and JSON files, functions, and directly passed
objects.
postcssCustomMedia({
importFrom: 'path/to/file.css' // => @custom-selector --small-viewport (max-width: 30em);
});
@media (max-width: 30em) {
/* styles for small viewport */
}
@media (--small-viewport) {
/* styles for small viewport */
}
Multiple sources can be passed into this option, and they will be parsed in the
order they are received. JavaScript files, JSON files, functions, and objects
will need to namespace custom media using the customProperties
or
custom-properties
key.
postcssCustomMedia({
importFrom: [
'path/to/file.css',
'and/then/this.js',
'and/then/that.json',
{
customMedia: { '--small-viewport': '(max-width: 30em)' }
},
() => {
const customProperties = { '--small-viewport': '(max-width: 30em)' };
return { customProperties };
}
]
});
The exportTo
option specifies destinations where custom media can be exported
to, which might be CSS, JS, and JSON files, functions, and directly passed
objects.
postcssCustomMedia({
exportTo: 'path/to/file.css' // @custom-media --small-viewport (max-width: 30em);
});
Multiple destinations can be passed into this option, and they will be parsed
in the order they are received. JavaScript files, JSON files, and objects will
need to namespace custom media using the customProperties
or
custom-properties
key.
const cachedObject = { customMedia: {} };
postcssCustomMedia({
exportTo: [
'path/to/file.css', // @custom-media --small-viewport (max-width: 30em);
'and/then/this.js', // module.exports = { customMedia: { '--small-viewport': '(max-width: 30em)' } }
'and/then/this.mjs', // export const customMedia = { '--small-viewport': '(max-width: 30em)' } }
'and/then/that.json', // { "custom-media": { "--small-viewport": "(max-width: 30em)" } }
cachedObject,
customProperties => {
customProperties // { '--small-viewport': '(max-width: 30em)' }
}
]
});
FAQs
Use Custom Media Queries in CSS
The npm package postcss-custom-media receives a total of 1,082,084 weekly downloads. As such, postcss-custom-media popularity was classified as popular.
We found that postcss-custom-media demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.