Using pointer-events with Media Queries

By  on  

We all know that there are a number of ways we can modify layout based on CSS media queries -- hell, we can even animate properties between media queries. One bit of outside the box thinking with media queries is deals with pointer-events. Using the pointer-events property, we can also enable and disable some functionality with CSS based on media query state!

The scenario is one that's present in the current implementation of the MDN redesign. On the left you see article content, on the right you see a table of contents:

MDN Redesign Desktop

When pushed into the tablet media query, the table of contents should move above the content:

MDN Redesign Tablet

When on desktop, the table of contents should not be toggleable when the title is clicked. On tablet or other mobile device, however, the table of contents should toggle (slide up) when clicked to free up critical space, so as to avoid (possibly) loads of scrolling. I didn't want to use JavaScript to enable/disable the toggler, because...well...that would be inefficient. Instead I paired the pointer-events property with the tablet media query:

/* disable by default */
#toc .heading {
	pointer-events: none;
}
#toc i {
	display: none;
}

/* enable for tablet! */
@media only screen and (max-width: 760px) {
	#toc .heading {
		pointer-events: auto;
	}
	#toc i {
		display: block;
	}	
}

I absolutely adore pointer-events because it even prevents click events from firing, thus the JavaScript I use to trigger the slide up/down doesn't occur. While I wouldn't say what I've done is overly clever, I would like to say that you should think about modifying more than just layout within different media queries. Oh, and also, pointer-events is awesome!

Recent Features

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Incredible Demos

  • By
    Form Element AJAX Spinner Attachment Using MooTools

    Many times you'll see a form dynamically change available values based on the value of a form field. For example, a "State" field will change based on which Country a user selects. What annoys me about these forms is that they'll often do an...

  • By
    Fullscreen API

    As we move toward more true web applications, our JavaScript APIs are doing their best to keep up.  One very simple but useful new JavaScript API is the Fullscreen API.  The Fullscreen API provides a programmatic way to request fullscreen display from the user, and exit...

Discussion

  1. I like the theory in this! ,
    but I can’t see myself being able to be so gung-ho about IE11+ CSS properties in
    my professional life, haha… but it did spark a thought for me… a way of doing
    this kind of thing without writing “if/else” into every event…

    (function(){
    
      var tablet = 800, $win = $(window);
    
      $win.on("resize.binds", function() {
        if( $win.width() > tablet ) { responsiveUnbinds(); } else { responsiveBinds(); }
      });
    
      function responsiveUnbinds() {
        $('.el').off(".responsive");
      }
    
      function responsiveBinds() { 
        $('.el').on("click.responsive", function() { /* ... */ });
        // loads of fun events that should only work on > tablet
      }
    
      $win.trigger("resize.binds");
    
    })();
    
    

    I’m sure you could optimize that even more! but it should avoid all the hassle
    of events firing and then logic’ing

  2. Seems to me like you’re using CSS for functionality instead of style. Of course this has been growing more and more (transitions and animation), but something like this seems shouldn’t be that hard to do without it. Interesting usage though.

  3. SachaMPS

    Haha, i found pointer events also interesting until i figured out IE9 doesnt support it. And in real live you still have to support this browser unfortunately. It’s already hard to explain even some bigger companies that IE8 times should really be finally over now. And I also think twice before i try to implement stuff like this via javascript…

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!