Home » Javascript » Set CSS property in Javascript?

Set CSS property in Javascript?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’ve created the following…

var menu = document.createElement('select');

How would I now set CSS attributes e.g width: 100px?

Answers:

Use element.style:

var element = document.createElement('select');
element.style.width = "100px";

Questions:
Answers:

Just set the style:

var menu = document.createElement("select");
menu.style.width = "100px";

Or if you like, you can use jQuery:

$(menu).css("width", "100px");

Questions:
Answers:

For most styles do this:

var obj = document.createElement('select');
obj.style.width= "100px";

For styles that have hyphens in the name do this instead:

var obj = document.createElement('select');
obj.style["-webkit-background-size"] = "100px"

Questions:
Answers:

That’s actually quite simple with vanilla JavaScript:

menu.style.width = "100px";

Questions:
Answers:

All of the answers tell you correctly how to do what you asked but I would advise using JavaScript to set a class on the element and style it by using CSS. That way you are keeping the correct separation between behaviour and style.

Imagine if you got a designer in to re-style the site… they should be able to work purely in CSS without having to work with your JavaScript.

In prototype I would do:

$(newElement).addClassName('blah')

Questions:
Answers:

if you want to add a global property, you can use:

    var styleEl = document.createElement('style'), styleSheet;
            document.head.appendChild(styleEl);
            styleSheet = styleEl.sheet;
            styleSheet.insertRule(".modal { position:absolute; bottom:auto; }", 0);

Questions:
Answers:
<h1>Silence and Smile</h1>
<input  type="button"  value="Show Red"   onclick="document.getElementById('h1').style.color='Red'"/>
<input  type="button"  value="Show Green" onclick="document.getElementById('h1').style.color='Green'"/>

Questions:
Answers:

When debugging, I like to be able to add a bunch of css attributes in one line:

menu.style.cssText = 'width: 100px';

Getting used to this style you can add a bunch of css in one line like so:

menu.style.cssText = 'width: 100px; height: 100px; background: #afafaf';

Questions:
Answers:
<body>
  <h1 id="h1">Silence and Smile</h1><br />
  <h3 id="h3">Silence and Smile</h3>

  <script type="text/javascript">
    document.getElementById("h1").style.color = "Red";
    document.getElementById("h1").style.background = "Green";
    document.getElementById("h3").style.fontSize = "larger" ; 
    document.getElementById("h3").style.fontFamily = "Arial";
  </script>
</body>

Questions:
Answers:

In react you can do this:

    <h1 style={{width: '100px', float: 'left',
 marginRight: '10px', marginTop: '-17px'}}>Hello</h1> //note the double brackets

or move the style to a style object and then do:

let styleObj = {width: '100px', float: 'left', marginRight: '10px', marginTop: '-17px'}  

<h1 style={styleObj}>Hello</h1> //no double brackets

camelCase where there’s a hyphen for a css attribute