Entrance Events! Chat Gallery Search Everyone Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining the forum??
June 01, 2025 - @282.37 (what is this?)
Activity rating: Three Stars Posts & Arts: 31/1k.beats ~ Boop! The forum will close in 718.beats! Unread Topics | Unread Replies | My Stuff | Random Topic | Recent Posts Start New Topic  Submit Art
News: :eyes: ~ Inconvenience is counterculture ~ :eyes: Super News: Upload a banner!

+  MelonLand Forum
|-+  World Wild Web
| |-+  ✁ ∙ Web Crafting
| | |-+  JS question: setting new elements styles pragmatically vs with external css?


« previous next »
Pages: [1] Print
Author Topic: JS question: setting new elements styles pragmatically vs with external css?  (Read 371 times)
candycanearter07
Hero Member ⚓︎
*****


i like slimes

⛺︎ My Room
SpaceHey: Friend Me!
StatusCafe: candycanearter
Itch.io: My Games
RSS: RSS

View Profile WWWArt

Goomy, I Choose You!Suck At Something September - Did It!uh oh! a pigeon got in!Artsy Candy CaneJoined 2024!
« on: September 03, 2024 @47.60 »

hi,

I'm working on (another) thing that creates elements with document.createElement(), and I was wondering: Is it "better" to set all the elements' style with element.style.x = y or by creating an entirely seperate css sheet to import?

Also, is there any way to set a class's style with js? Like changing the right: attribute for all elements in a class?
Logged

new to oldnet be nice




Rose
Casual Poster ⚓︎
*

⛺︎ My Room

View Profile WWW

Actually A FlowerJoined 2024!
« Reply #1 on: September 03, 2024 @73.21 »

Hey 🙂 Will give a shot at answering although I am not sure I 100% understand the context behind your question so it might not fit what you are looking for.

Quote
and I was wondering: Is it "better" to set all the elements' style with element.style.x = y or by creating an entirely seperate css sheet to import?

I feel like the answer is "it depends" but I think my instinct would be to assign them all the same class name and then have a stylesheet that targets that class name. So like


Code
let spanElement = document.createElement("span");
spanElement.className = "cute-little-span"
document.body.appendChild(span);

...and then in your style.css (or whatever)

Code
.cute-little-span {
  color: red;
}

.... Is that kind of the answer you were looking for?
« Last Edit: September 03, 2024 @91.02 by Rose » Logged
candycanearter07
Hero Member ⚓︎
*****


i like slimes

⛺︎ My Room
SpaceHey: Friend Me!
StatusCafe: candycanearter
Itch.io: My Games
RSS: RSS

View Profile WWWArt

Goomy, I Choose You!Suck At Something September - Did It!uh oh! a pigeon got in!Artsy Candy CaneJoined 2024!
« Reply #2 on: September 03, 2024 @76.28 »

.... Is that kind of the answer you were looking for?

That seems to solve the issue I had, thanks. However, I assume that you had meant to put down spanElement.className = "cute-little-span" instead of span.className = "cute-little-span", since I don't think span is defined here? also, do you happen to know how to set the style for an entire class through the javascript? thanks!
« Last Edit: September 14, 2024 @111.97 by Melooon » Logged

new to oldnet be nice




Rose
Casual Poster ⚓︎
*

⛺︎ My Room

View Profile WWW

Actually A FlowerJoined 2024!
« Reply #3 on: September 03, 2024 @94.73 »

Quote
I assume that you had meant to put down spanElement.className


Ah yes typo sorry 🙂 Fixed it!

Quote
Also, is there any way to set a class's style with js? Like changing the right: attribute for all elements in a class?

Not with vanillaJS that I know of 🤔 I think there's "css-in-js" libraries that might sort-of accomplish that. I know this was popular with the React community for awhile. I don't think I'd recommend it myself!

Possible suggestion that might work for you depending on what you need - You could use CSS variables and modify them with CSS.

So with the CSS you could have like

Code
:root {
  --text-color: black;
}

.my-cute-span {
  color: var(--text-color);
}

and then in JS you could modify variables

Code
let root = document.querySelector(':root');
root.style.setProperty("--text-color", "pink");
Logged
Pages: [1] Print 
« previous next »
 

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021 | Privacy Notice | ~ Send Feedback ~ Forum Guide | Rules | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi