简体   繁体   English

SCSS 变量类名

[英]SCSS variable class name

On my website, I'm constantly doing style="font-size: #ofpx;".在我的网站上,我一直在做 style="font-size: #ofpx;"。 However, I was wondering if there's a way to do it with scss so that, when I declare a class, it would also change the font size.但是,我想知道是否有办法用 scss 来做到这一点,这样当我声明一个类时,它也会改变字体大小。 For example:例如:

<div class='col-lg-4 font-20'>whatever here</div>

and this would change my font-size to 20. If I did font-30, it would change my font-size to 30 and etc...这会将我的字体大小更改为 20。如果我使用 font-30,它会将我的字体大小更改为 30 等等......

What I have so far:到目前为止我所拥有的:

.font-#{$fontsize} {
      font-size: $fontsize;
}

This can't be done for arbitrary sizes.对于任意大小,这是无法做到的。 The nature of SCSS is that is needs to be flattened down to CSS before it gets applied to the HTML. SCSS 的本质是在应用于 HTML之前需要将其扁平化为 CSS。 What you are asking for, however, is essentially to create rules at run-time rather than compile-time.但是,您要求的是在运行时而不是编译时创建规则。

In other words, SCSS makes it easier to write some of the repetitive parts of CSS, but it doesn't allow you to do anything new that wasn't already possible with plain old CSS.换句话说,SCSS 使编写 CSS 的一些重复部分变得更容易,但它不允许您做任何使用普通旧 CSS 无法实现的新内容。

What you're asking for is also a code smell .您要求的也是代码气味 It smells like your markup isn't semantic enough.它闻起来像你的标记不够语义。 The purpose of a CSS class is to group objects with similar characteristics, but you're using them instead to describe the styles they impart. CSS 类的目的是将具有相似特征的对象分组,但您使用它们来描述它们赋予的样式。 I would suggest stepping back and reconsidering what it is that you really want.我建议退后一步,重新考虑你真正想要的是什么。

You obviously have details of certain elements that are context-dependent.您显然拥有某些与上下文相关的元素的详细信息。 For example, maybe you are applying these rules to buttons when you want to make them smaller or larger than usual.例如,当您想让按钮比平时更小或更大时,您可能正在将这些规则应用于按钮。 You need to identify the scenarios in which the buttons change.您需要确定按钮更改的场景。 Maybe they are 20% smaller if they are in a modal dialog?如果它们在模态对话框中,它们可能会小 20%? Then write your normal .button rules, and also create rules for .modal .button which make it smaller.然后编写正常的.button规则,并为.modal .button创建规则,使其更小。

If you're positive that you want to define font-size for each element within the HTML (and sometimes there are good reasons for doing so), just continue using inline styles.如果您确定要为 HTML 中的每个元素定义字体大小(有时这样做是有充分理由的),请继续使用内联样式。 The only reason inline styling is frowned upon is because it combines model and view logic in a way that harms reusability;内联样式不受欢迎的唯一原因是它以一种损害可重用性的方式结合了模型和视图逻辑; however, what you are requesting does so in exactly the same way.但是,您所请求的方式完全相同。 This is what inline styles were made for.这就是内联样式的用途。 Don't re-invent the wheel.不要重新发明轮子。

With all of that said, you can use sass loops to automatically generate classes for integers within a range.综上所述,您可以使用 sass 循环为范围内的整数自动生成类。 For example:例如:

/* warning: this is generally a bad idea */
@for $i from 1 through 100 {
  .font-#{$i} {
    font-size: #{$i}px;
  }
}

This is not a good idea.这不是一个好主意。 Pragmatically speaking it doesn't offer any advantages over just using inline styles and with large ranges your resulting file will be larger (which affects website load times).务实地说,与仅使用内联样式相比,它没有任何优势,并且在大范围内生成的文件会更大(这会影响网站加载时间)。


Aside: There is a CSS philosophy (or trend, if you're feeling ungenerous) called Atomic CSS (or sometimes Functional CSS) which defies the classical advice given in this answer.旁白:有一种 CSS 哲学(或趋势,如果你觉得不够慷慨的话)称为原子 CSS(或有时是功能 CSS),它违背了这个答案中给出的经典建议。 I won't give an opinion on its effectiveness at producing clean, maintainable code, but it does typically require more tooling than SCSS alone if used with the degree of specificity requested in this question.我不会对它在生成干净、可维护的代码方面的有效性发表意见,但如果在这个问题中要求的特定程度下使用,它通常需要比单独的 SCSS 更多的工具。

Just going to add, mixins are great, but if you want a util class (attach a class to an element, get that font-size applied to it, do a for-loop in SCSS like so..只是要补充一点,mixin 很棒,但是如果你想要一个 util 类(将一个类附加到一个元素上,将字体大小应用到它,在 SCSS 中做一个 for 循环,像这样..

@for $i from 1 through 4 {
    $fontsize: 10px * $i;
    .font-#{$i} { 
        font-size: $fontsize;
        }
}

compiles to编译为

.font-1 {
  font-size: 10px;
}

.font-2 {
  font-size: 20px;
}

.font-3 {
  font-size: 30px;
}

.font-4 {
  font-size: 40px;
}

If you want the class to match the # of pixels...如果您希望该类匹配像素数...

@for $i from 1 through 4 {
    $base: 10;
    $fontsize: $base * $i;
    .font-#{$fontsize} { 
        font-size: $fontsize + 0px;
        }
}

Which compiles to编译成

.font-10 {
  font-size: 10px;
}

.font-20 {
  font-size: 20px;
}

.font-30 {
  font-size: 30px;
}

.font-40 {
  font-size: 40px;
}

Codepen example.代码笔示例。

Of course, inline style tags are bad form.当然,内联style标签是不好的形式。 So yes, you should add some classes for font size, or just set font size on the elements you need to as you go.所以是的,您应该为字体大小添加一些类,或者只是在您需要的元素上设置字体大小。 Up to you.由你决定。 If you want, you could use a mixin like so:如果你愿意,你可以像这样使用 mixin:

@mixin font-size($size) {
  font-size: $size;
}

.some-div { @include font-size(10px); }

But that's probably overkill unless you get a group of rules that usually go together.但这可能是矫枉过正,除非你得到一组通常一起使用的规则。

When using "words" instead of "numbers" for variables, and the word not being at the end of the classname.当变量使用“单词”而不是“数字”时,并且单词不在类名的末尾。 I could work something out using CSS Attribute selectors ("wildcard selector").我可以使用 CSS 属性选择器(“通配符选择器”)解决一些问题。 I can iterate over a map object, and use text values to build CSS selectors.我可以遍历地图对象,并使用文本值来构建 CSS 选择器。

SASS SASS

//map
$colors: (
  primary: #121212,
  success: #8bcea8
);

//loop
@each $color, $value in $colors {
  //can't do this: div.first-class.is-style-#{$color}-component
  //can do this:
  div.first-class[class*="is-style-#{$color}-component"] {
     background-color: $value;
  }
}

HTML HTML

<div class="first-class is-style-primary-component"></div>

This will generate a div.myComponent[class*="is-style-primary-component"] selector and so <div class="first-class is-style-primary-component"></div> ( .first-class is not required, selector could be div[class*="is-style-#{$color}-component"] or even [class*="is-style-#{$color}-component"] only).这将生成一个div.myComponent[class*="is-style-primary-component"]选择器,因此<div class="first-class is-style-primary-component"></div> ( .first-class不是必需的,选择器可以是div[class*="is-style-#{$color}-component"]甚至[class*="is-style-#{$color}-component"] )。

Yet, in some cases of CSS class naming, it could be limited due to the wildcard selector, which is "larger" than a specific class selector rule.然而,在 CSS 类命名的某些情况下,它可能会由于通配符选择器而受到限制,它比特定的类选择器规则“大”。

Just for those of you who might stumble across this question in a more recent time and are new to FrontEnd Development.仅适用于最近可能偶然发现此问题并且不熟悉前端开发的人。

What Woodrow Barlow said about using inline-styles instead of rule specific classes isn't quite an up-to-date opinion. Woodrow Barlow 关于使用内联样式而不是规则特定类的说法并不是最新的观点。 For instance, Bootstrap has some of those and Tachyons is entirely built upon them.例如,Bootstrap 有一些,而 Tachyons 完全建立在它们之上。 Actually this practice is called Atomic CSS or Functional CSS.实际上,这种做法称为原子 CSS 或功能 CSS。

It's better explained by John Polacek in his CSS Tricks article: https://css-tricks.com/lets-define-exactly-atomic-css/ John Polacek 在他的 CSS Tricks 文章中更好地解释了这一点: https ://css-tricks.com/lets-define-exactly-atomic-css/

You can use mixins like this你可以像这样使用mixin

    @mixin font($fontsize) {
      font-size: $fontsize;
    }

    .box { 
        @include font(10px); 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM