为什么我需要使用CSS,而不只是使用Javascript?

huangapple go评论79阅读模式
英文:

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:

&lt;style&gt;
    a.link {
        color: custom_color
    }
&lt;/style&gt;

where custom_color is defined somewhere in &lt;script&gt;. My intuition was that I could pass a JavaScript variable to &lt;style&gt; with Svelte, because I can do so in &lt;main&gt; 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=&#39;sass&#39; in &lt;style&gt;.

答案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属性,如margincolorbackground-imagefont-size,控制元素的外观。

有三种指定CSS样式的方法:

  1. 在元素的 "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';
    
  2. <style> 节点中:

    <style>
        #hello {
            font-size: 24px;
            color: blue;
        }
    </style>
    
  3. 从外部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:

  1. In the element &quot;style&quot; property:

     &lt;span id=&quot;hello&quot; style=&quot;font-size:24px;color:blue&quot;&gt;
         Hello blue text!
     &lt;/span&gt;
    

    or in javascript:

     let hello = document.getElementById(&#39;hello&#39;);
     hello.style.fontSize = &#39;24px&#39;;
     hello.style.color = &#39;blue&#39;;
    
  2. In a &lt;style&gt; node:

     &lt;style&gt;
         #hello {
             font-size: 24px;
             color: blue;
         }
     &lt;/style&gt;
    
  3. Linked from an external CSS file:

     &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;hello.css&quot; /&gt;
    

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(&#39;--var&#39;, &#39;value&#39;);

document.documentElement.style.setProperty(&#39;--custom_color&#39;, &#39;#ff8500&#39;);

huangapple
  • 本文由 发表于 2023年7月7日 06:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定