.cards {
width: 'window.screen.height / 15 or whatever' + px;
height: '.cards.width * 1.5' + px;
}
I have this code, I want to set the width of the cards class to a percent of the screen size in some way.
I don’t want to edit the size of each individual element of the class because I think it would take more processing power. Instead, I want to change the entire class so that the next element I create for that class will have these updated attributes, is there any easy way to do this?
I tried inserting a script tag in the style sheet, but that just made me feel stupid so I asked chatGPT and it couldn’t understand what the problem was, so I got no help there either.
2
Answers
You can just use CSS:
vh is 1 percent height of viewport (vw 1 % of width of viewport)
Although the current task (querying screen height) is achievable using just CSS too, it’s not impossible to modify CSS with JavaScript. In fact, you can introduce variables in CSS as well, which you can later manipulate.
You can influence the attributes of any DOM element (
style="..."
) by querying the element and using thesetProperty
function within thestyle
property. I’ve declared variables in the root element, so modifying them can be easily achieved through thestyle
property of thedocument.documentElement
root element.