Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
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.
npm install postcss-custom-media --save-dev
PostCSS Custom Media lets you define @custom-media
in CSS following the Custom Media Specification.
@custom-media --small-viewport (max-width: 30em);
@media (--small-viewport) {
/* styles for small viewport */
}
/* becomes */
@media (max-width: 30em) {
/* styles for small viewport */
}
true
and false
With @custom-media
you can use the constants true
and false
.
These are especially handy when debugging.
If you are unsure how your page is affected when a certain media query matches or not you can use these, to quickly toggle the results. This plugin downgrades these queries to something that works in all browsers.
Quickly check the result as if the query matches:
@custom-media --small-viewport true;
@media (--small-viewport) {
/* styles for small viewport */
}
/* becomes */
@media (max-color:2147477350) {
/* styles for small viewport */
}
Quickly check the result as if the query does not match:
@custom-media --small-viewport false;
@media (--small-viewport) {
/* styles for small viewport */
}
/* becomes */
@media (color:2147477350) {
/* styles for small viewport */
}
It is impossible to accurately and correctly resolve complex @custom-media
queries
as these depend on the browser the queries will eventually run in.
Some of these queries will have only one possible outcome but we have to account for all possible queries in this plugin.
[!NOTE] When handling complex media queries you will see that your CSS is doubled for each level of complexity.
GZIP works great to de-dupe this but having a lot of complex media queries will have a performance impact.
An example of a very complex (and artificial) use-case :
@custom-media --a-complex-query tty and (min-width: 300px);
@media not screen and ((not (--a-complex-query)) or (color)) {
/* Your CSS */
}
/* becomes */
@media tty and (min-width: 300px) {
@media not screen and ((not (max-color:2147477350)) or (color)) {
/* Your CSS */
}
}
@media not tty,not all and (min-width: 300px) {
@media not screen and ((not (color:2147477350)) or (color)) {
/* Your CSS */
}
}
Add PostCSS Custom Media to your project:
npm install postcss postcss-custom-media --save-dev
Use it as a PostCSS plugin:
const postcss = require('postcss');
const postcssCustomMedia = require('postcss-custom-media');
postcss([
postcssCustomMedia(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
The preserve
option determines whether the original notation
is preserved. By default, it is not preserved.
postcssCustomMedia({ preserve: true })
@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 */
}
If you're using Modular CSS such as, CSS Modules, postcss-loader
or vanilla-extract
to name a few, you'll probably
notice that custom media queries are not being resolved. This happens because each file is processed separately so
unless you import the custom media query definitions in each file, they won't be resolved.
To overcome this, we recommend using the PostCSS Global Data plugin which allows you to pass a list of files that will be globally available. The plugin won't inject any extra code in the output but will provide the context needed to resolve custom media queries.
For it to run it needs to be placed before the PostCSS Custom Media plugin.
const postcss = require('postcss');
const postcssCustomMedia = require('postcss-custom-media');
const postcssGlobalData = require('@csstools/postcss-global-data');
postcss([
postcssGlobalData({
files: [
'path/to/your/custom-media-queries.css'
]
}),
postcssCustomMedia(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);
FAQs
Use Custom Media Queries in CSS
The npm package postcss-custom-media receives a total of 4,733,231 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.