Add CSS to a JavaScript button
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
javascript css button
add a comment |
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
javascript css button
Simply add the CSS property you want likeleftBtn.marginLeft = "20px"
.....
– Mamun
Nov 12 '18 at 14:07
Does this work with classes?
– Auron Hines
Nov 12 '18 at 14:08
add a comment |
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
javascript css button
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
javascript css button
javascript css button
edited Nov 12 '18 at 14:30
Auron Hines
asked Nov 12 '18 at 14:05
Auron HinesAuron Hines
83
83
Simply add the CSS property you want likeleftBtn.marginLeft = "20px"
.....
– Mamun
Nov 12 '18 at 14:07
Does this work with classes?
– Auron Hines
Nov 12 '18 at 14:08
add a comment |
Simply add the CSS property you want likeleftBtn.marginLeft = "20px"
.....
– Mamun
Nov 12 '18 at 14:07
Does this work with classes?
– Auron Hines
Nov 12 '18 at 14:08
Simply add the CSS property you want like
leftBtn.marginLeft = "20px"
.....– Mamun
Nov 12 '18 at 14:07
Simply add the CSS property you want like
leftBtn.marginLeft = "20px"
.....– Mamun
Nov 12 '18 at 14:07
Does this work with classes?
– Auron Hines
Nov 12 '18 at 14:08
Does this work with classes?
– Auron Hines
Nov 12 '18 at 14:08
add a comment |
7 Answers
7
active
oldest
votes
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.
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
– Auron Hines
Nov 12 '18 at 14:16
then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
– Ron C
Nov 12 '18 at 14:18
leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary";
Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
– Auron Hines
Nov 12 '18 at 14:27
?? 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?
– Ron C
Nov 12 '18 at 14:30
1
I see on the page you linked to that you need to use "btn btn-primary" so useleftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
– Ron C
Nov 12 '18 at 14:32
|
show 7 more comments
You could add a class to your buttons, and then add any styling in CSS:
leftBtn.className = "class_name"
add a comment |
You probably need to use the className to your Javascript:
EXAMPLE:
leftBtn.className = "name";
Hope this helps :)
add a comment |
you can use for example :
leftBtn.style.color = "blue";
Lear more about in https://developer.mozilla.org/es/docs/Web/API/HTMLElement/style
add a comment |
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
add a comment |
Just do something like this
leftBtn.style.marginLeft = '20px'
rightBtn.style.marginLeft = '20px'
I guess it
add a comment |
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/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263825%2fadd-css-to-a-javascript-button%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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
– Auron Hines
Nov 12 '18 at 14:16
then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
– Ron C
Nov 12 '18 at 14:18
leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary";
Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
– Auron Hines
Nov 12 '18 at 14:27
?? 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?
– Ron C
Nov 12 '18 at 14:30
1
I see on the page you linked to that you need to use "btn btn-primary" so useleftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
– Ron C
Nov 12 '18 at 14:32
|
show 7 more comments
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.
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
– Auron Hines
Nov 12 '18 at 14:16
then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
– Ron C
Nov 12 '18 at 14:18
leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary";
Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
– Auron Hines
Nov 12 '18 at 14:27
?? 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?
– Ron C
Nov 12 '18 at 14:30
1
I see on the page you linked to that you need to use "btn btn-primary" so useleftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
– Ron C
Nov 12 '18 at 14:32
|
show 7 more comments
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.
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.
answered Nov 12 '18 at 14:08
Ron CRon C
9,31163873
9,31163873
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
– Auron Hines
Nov 12 '18 at 14:16
then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
– Ron C
Nov 12 '18 at 14:18
leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary";
Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
– Auron Hines
Nov 12 '18 at 14:27
?? 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?
– Ron C
Nov 12 '18 at 14:30
1
I see on the page you linked to that you need to use "btn btn-primary" so useleftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
– Ron C
Nov 12 '18 at 14:32
|
show 7 more 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
– Auron Hines
Nov 12 '18 at 14:16
then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
– Ron C
Nov 12 '18 at 14:18
leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary";
Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)
– Auron Hines
Nov 12 '18 at 14:27
?? 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?
– Ron C
Nov 12 '18 at 14:30
1
I see on the page you linked to that you need to use "btn btn-primary" so useleftBtn.className = "btn btn-primary"; rightBtn.className = "btn btn-primary"; fireBtn.className = "btn btn-primary";
– Ron C
Nov 12 '18 at 14:32
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
– Auron Hines
Nov 12 '18 at 14:16
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
– Auron Hines
Nov 12 '18 at 14:16
then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
– Ron C
Nov 12 '18 at 14:18
then in my example just change leftOne, rightOne and fireOne to btn and then each will have the btn css class assigned to it.
– Ron C
Nov 12 '18 at 14:18
leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary";
Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)– Auron Hines
Nov 12 '18 at 14:27
leftBtn.className = "btn-primary"; rightBtn.className = "btn-primary"; fireBtn.className = "btn-primary";
Doesn't work. (picturepan2.github.io/spectre/elements/buttons.html)– Auron Hines
Nov 12 '18 at 14:27
?? 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?– Ron C
Nov 12 '18 at 14:30
?? 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?– Ron C
Nov 12 '18 at 14:30
1
1
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";
– Ron C
Nov 12 '18 at 14:32
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";
– Ron C
Nov 12 '18 at 14:32
|
show 7 more comments
You could add a class to your buttons, and then add any styling in CSS:
leftBtn.className = "class_name"
add a comment |
You could add a class to your buttons, and then add any styling in CSS:
leftBtn.className = "class_name"
add a comment |
You could add a class to your buttons, and then add any styling in CSS:
leftBtn.className = "class_name"
You could add a class to your buttons, and then add any styling in CSS:
leftBtn.className = "class_name"
answered Nov 12 '18 at 14:08
McVencoMcVenco
1,00011528
1,00011528
add a comment |
add a comment |
You probably need to use the className to your Javascript:
EXAMPLE:
leftBtn.className = "name";
Hope this helps :)
add a comment |
You probably need to use the className to your Javascript:
EXAMPLE:
leftBtn.className = "name";
Hope this helps :)
add a comment |
You probably need to use the className to your Javascript:
EXAMPLE:
leftBtn.className = "name";
Hope this helps :)
You probably need to use the className to your Javascript:
EXAMPLE:
leftBtn.className = "name";
Hope this helps :)
answered Nov 12 '18 at 14:08
Ben SwindellsBen Swindells
1809
1809
add a comment |
add a comment |
you can use for example :
leftBtn.style.color = "blue";
Lear more about in https://developer.mozilla.org/es/docs/Web/API/HTMLElement/style
add a comment |
you can use for example :
leftBtn.style.color = "blue";
Lear more about in https://developer.mozilla.org/es/docs/Web/API/HTMLElement/style
add a comment |
you can use for example :
leftBtn.style.color = "blue";
Lear more about in https://developer.mozilla.org/es/docs/Web/API/HTMLElement/style
you can use for example :
leftBtn.style.color = "blue";
Lear more about in https://developer.mozilla.org/es/docs/Web/API/HTMLElement/style
answered Nov 12 '18 at 14:10
Matias SimoneMatias Simone
154
154
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 12 '18 at 14:43
Eugene DzhevadovEugene Dzhevadov
1064
1064
add a comment |
add a comment |
Just do something like this
leftBtn.style.marginLeft = '20px'
rightBtn.style.marginLeft = '20px'
I guess it
add a comment |
Just do something like this
leftBtn.style.marginLeft = '20px'
rightBtn.style.marginLeft = '20px'
I guess it
add a comment |
Just do something like this
leftBtn.style.marginLeft = '20px'
rightBtn.style.marginLeft = '20px'
I guess it
Just do something like this
leftBtn.style.marginLeft = '20px'
rightBtn.style.marginLeft = '20px'
I guess it
answered Nov 12 '18 at 14:13
Igor BezsmertnyiIgor Bezsmertnyi
1
1
add a comment |
add a comment |
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/
add a comment |
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/
add a comment |
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/
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/
answered Nov 12 '18 at 14:35
VladoVlado
1,0761011
1,0761011
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263825%2fadd-css-to-a-javascript-button%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Simply add the CSS property you want like
leftBtn.marginLeft = "20px"
.....– Mamun
Nov 12 '18 at 14:07
Does this work with classes?
– Auron Hines
Nov 12 '18 at 14:08