How to modify root element with javascript [duplicate] - javascript

This question already has answers here:
How do I edit a CSS variable using JS?
(7 answers)
Closed 4 years ago.
I have got a question.
How to change font-size of :root, using javascript?
I need to make a button to resize font in a whole website.

While I have no idea what --font-size should be, accessing :root itself seems to be working with CSSStyleSheet.insertRule()
function doThing(){
document.styleSheets[0].insertRule(':root { font-size: 26px; }');
}
HelloWord<br>
<button onclick="doThing()">Clicky</button>

If you change the document.body.style.fontSize you are basically changing the :root. As long as the rest of your styles use em or rem font-sizes, all the text on the site should change accordingly.

Related

How to paint half of a letter or text in HTML(CSS)? [duplicate]

This question already has answers here:
Is it possible to apply CSS to half of a character?
(20 answers)
CSS Display One Character in 2 Colors [duplicate]
(2 answers)
Closed 5 years ago.
If I have : <p>A</p>
Is there any way in html or css or Javascript which I am able to paint half of "A" white and the other black or any other color.
Thanks
You can try it with gradients as Robbie suggested. There is only one down side of this. You won't be able to use text-shadow property (except with another hack - make duplicate of text and give it the shadow property)
Edited on request...
p {
font-size: 12px;
background: -webkit-linear-gradient(#000, #DDD);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
Source code can be found here https://css-tricks.com/snippets/css/gradient-text/

Why can't I create a div that has more than one style element? [duplicate]

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>");

can i give a name to the css property [duplicate]

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;
}

Pure CSS tooltip without jQuery [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I create a “tooltip tail” using pure CSS?
I would like to create a Facebook/ Twitter style tooltip (that thing when you hover over people's profiles and there's a short summary of their profile). I came across a lot of tutorials but they use jQuery. Is there a way to do this is CSS and maybe javascript?
+I'd like for this to work on images as well.
Thanks
You can do it with pure css
You can use the hover pseudo class to trigger it.
Here is a general concept that should get you started
div.profile-on-hover { display: none }
a:hover + .profile-on-hover { display: block }
<a>hover me</a>
<div class="profile-on-hover">bunch of stuff</div>
http://jsfiddle.net/AsKpv/
the div will need to follow the a tag for the '+' to work with CSS
if you want to make it snazzy you could use opacity: 0 and opacity:1 with a css3 transition to make it fade in as well.
good luck

jquery: how to change properties of whole css-class? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Changing a CSS rule-set from Javascript
I know that there are no variables in CSS, but if it is possible to change body of CSS-listing on the page with JQ - it may be work around of this. Also, I know aboud addClass/removeClass, and css('blabla','blabla') JQ-methods, and it is not what I need.
You can use a body class and then use CSS to manipulate only the class of body and then style everything based on your body class.
<body class="my-skin">
<p>Lorem<p>
</body>
And in your CSS:
.my-skin p{color: red}
.my-skin img{border:2px solid #333;margin:5px}
.second-skin p{color:blue}
jQuery:
$('body').addClass('my-skin');
//or
$('body').removeClass('my-skin').addClass('second-skin');

Categories