英文:
Why do I need to use CSS when I can just use Javascript?
问题
Context:
我对编程并不陌生,但我对前端开发还是新手。也就是说,我的背景主要是科学计算,现在我正在构建一个基本的Web应用程序。
Main:
我正在从零开始使用Svelte/SvelteKit。我对JS/TS/CSS/HTML有一些基本的了解,至少在语法上是这样。最近,我遇到了以下问题。
我想要同时更改Web页面的多种不同颜色。例如,链接和已访问文本的颜色。
我可以逐个设置每个颜色在<main>
下,但这很慢,容易出错,显然不是个好方法。
相反,我认为我可以设置一个CSS块来做到这一点,类似于以下方式:
<style>
a.link {
color: custom_color
}
</style>
其中custom_color
在<script>
中定义。我的直觉是,我可以使用Svelte将JavaScript变量传递给<style>
,因为在<main>
中我可以使用大括号语法这样做。我进行了一些调查,似乎没有一种简单的方法可以实现我所寻求的功能,因此似乎我将不得不依赖于一些使用JavaScript中的document.getElementsByName()
方法的方法。
所有这些都让我产生了一个问题:如果我可以用JavaScript操纵一切,那CSS有什么用?如果我不能将变量传递给CSS,CSS似乎并不是很有用。这是Svelte的瓶颈吗?我是否遗漏了一些明显的东西?
编辑:
对于处于类似境地的任何人,只需使用sass/scss。我创建了一个theme.scss文件,我可以在我的任何Svelte页面中导入和使用它。您还需要在<style>
中设置lang='sass'
。
英文:
Context:
I am not new to programming, but I am new to front end development. That is, my background is mainly in scientific computing, and I now am building a basic web application.
Main:
I am starting from the ground up using Svelte/SvelteKit. I have a basic understanding of JS/TS/CSS/HTML, at least syntactically. Recently, I have run into the following issue.
I want to use custom colors/custom variable names for these colors (e.g., blue_light, blue_dark, which I have defined in another file and imported into my main +page.svelte) that match the specific pantone colors of the brand, plus it keeps things more organized for the team.
What I am seeking is a way to change several different colors of the web page at once. For example, the colors of link and visited text.
I could go through one-by-one and set each one under <main>, but this is slow and prone to errors, obviously.
Instead, I thought I could set a CSS block to do exactly that, something like as follows:
<style>
a.link {
color: custom_color
}
</style>
where custom_color
is defined somewhere in <script>
. My intuition was that I could pass a JavaScript variable to <style>
with Svelte, because I can do so in <main>
with curly brace syntax. I have done some digging, and it doesn't seem like there's an easy way to get what I am seeking, so it seems like I will have to rely on some method that utilizes document.getElementsByName()
in Javascript.
All of this leads me to my question: What is the point of CSS, when I can manipulate everything with JavaScript? CSS does not seem very useful if I cannot pass variables to it. Is this a bottleneck of Svelte? Am I missing something obvious?
I have looked into JQuery to manipulate the page more concisely, but this does not lead me to understanding why CSS is necessary when page manipulation can just be handled with JavaScript.
edit:
For anyone that's in a similar spot, just use sass/scss. I created a theme.scss file that I can import and use in any of my svelte pages. You'll also want to set lang='sass'
in <style>
.
答案1
得分: 2
样式被预处理并编译成单独的CSS资源,如果它们具有动态部分,这将根本不可能。
一般来说,CSS应该关注样式/布局,而JS应该关注状态/逻辑。这使得更容易知道什么应该放在哪里/可以找到,并且更容易阅读。此外,有很多事情在JS中根本无法做到,反之亦然,这些语言都是为了它们特定的角色而设计的。
如果我不能将变量传递给CSS,CSS似乎并不是很有用。这是Svelte的瓶颈吗?我是否遗漏了一些明显的东西?
一般来说,你不需要这样做。你设置状态,例如类和属性,所有必要的样式或布局更改都应在CSS中发生。如果你想在“代码中做一切”,那你可能是在错误的方式上思考问题;Svelte在这方面有一些限制,但如果样式处理得当,这些限制实际上并不重要。
当确实有必要时,你也可以通过使用style
属性传递变量,设置可以在CSS中访问的自定义属性。这可能在移动诸如弹出窗口之类的元素时是必要的,当CSS中无法完全计算布局时。
英文:
Styles are preprocessed and compiled to separate CSS assets, this would simply not be possible if they had dynamic parts.
In general CSS should concern itself with styling/layout and JS with state/logic. This makes it easier to know where what should go/can be found and easier to read. Also there are many things you just cannot do in JS at all and vice versa, the languages are designed for their specific roles.
> CSS does not seem very useful if I cannot pass variables to it. Is this a bottleneck of Svelte? Am I missing something obvious?
In general you just don't have to. You set state, e.g. classes and attributes, and all the necessary styling or layout changes that state should entail happen in CSS. If you want to do everything "in code' you are probably thinking about problems the wrong way; Svelte has some limitations here but they really don't matter much if styling is approach the right way.
When it really is necessary, you also can pass variables by using the style
attribute, setting custom properties which can be accessed in CSS. This can, for example, be necessary to move elements like popups around, when layout cannot be fully calculated in CSS.
答案2
得分: 0
你的问题有点不太合理,因为JavaScript没有任何控制文档外观的功能。只有两件事可以控制文档的外观:CSS和(现在较少使用的)HTML属性。
CSS是现代Web开发中控制渲染样式的主要机制。CSS属性,如margin
,color
,background-image
和font-size
,控制元素的外观。
有三种指定CSS样式的方法:
-
在元素的 "style" 属性中:
<span id="hello" style="font-size:24px;color:blue"> Hello blue text! </span>
或者在JavaScript中:
let hello = document.getElementById('hello'); hello.style.fontSize = '24px'; hello.style.color = 'blue';
-
在
<style>
节点中:<style> #hello { font-size: 24px; color: blue; } </style>
-
从外部CSS文件链接:
<link rel="stylesheet" type="text/css" href="hello.css" />
所有这些都是有效的。你使用哪种方法取决于你的需求。
有一些开发团队或工作环境可能有规定你可以或不能使用的规则,比如禁止在元素上设置样式(因此禁止直接使用JavaScript设置样式),但这些只是意见。要知道他们制定这些规则的原因,你需要询问那些制定规则的人(如果我要详细阐述我的观点,这个回答可能会写成一本小书)。
英文:
Your question kind of does not make sense since javascript does not have any feature to control how a document should look. Only two things can control how a document should look: CSS and (the now less commonly used) HTML properties.
CSS is the main mechanism in modern web development for controlling rendering sytles. CSS properties such as margin
, color
, background-image
and font-size
control how elements should look.
There are three ways of specifying CSS styles:
-
In the element
"style"
property:<span id="hello" style="font-size:24px;color:blue"> Hello blue text! </span>
or in javascript:
let hello = document.getElementById('hello'); hello.style.fontSize = '24px'; hello.style.color = 'blue';
-
In a
<style>
node:<style> #hello { font-size: 24px; color: blue; } </style>
-
Linked from an external CSS file:
<link rel="stylesheet" type="text/css" href="hello.css" />
All are valid. What you use will depend on what you need.
There are some development teams or work environments that may have rules of what you can and cannot use such as banning styles on elements (thereby banning setting styles directly with javascript) but those are opinions. To know why they made such rules you'll have to ask the people who made those rules (and if I were to elaborate my opinion in this answer I can write a small book).
答案3
得分: -1
你可以在你的CSS中定义变量在:root
中:
:root {
--custom_color: #0085ff;
}
然后使用var()
函数访问它们:
a.link {
var(--custom_color);
}
更多信息,你可以阅读关于CSS变量的使用在W3Schools上。
你也可以像这样使用JavaScript来编辑CSS :root
中的变量:
document.documentElement.style.setProperty('--var', 'value');
document.documentElement.style.setProperty('--custom_color', '#ff8500');
英文:
You can define variables in your CSS :root
:
:root {
--custom_color: #0085ff;
}
and then access them with the var() function:
a.link {
var(--custom_color);
}
For more information, you can read this about CSS variable usage on W3Schools.
You can also edit the variables in your CSS :root
with Javascript like this:
document.documentElement.style.setProperty('--var', 'value');
document.documentElement.style.setProperty('--custom_color', '#ff8500');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论