Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

hover-media-query

scriptex485MIT1.2.3

A progressively enhanced "hover" media query.

CSS Hover, SCSS Mixin, Hover Media Query, PostCSS Custom Media, CSS Hover Media Feature

readme

Github Build Codacy Badge Codebeat Badge CodeFactor Badge DeepScan grade Analytics

CSS Hover Media Query

A progressively enhanced "hover" media query.

Visitor stats

GitHub stars GitHub forks GitHub watchers GitHub followers

Code stats

GitHub code size in bytes GitHub repo size GitHub language count GitHub top language GitHub last commit

About

Detailed info about the Hover CSS media feature can be found on the MDN website.

This CSS media feature is implemented and supported in almost all modern browsers and works as expected.

The modern browsers include Chrome, Opera, Safari, Edge, Brave, Vivaldi, etc.

Internet Explorer and Firefox (prior v.64) do not understand this media feature and therefore will simply ignore all rules inside the query.

Details

The Hover Media Query module provides means to progressively enhance the CSS hover state by providing a unified set of media queries which target all browsers:

  • those which support the hover media query and:

    • support hover
    • do not support hover
  • those which do not support the hover media query at all

Examples

The following example shows how to use the Hover Media Query and target all browsers which support the hover CSS media query.

The following example targets also browsers which do not support it (such as IE and Firefox prior v.64).

/**
 * Enable hover states on devices which support hover media feature.
 * On IE10, IE11 and Firefox prior v.64 hover states work on any device.
 *
 * -ms-high-contrast: none      Targets IE10 and IE11
 * -ms-high-contrast: active    Targets IE10 and IE11
 * -moz-touch-enabled: 0        Targets Firefox before 64
 * hover: hover                 Targets all browsers which support the Hover CSS Media Feature
 */
@media (-ms-high-contrast: none), (-ms-high-contrast: active), (-moz-touch-enabled: 0), (hover: hover) {
    .element:hover {
        color: lavender;
    }
}

The following example shows how to use the Hover Media Query and target all browsers which support the hover media query but do not support the CSS hover state (such as mobile browsers which are usually used without a pointer device).

/**
 * For devices which support the "hover" media query but do not support ":hover" state
 * the following media query disables the highlight color on tap on the element and
 * adds styles to the ":active" state
 */
@media (hover: none) {
    .element {
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    }

    .element:active {
        color: lavender;
    }
}

Bonus 1: SCSS mixin

If your environment supports the SCSS language, you can use the SCSS provided here:

First define the SCSS mixin:

@mixin hover {
    @media (hover: none) {
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0);

        &:active {
            @content;
        }
    }

    @media (-ms-high-contrast: none), (-ms-high-contrast: active), (-moz-touch-enabled: 0), (hover: hover) {
        &:hover {
            @content;
        }
    }
}

Or import it:

@import 'hover-media-query/hover.scss';

And then use it wherever appropriate:

.button {
    @include hover {
        color: lavender;
    }
}

Bonus 2: PostCSS custom media queries

If your environment is configured to use the PostCSS postprocessor, you can use the custom PostCSS media queries. In order to to so you need to install and configure the PostCSS Custom Media plugin

First define the custom media queries:

@custom-media --hover (-ms-high-contrast: none), (-ms-high-contrast: active), (-moz-touch-enabled: 0), (hover: hover);
@custom-media --no-hover (hover: none);

Or import them:

@import 'hover-media-query/hover.css';

And then use them wherever appropriate:

@media (--hover) {
    .button:hover {
        color: lavender;
    }
}

@media (--no-hover) {
    .button {
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    }

    .button:active {
        color: rebeccapurple;
    }
}

LICENSE

MIT


Connect with me:

                     

Support and sponsor my work:

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

1.2.3

14 August 2023

  • Bump word-wrap from 1.2.3 to 1.2.4 #91
  • Update dependency postcss-custom-media to v10 #88
  • Bump semver from 7.3.8 to 7.5.3 #90
  • Update dependency release-it to v16 #89
  • Bump vm2 from 3.9.17 to 3.9.18 #86
  • Bump yaml from 2.1.3 to 2.2.2 #84
  • Bump vm2 from 3.9.16 to 3.9.17 #83
  • Bump vm2 from 3.9.15 to 3.9.16 #82
  • Bump vm2 from 3.9.13 to 3.9.15 #81
  • Bump cacheable-request from 10.2.3 to 10.2.7 #80
  • Bump http-cache-semantics from 4.1.0 to 4.1.1 #79
  • Update peer dependencies 4a4c8d9
  • Update dependency release-it to v15.10.5 ec4043e
  • Update dependency release-it to v15.9.1 907a415

1.2.2

16 December 2022

  • Update dependency postcss-custom-media to v9 #78
  • Fix peer dependencies range 6a67a8d
  • Update dependency release-it to v15.5.1 55fa8b2
  • Update dependency postcss-cli to v10.1.0 25d08e6

1.2.1

7 November 2022

  • Fix the URL to the documentation 8315aa8
  • Update dependency release-it to v15.4.3 86a13be
  • Update dependency release-it to v15.4.2 949d628

1.2.0

12 September 2022

  • Bump parse-url from 6.0.0 to 6.0.2 #73
  • Update dependency postcss-cli to v10 #72
  • Update dependency release-it to v15 #65
  • Bump minimist from 1.2.5 to 1.2.6 #64
  • Update actions/checkout action to v3 #61
  • Update actions/setup-node action to v3 #60
  • Update metadata 9b5975b
  • Update dependency release-it to v15.1.1 f724b45
  • Update dependency release-it to v14.14.3 2891361

1.1.1

25 January 2022

  • Bump node-fetch from 2.6.5 to 2.6.7 #58
  • Bump nanoid from 3.1.30 to 3.2.0 #57
  • Update dependency sass to v1.49.0 #56
  • Update dependency sass to v1.48.0 #54
  • Update dependency release-it to v14.12.1 #53
  • Update dependency sass to v1.47.0 #52
  • Update dependency sass to v1.46.0 #50
  • Update dependency postcss-cli to v9.1.0 #48
  • Update dependency sass to v1.45.0 #49
  • Update dependency sass to v1.44.0 #47
  • Update dependency postcss to v8.4.0 #46
  • Upgrade dependencies 8cff2ea
  • Update dependency release-it to v14.11.8 1a100d0
  • Update dependency release-it to v14.12.4 16dfa35

1.1.0

18 October 2021

  • Migrate from node-sass to dart sass #44
  • Update dependency postcss-cli to v9 #43
  • Bump tar from 6.1.5 to 6.1.11 #41
  • Bump path-parse from 1.0.6 to 1.0.7 #39
  • Update dependency release-it to v14.11.0 #38
  • Bump tar from 6.1.0 to 6.1.5 #37
  • Update dependency release-it to v14.10.0 #36
  • Update dependency release-it to v14.9.0 #35
  • Update dependency release-it to v14.8.0 #32
  • Update dependency release-it to v14.7.0 #31
  • Update dependency postcss to v8.3.0 #30
  • Update dependencies #29
  • Bump hosted-git-info from 2.8.8 to 2.8.9 #28
  • Update dependency release-it to v14.6.0 #21
  • Update dependency node-sass to v6.0.1 b84aed7
  • Update dependency release-it to v14.11.6 eb98c88
  • Update dependency release-it to v14.11.3 fd45ed9

1.0.0

5 April 2021