2

On my project I use JS Buttons a lot, I can't find a way to add CSS to them. Can you help?

leftBtn = document.createElement('button');
leftBtn.innerText = "<";
rightBtn = document.createElement('button');
rightBtn.innerText = ">";
fireBtn = document.createElement('button');
fireBtn.innerText = "*";

Using CSS classes to change buttons: <button class="btn">default button</button>

Class used

2
  • Simply add the CSS property you want like leftBtn.marginLeft = "20px"..... Commented Nov 12, 2018 at 14:07
  • Does this work with classes? Commented Nov 12, 2018 at 14:08

7 Answers 7

3

You could use this code to add a css class to each button:

 leftBtn.className = "leftOne";
 rightBtn.className = "rightOne";
 fireBtn.className = "fireOne";

Then you can use regular css to style them.

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

9 Comments

I want something like this: <button class="btn">default button</button> except I can use class="btn" on it so it has the CSS style of the button
then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary"; Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
?? Doing so assigns the "btn-primary" css class to each button an allows you to style the button by using <style> .btn-primary {/*some styles here*/}; </style> What doesn't work about it?
I see on the page you linked to that you need to use "btn btn-primary" so use leftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
|
1

You could add a class to your buttons, and then add any styling in CSS:

leftBtn.className = "class_name"

Comments

1

You probably need to use the className to your Javascript:

EXAMPLE:

leftBtn.className = "name";

Hope this helps :)

Comments

1

you can use for example :

leftBtn.style.color = "blue";

Lear more about in https://developer.mozilla.org/es/docs/Web/API/HTMLElement/style

Comments

1

In general, there are two main ways to do that.

The first one using inline styles, just keep in mind that inline style has the highest precedence in a document.

elt.style.color = '...'

or

elt.setAttribute('style', '...')

More info here

The second is by using the class name. You can simply define a class name and write CSS for this class name.

element.className.add = 'your-class-name'

then

.your-class-name { color: red }

In a second way, you can manage class names by using methods like add, remove, toggle

More info here

Comments

0

Just do something like this

leftBtn.style.marginLeft = '20px'
rightBtn.style.marginLeft = '20px'

I guess it

Comments

0
var leftBtn = document.createElement('button');
leftBtn.innerText = "<";
leftBtn.className = "test_style"; // Set class name
leftBtn.style.color = "#000"; // You can also set style properties inline

...

Here is a playground:

https://jsfiddle.net/u5hojsgb/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.