Code Snippet - How to Add CSS Class to Jade
2014/05/311 min read
bookmark this
Table of Contents
Introduction
The following is an example of how to conditionally add a CSS class to an HTML element in Jade.
Example Code
Append the CSS class active when a variable is equal to
- The variable data.Value is coming from the Node.js server side.
input(type='submit' value='button' name="number")(class= data.Value === 1 ? "active" : "")
The HTML Code
The actual HTML code will generate like the following:
input name="pageNumber" type="submit" value="1"
Conclusion
Using ternary expressions in Jade (Pug) templates makes it easy to conditionally add CSS classes to HTML elements based on server-side variables.