Home Events! Entrance Everyone Wiki Search Login Register

Welcome, Guest. Please login or register. - Thinking of joining the forum??
November 21, 2024 - @901.60 (what is this?)
Forum activity rating: Three Stars Posts: 27/1k.beats Unread Topics | Unread Replies | My Stuff | Random Topic | Recent Posts    Start New Topic
News: :eyes: ~ Inconvenience is counterculture ~ :eyes:

+  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 154 times)
candycanearter07
Sr. Member ⚓︎
****


i like slimes

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

View Profile WWW

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
Sr. Member ⚓︎
****


i like slimes

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

View Profile WWW

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 »
 

Vaguely similar topics! (3)

Case Study - Design elements and hits, should we care?

Started by MelooonBoard ✁ ∙ Web Crafting

Replies: 3
Views: 1951
Last post April 22, 2022 @82.67
by Icey!
HELP: Elements disappearing when they are too long for the screen?

Started by wangobangyBoard ☔︎ ∙ I need Help!

Replies: 6
Views: 673
Last post June 06, 2023 @841.30
by wangobangy
Is it fanfiction without an existing cast and setting?

Started by starbreakerBoard ✑ ∙ Writing & Stationery

Replies: 6
Views: 742
Last post July 28, 2023 @524.18
by Memory

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021, Simple Machines | Terms and Policies 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