This question already has answers here:
How to get a style attribute from a CSS class by javascript/jQuery?
(8 answers)
Closed 2 years ago.
How do I get the CSS properties of an element with js? Let say I have the following
.btn {
background-color: green;
}
.navbtn {
font-size: 20px;
}
and
<button id='mybtn' class='btn navbtn' onclick='alert("hi")'>Click me</button>
Then, I want to extract the CSS properties of the element mybtn. If I do
window.getComputedStyle(document.getElementById('mybtn'))
it returns a CSSStyleDeclaration which contains all CSS properties, even those I didn't set. However, what I want is an object (or whatever) that contains
{
background-color: green;
font-size: 20px;
}
and everything else will consider as 'default'.
So my question is:
Is there such function or library that does the job?
If not, is it possible to get the CSS properties given the class name (like getProperties('btn') returns { background-color: green; })
Thanks in advance
So simple as this code :
let background = document.querySelector('.btn').style.backgroundColor;
console.log(backround);
Related
This question already has answers here:
modify a css rule object with javascript [duplicate]
(2 answers)
Set CSS property in JavaScript?
(11 answers)
Closed 17 days ago.
How can I add a new CSS property to an existing class with JavaScript?
My class:
.messageBox p{
border-bottom: 1px solid #808080;
margin: 2px;
margin-bottom: 1px;
}
I want to add min-height: 17px; to this already existing class.
There are ways for adding this to every element of the above mentioned class but I'm not able to handle later added elements.
Is this even a good approach to change the class or is there a more convenient way for doing this?
This question already has answers here:
Change CSS class properties with jQuery
(8 answers)
Changing CSS properties via JavaScript
(6 answers)
Closed 5 years ago.
Is there a way to use jQuery to set values inside a class? So for example if I have a class like this:
.menubar {
background: #333;
color: #fff;
}
Can I then run something like:
$('.menubar').css('background', '#999');
... to set the value of that class to #999? And I don't mean to select all the elements with the class and set their background, I mean can I change the actual classes value to #999 so that if an element is created later with the class it will have #999 instead of #333.
This question already has answers here:
Issue while setting html of div using the jquery html() method
(5 answers)
Closed 6 years ago.
I am trying to create div like so using jQuery:
let gridBlock = $("<div style=width:10px; height:20px; background-color:red;></div>");
However when I look at the render page the console shows that the div only has a style of width:
What am I doing wrong?
Though you have already got the answer in comment but there is a better way to implement using class. The reason css-specificity. Adding style using style attribute is hard to overwrite & need to use !important for that.
So to avoid that you can use the class attribute and define the style in css file
some.css
.someClass {
width: 10px;
height: 20px;
background-color: red;
}
in js file
let gridBlock = $("<div class='someClass'>Hello</div>");
DEMO
Use quotes mate
let gridBlock = $("<div style='width:10px; height:20px; background-color:red;'></div>");
This question already has answers here:
How to get an HTML element's style values in JavaScript?
(5 answers)
Closed 6 years ago.
I have a simple script that is getting the color of the text in an element and printing it to the console. However, when I run the script, I'm getting an empty string rather than the actual color. Can anyone explain to me why and how to fix it?
HTML
<div id="scrollingTextHolder">
<p id="scrollingText">Hello</p>
</div>
CSS
#scrollingText{
margin-top: 5%;
color: black;
}
JS
window.addEventListener("load", function(){
console.log(document.getElementById("scrollingText").style.color);
})
Pen
You can use getComputedStyle() and getPropertyValue(), also color is returned as rgb(R,G,B)
var a = document.getElementById("scrollingText");
console.log(window.getComputedStyle(a).getPropertyValue('color'))
#scrollingText {
margin-top: 5%;
color: black;
}
<div id="scrollingTextHolder">
<p id="scrollingText">Hello</p>
</div>
This question already has answers here:
Creating CSS Global Variables : Stylesheet theme management [duplicate]
(6 answers)
Is there any way to use variables in css?
(3 answers)
Closed 8 years ago.
Here is an example:
This is the normal css:
h1{
background-color: #fafafa;
}
I would like to change this to the following:
h1{
background-color: default or any id name
}
Where can I give a javascript command to change this color code(i.e #fafafa)? Where ever it is in the stylesheet to default or any id name.
So that I can use it in a color switcher to change the color for this code. I don't want to use less because I have already gone way to far in my project.
You can not do that as CSS is completely static what you can do is when you want to change the color for that element on a particular condition you can add an id to the element using javascript/jQuery by enclosing the element in a span/div in the first place. and write a new css for that particular id. so on your desired event new css will apply to that element and color will be changed at runtime.
Either you use some variables for example in the style you can use this:
var width = "150px";
And in the container use something like:
<div style="width: #width">A div with width 150px.</div>
But brother there is no default value for them.
As the code you are showing is using a hex value for a color. That cannot be converted to a default name. However using variable can do it. Or you can try using rgba. But there isn't any default name. However You can try to write the color name itself as:
color: white;
or
color: red;
But I am not sure it will work for you for your job.
You can give name to css property through this font-family
p
{
font-family:"Times New Roman",Georgia,Serif;
}