Weird little snafu. I'm using jQuery's .css()
method to change the size of text (long story; no, I can't use media queries) using a variable and I need to add em
to it. I'm not sure what the syntax is because there are multiple values for the CSS change.
To illustrate:
This works perfectly. It adds em
to the calculated value of victore
:
$('h1').css('font-size', victore + 'em');
This doesn't:
$('h1').css({
'font-size':victore + 'em',
'line-height':vignelli + 'em';
});
The em
needs quotes... but so does the value. Wrapping it in parens didn't work
Apply multiple CSS properties using a single JQuery method CSS( {key1:val1, key2:val2....). You can apply as many properties as you like in a single call. Here you can pass key as property and val as its value as described above.
jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.
The jQuery CSS methods allow you to manipulate CSS class or style properties of DOM elements. Use the selector to get the reference of an element(s) and then call jQuery css methods to edit it. Important DOM manipulation methods: css(), addClass(), hasClass(), removeClass(), toggleClass() etc.
Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css(“propertyName”);
You shouldn't have the quotes around the whole thing:
$('h1').css({
'font-size': victore + "em",
'color':'red'
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With