Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
a Summer day - @401.70
Activity rating: Three Stars Posts & Arts: 49/1k.beats ~ Boop! The forum will close in 599.beats! Random | Recent Posts | Guild Recents
News: :4u: ~~~~~~~~~~~  :4u: Guild Events: Summerween Watch-a-thon

+  MelonLand Forum
|-+  Life & The Web
| |-+  ✁ ∙ 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 1132 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: an Autumn night » 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: buzzystickerSave the saveshoeBlob Creaturecards all the way downCommon Slorg
Rose
Casual Poster ⚓︎
*
View Profile WWW

⛺︎ My Room

Artifacts:
Actually A FlowerJoined 2024!
« Reply #1 on: an Autumn night » 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: an Autumn night 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: an Autumn night » 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: an Autumn night 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: buzzystickerSave the saveshoeBlob Creaturecards all the way downCommon Slorg
Rose
Casual Poster ⚓︎
*
View Profile WWW

⛺︎ My Room

Artifacts:
Actually A FlowerJoined 2024!
« Reply #3 on: an Autumn night » 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 MelonLand Zap! Want to Login or Join ? Forum Art Hub Chat Webring Editor Recent Edits Tenement Arcade Web Guides Graphics Catalogue Wiki Newsletters Image Stream
Melon's Sites TamaNotchi Textures PixelSea GifyPet MoMG Ozwomp Online Loom Videos Leaky Webring Melonking
Tools Melon Software ArtHub Embed Maker ML Passports
Outlinks Webrings Internet Phone Book HTML Energy Declarations Hackers & Designers Frutiger Aero Forum m15o's Web Services 32Bit Cafe iMood
Minecraft: Online
Join: craft.melonking.net