Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
Autumn, 18 Sep 2026 - @437.00
Forum Activity: Four Stars Random | Recent Posts | Guild Recents
News: It's only life! :dog: Guild Events: Prepare for Halloween Reminder!

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


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


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

Guild Memberships:
Artifacts:
Visited on Melon's 10th Anniversary!it's tbhchansey!Goomy, I Choose You!uh oh! a pigeon got in!Artsy Candy Cane
« on: Autumn, 03 Sep 2024 @048 beats » Embed

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
https://status.cafe/users/candycanearter/badge.png https://abslimeware.neocities.org/assets/images/blinkers/penguins.gif

https://abslimeware.neocities.org/assets/images/blinkers/slimebounce.gif https://card.exophase.com/2/0/268504.png?1727352149

https://i.imgur.com/S1cx8ZZ.pnghttps://i.imgur.com/7ntZZGM.pnghttps://i.imgur.com/xKIpW2A.pnghttps://i.imgur.com/YMPbu9R.png

Artifact Swap: buzzystickerSootBlob Creature
Rose
Casual Poster ⚓︎
*
View Profile WWW

⛺︎ My Room

Artifacts:
Actually A FlowerJoined 2024!
« Reply #1 on: Autumn, 03 Sep 2024 @073 beats » Embed

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: Autumn, 03 Sep 2024 @091 beats by Rose » Logged
candycanearter07
Hero Member ⚓︎
*****
View Profile WWWArt


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

Guild Memberships:
Artifacts:
Visited on Melon's 10th Anniversary!it's tbhchansey!Goomy, I Choose You!uh oh! a pigeon got in!Artsy Candy Cane
« Reply #2 on: Autumn, 03 Sep 2024 @076 beats » Embed

.... 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: Autumn, 14 Sep 2024 @112 beats by Melooon » Logged

new to oldnet be nice
https://status.cafe/users/candycanearter/badge.png https://abslimeware.neocities.org/assets/images/blinkers/penguins.gif

https://abslimeware.neocities.org/assets/images/blinkers/slimebounce.gif https://card.exophase.com/2/0/268504.png?1727352149

https://i.imgur.com/S1cx8ZZ.pnghttps://i.imgur.com/7ntZZGM.pnghttps://i.imgur.com/xKIpW2A.pnghttps://i.imgur.com/YMPbu9R.png

Artifact Swap: buzzystickerSootBlob Creature
Rose
Casual Poster ⚓︎
*
View Profile WWW

⛺︎ My Room

Artifacts:
Actually A FlowerJoined 2024!
« Reply #3 on: Autumn, 03 Sep 2024 @095 beats » Embed

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 Embed 
« previous next »
 

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021 | Privacy Notice | Send Feedback | Supporters ♥ 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
@000 Melon
Land
Editor Recent Edits Tenement Arcade Random Link Land → Forum Art Hub Chat Webring Want to Login or Join ? Web Craft Guide Graphic Catalogue Wiki Newsletters Image Stream
Passport Sites Asatte Add your site?
Zap!
Minecraft: Online
Join: craft.melonking.net