1

I know about matchMedia.js but I was thinking I could detect the current @media rule with something as easy as this. However, it's not working in firefox – (which usually means it's not correct in the first place...) Looking at it now, shouldn't the content be on an :after pseudo element? Any advice? I have a CodePen HERE:

CSS

#test {
  content:  "small"
}

@media only screen and (min-width: $bp1) {
  #test {
    content: "medium"
  }
}

jQuery

var sizeCheck = function() {
  
  if ( $("#test").css('content') === 'small') {
    
    $('.proof').text('jQuery knows this page is SMALL from reading the CSS @media rules.');

  } else if ( $("#test").css('content') === 'medium') {
    
    $('.proof').text('jQuery knows this page is MEDIUM from reading the CSS @media rules.');

  }
    
};

// run the function on document ready
$(document).ready(sizeCheck);

// AND run the function on window resize event
$(window).resize(sizeCheck);
1
  • Take a look at this article: css-tricks.com/… Commented Apr 22, 2014 at 23:04

1 Answer 1

1

You've got quite an interesting test there!

To answer your question: yes, content can only be used with :after and :before pseudo classes.

Applies to ::before and ::after pseudo-elements

Source: content - CSS | MDN

EDIT #1

I found something for you; looks like it is possible to get the content of :after 'after all' :)

var content = window.getComputedStyle(
    document.querySelector('#test'), ':after'
).getPropertyValue('content');

Source: Get Pseudo-Element Properties with JavaScript

I'm not used to CodePen, so created a jsfiddle.

EDIT #2

Looks like FireFox adds a double code to the actual value, so in the console it appeared like this: ""string"".

A way around this could be to test against both like this:

if (content == '"small"' || content == 'small') {
    // ...
}

I updated the fiddle here.

Sign up to request clarification or add additional context in comments.

6 Comments

Well, that adds :after but the jQuery isn't actually seeing it because it can't check after. I wonder is there is a property like content that can be descriptive AND be read by both. Otherwise I'll have to use display:none or something to detect.
Looks great, but still no dice in FireFox. Mysterious.
interesting, 'cause when i console.log(content) the value of content is there, but for some reason it's enclosed in two double quotes, like this: ""medium"" - have you seen that?
well, found a way around it (see my edit #2 above)... not sure if it's the perfect solution, but it definitely is one ;)
Hmmm. Great. I'll check back in after my day. I wonder if it makes a difference if you use single 'quotes' in the CSS.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.