question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible to use css function to validate css rules?

See original GitHub issue

Is it possible to use .css to validate some basic inline rules to see if they are valid?

var tree = $("<div><span></span></div>");
tree.find('span')
    .css("background-color","#eee");
console.log(tree); // span changed 

tree.find('span')
    .css("background-color","blaahhhhcolor");
console.log(tree); //no change to span

tree.find('span')
    .css("background-colaar","black");
console.log(tree); //no change to span

Compare both before and after and see if there has been an inline style attached, if there is it is valid?

If not would it be possible to add some type of callback to CSS to suggest the given rules are valid? I know jQuery isn’t a linter but in my case and possibly others this may suffice.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mgolcommented, Aug 30, 2016

You can just check background-color after the change and if its value is different than what you set it to it means the rule was invalid.

If you want something more, that’s more a job for a plugin; this is way too specific to be included in jQuery Core.

0reactions
mscreeniecommented, Aug 30, 2016

Thank you. This could be achieved without jQuery in that case.

Here’s my basic function if it helps anyone.

function validateCss(rules) {

    var original = "<div></div>";
    var parse_bench = $("<div>" + original + "</div>");
    parse_bench.find("div").css(rules);

    if (original != parse_bench.html()) {
        return true;
    } else {
        return false;
    }

}


validateCss({
    "background": 'blue'
}); // true

validateCss({
    "background": 'bluea'
}); // false

Read more comments on GitHub >

github_iconTop Results From Across the Web

valid - CSS: Cascading Style Sheets - MDN Web Docs
This pseudo-class is useful for highlighting correct fields for the user. Syntax. :valid. Examples. Indicating valid and invalid form fields.
Read more >
CSS Validator User Manual - Jigsaw - W3C
When using the basic interface, the validator will check the compliance against CSS 2.1, which is the current CSS technical recommendation. It will...
Read more >
Form Validation UX in HTML and CSS
Perhaps the easiest possible validation on a form you can do is use the required attribute to require a field.
Read more >
WebD2: Validating Your CSS - University of Washington
Validate a Sample Page · Open the sample page with invalid CSS . · View the web page's source code. · Save the...
Read more >
CSS Validator
Validate your CSS files using our free css validator. When using this tool, you'll find errors and warnings that may need to be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found